def traverse(self):
if self.root !=None:
print('*****Traversing*****')
print('self.root is', self.root.data)
print('self.root.Left is', self.root.leftchild.data)
print('self.root.Right is', self.root.rightchild.data)
self.traverse_in_order(self.root)
def traverse_in_order(self,node):
if node.leftchild!=None: #there is leftchild
print('node.leftchild is',node.leftchild.data )
self.traverse_in_order(node.leftchild) #go till end of leftnode
print('')
print('print node ', node.data)
print('')
if node.rightchild!=None:
print('Node.RIght',node.rightchild.data)
self.traverse_in_order(node.rightchild)
bst=BST()
bst.insert(32)
bst.insert(10)
bst.insert(1)
bst.insert(19)
bst.insert(46)
bst.traverse()
Output----
*****Traversing*****
self.root is 32
self.root.Left is 10
self.root.Right is 46
node.leftchild is 10
node.leftchild is 1
-------------print node 1
-------------print node 10
Node.RIght 19
print node 19
print node 32
Node.RIght 46
print node 46
Can anyone help with traverse_in_order function. My doubt is from root we go till last left node and then print its data ie(1 is printed understood.)How 10 is printed again.Same with rights subtree. I hope you got what iam asking. Thanks
"10" is not printed again. The only prints that matter are the ones that say "print node", and those are all unique and in order. The others are just debug prints to help you follow the logic. They don't count.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.