Created
December 26, 2019 00:13
-
-
Save luqs1/9d8ecee21c701ed9df5ca86b447c0f32 to your computer and use it in GitHub Desktop.
Binary Tree structure with in-order, pre-order, and post-order recursion support.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Look at end for details | |
| class Tree: | |
| def __init__(self,data,left,right): | |
| self.data = data | |
| self.left = left | |
| self.right = right | |
| def traverse1(self,*f): #In Order | |
| out = [] | |
| #out = '' | |
| if self.left != -1: | |
| out.append(self.left.traverse1(*f)) | |
| #out += self.left.traverse1() | |
| if f != (): | |
| out.append(f[0](self.data)) #,self.data)) | |
| #out += f[0](self.data) | |
| else: | |
| out.append(self.data) | |
| #out += self.data | |
| if self.right != -1: | |
| out.append(self.right.traverse1(*f)) | |
| #out += self.right.traverse1() | |
| return out | |
| def traverse2(self,*f): #Pre Order | |
| out = [] | |
| if f != (): | |
| out.append(f[0](self.data)) | |
| else: | |
| out.append(self.data) | |
| if self.left != -1: | |
| out.append(self.left.traverse2(*f)) | |
| if self.right != -1: | |
| out.append(self.right.traverse2(*f)) | |
| return out | |
| def traverse3(self,*f): #Post Order | |
| out = [] | |
| if self.left != -1: | |
| out.append(self.left.traverse2(*f)) | |
| if self.right != -1: | |
| out.append(self.right.traverse2(*f)) | |
| if f != (): | |
| out.append(f[0](self.data)) | |
| else: | |
| out.append(self.data) | |
| return out | |
| def traverse(self,order,*f): | |
| orders = ['in','pre','post'] | |
| a = orders.index(order) | |
| if a == 1: | |
| return self.traverse1(*f) | |
| if a == 2: | |
| return self.traverse2(*f) | |
| if a == 3: | |
| return self.traverse3(*f) | |
| return 'Invalid Order' | |
| A = Tree('Luqmaan',Tree('Tanya',Tree('Loletta',-1,-1),Tree('Adarsh',-1,-1)),Tree('Aryan',Tree('Aaron',-1,-1),-1)) | |
| ################################################################################## | |
| def createTree(schema,List): | |
| Deepest = True | |
| for I in schema: | |
| if type(I) is list: | |
| for A in I: | |
| if type(A) is list: | |
| Deepest = False | |
| if Deepest: | |
| a = List[schema[1][0]] | |
| b = List[schema[1][1]] | |
| if a != -1: | |
| a = Tree(List[schema[1][0]],-1,-1) | |
| if b != -1: | |
| b = Tree(List[schema[1][1]],-1,-1) | |
| return Tree(List[schema[0]],a,b) | |
| return Tree(List[schema[0]],createTree(schema[1],List),createTree(schema[2],List)) | |
| schema = [0,[1,[2,3]],[4,[5,6]]] | |
| items = ['Luqmaan','Tanya','Loletta','Adarsh','Aryan','Aaron',-1] | |
| # A schema is a model of the structure of the tree to be created, in the format: | |
| # [root,[left,right]] | |
| # when left or right are roots themselves you can recursively use the same notation so | |
| # [root, | |
| # [leftroot,[leftleft,leftright], | |
| # [rightroot,[rightleft,rightright] | |
| # ] | |
| # Rather than putting the data itself where the placeholders are | |
| # (in which case you should just follow the example in Variable A) | |
| # you can put all the data in a list, and put the indexes instead | |
| # allowing for a more concise "schema" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment