tree data structure in python

Solutions on MaxInterview for tree data structure in python by the best coders in the world

showing results for - "tree data structure in python"
Lilli
28 Oct 2019
1class TreeNode:
2    def __init__(self, data):
3        self.data = data
4        self.children = []
5        self.parent = None
6
7    def add_child(self, child):
8        child.parent = self
9        self.children.append(child)
10
11    def getlevel(self):
12        level = 0
13        p = self.parent
14        while p:
15            level += 1
16            p = p.parent
17        return level
18
19    def printt(self):
20        prefix = (" " * 4 * self.getlevel()) + ("|--" if self.parent else "")
21        print(prefix + self.data)
22        if self.children:
23            for child in self.children:
24                child.printt()
25
26
27def build_tree():
28    root = TreeNode("Food")
29
30    italy = TreeNode("Italy")
31    italy.add_child(TreeNode("Pizza"))
32    italy.add_child(TreeNode("Lasgna"))
33    italy.add_child(TreeNode("Pistacho Ice"))
34
35    chinese = TreeNode("Chineese")
36    chinese.add_child(TreeNode("Noodles"))
37    chinese.add_child(TreeNode("Rice balls"))
38    chinese.add_child(TreeNode("Fried Rice"))
39
40    mexican = TreeNode("Mexican")
41    mexican.add_child(TreeNode('Tacos'))
42    mexican.add_child(TreeNode('Gyro'))
43    mexican.add_child(TreeNode('Shawarma'))
44
45    root.add_child(italy)
46    root.add_child(chinese)
47    root.add_child(mexican)
48
49    return root
50
51    # mexican.printt()
52
53
54if __name__ == "__main__":
55    root = build_tree()
56    root.printt()
57
Emilia
04 Oct 2016
1import random
2class Node:
3    def __init__(self, v, dx=None, sx=None):
4        self.dx = dx
5        self.sx = sx
6        self.v = v
7
8    def nodeCount(self):
9        if self is None:
10            return 0
11        x, y = 0, 0
12        if self.sx:
13            x = self.sx.nodeCount()
14        if self.dx:
15            y = self.dx.nodeCount()
16        return x + y + 1
17
18    def minValue(self):
19        if self is None:
20            return float("+inf")
21        x = y = float("+inf")
22        if self.sx:
23            x = self.sx.minValue()
24        if self.dx:
25            y = self.dx.minValue()
26        return min(self.v, x, y) 
27
28    def maxValue(self):
29        if self is None:
30            return float("-inf")
31        x = y = float("-inf")
32        if self.sx:
33            x = self.sx.maxValue()
34        if self.dx:
35            y = self.dx.maxValue()
36        return max(self.v, x, y)
37
38    def printNode(self):
39        if self is None:
40            return
41        print(self.v)
42        if self.sx:
43            self.sx.printNode()
44        if self.dx:
45            self.dx.printNode()
46    
47class binaryTree:
48    def __init__(self, Node=None):
49        self.Node = Node
50
51    def buildTree(self, numberOfNode, valueLimit):
52        if numberOfNode == 0: 
53            return None
54
55        node = Node(random.randint(1, valueLimit))
56        numberOfNode -= 1
57        if numberOfNode >= 0:
58            x = random.randint(0, numberOfNode)
59            node.sx = self.buildTree(x, valueLimit)
60            node.dx = self.buildTree(numberOfNode-x, valueLimit)
61
62        return node
queries leading to this page
tree data structures in pythonexample of tree in data structuretree data stproperties of binary tree in data structureall basics of tree data structuretree in pythontree data structure python librarytree data structurestree data structure class pythonbst tree in data structuredata structures and algorithms treestree data structuretree data structure c 2bpython tree librarywhen is a tree use in data structuretree data python tree data structure example pythoncomplete code for tree data structureis tree data structure hard 3ftree data structure exampleexample of tree data structuretree data structure programtree data structure pythontree data structures and algorithms pythonis tree a data structurebinary tree properties in data structurewhat type of data structure is tree 3ftree data strucutresyntax tree pythontree structure data structurebinary tree data structuretreenode py methods 28 29 pythontree data structure whydo python have treedata tree structureswhat is binary tree in data structuretree data structures typestree data structure in pythontree ds in pythoindata structure treetree data structure codedata structure used to implementavl treetree structure in data structure applicationsdata structures treesdisplay tree in pythonhow tree data structure is implementedtree types data structuretree definition in data structurehow to define a tree in pythongeneral tree in data structure pythintree in data structurecomponents of tree data structuretree list pythonwhat data structure does a tree set usehow to create tree in pythontree structure in pythonhow to deal with tree structure in pythonpython treeexample tree object pythonbinary tree data structurestree using pythonall tree in data structurewhat is tree data structuretree code in data structuretree is good example for which data structuretrees in data structuretree implementation in pythonis a binary tree a data structurebest way to implement tree data structures python tree diagrampython code base structure treedata structure treeswhen we use tree data structuretree data structure in c 2b 2bwhat is trees in data structurepython implement tree structurewhat defines a tree data structuretree datastructuredata structure b treetree data structurwhat is a data treedefinition of tree in data structuretree data structure propertiestree in data structure exampledata structure binary treepython tree implementationhow do you define tree in data structurehow t represent a tree in pythonis a tree python built in data structureproperties of tree in data structurematplotlib treewhat is the tree data structuretree structure in databasetrees data structuretree data structure well explainedpython create tree data structurewhy use tree data structuretree data structure programsdefine the tree data structure data treetree in data structure 5ctree representation in pytonhtree structures in pythonhow to display tree structure in pythonbinary tree data structure pythondata structures for treestree pythonypython treedata structures treetree python packagetree data structure functionstree data structure python display tree structureexamples of tree in data structuretree data structure tutorialtree structuires in pythonwhat is a binary tree in data structureis binary tree a data structuretree structure pythonpython implementation of treepython tree structure librarytree data structure detailstree sitter pythonlist tree pythona tree is a data structurewhy we use tree in data structureunderstand tree data structure basicstreee data structureswhat are trees in data structurewhat is tree in data structurewhat is the tree in data structuretree data structure porblemspython built in tree data structureuse of tree data structuretrees in pythonwhy tree data structure is usedwhat is a tree data structuretree python codetree data structure diagramtree dat structuretree uses which data structuretree python data structuredata structure and algorithm tree and graphpython tree data structure examplepython tree structuretree inpythontrees data structureswhen was the data tree structure invented 3ffull tree in data structuredefine tree data structurehow to make a tree in pythontree class in pythontree function pythongeneral tree data structuretree based program in data structuretree applications in data structurebinary tree in data structuretrees data data structurehow to handle tree pythonwhich of the following use tree data structurecomplete tree in data structuretree dataswhy we use tree data structurewhat is full binary tree in data structureepyhton treetree data structure in databasedifferent trees in data structuretree type in data structuretree data structure definitiontrees in data structurestree structure in data structurecomplete explanation of tree in data structuretree in data structure usestrees pythondefine tree in data structurewhen to use a tree data structurebinary trees in data structurehow to see the tree structure in pythontree data structure algorithmtree definition data structurepython tree data structuredata tree structuretree representation in data structurewhere does data structure tree worktree types in data structureimplementation of tree data structuretree view pythonwhat is a tree in data structuretrees in pythontrees definition in data structurepython 2c tree implementationtree data structure in python