I have a problem with one exercise that based on binary tree creation. I will try to explain. A task is: There is a array that was created by walking binary tree with preorder procedure. For example we have the following output of preorder procedure : 2 1 0 0 3 0 0. 2 is root of balanced binary tree, 1 is left child, 2 is right child. Node 1 and 2 are the leaves, so their children are 0 0 and 0 0. This output is made by preorder procedure that looks following:
printPreorder(root): if root: print(root.key) printPreorder(root.left) printPreorder(root.right)
I have class Tree (python)
class(Tree): def init(self, key): self.key = key self.left = None self.right = None
Using output of preorder procedure I need to creater array with objects that are the nodes of binary tree that was input in preorder procedure. How to make it maybe with recursion, maybe not.Each property of Tree objects have to be pointer on other ojects. I hope that who knows what I mean have to undersand me.
Aucun commentaire:
Enregistrer un commentaire