tree python

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

showing results for - "tree python"
Ilyes
31 Nov 2017
1# pypi, the Python Package Index, suggests tinytree, treedict, caxes, 
2# treelib, pyavl... these are just the top few after filtering away the 
3# many accidental hits (which point to specific tree such as XML ones, 
4#                 AST ones, etc, etc;-). If you clarify what you want 
5# to do with your trees it may be easier to suggest a specific package.
6
7pip install treelib pyavl tinytree treedict caxes
8# or 
9pip3 install treelib pyavl tinytree treedict caxes
Maelie
14 Feb 2018
1import turtle
2arrow=turtle.Turtle()
3arrow.color ("green")
4window=turtle.Screen()
5window.bgcolor("cyan")
6arrow.begin_fill()
7arrow.forward(100)
8arrow.setheading(120)
9arrow.forward(100)
10arrow.setheading(240)
11arrow.forward(100)
12arrow.setheading(270)
13arrow.end_fill()
14arrow.penup()
15arrow.forward(60)
16arrow.pendown()
17arrow.begin_fill()
18arrow.setheading(0)
19arrow.forward(100)
20arrow.setheading(120)
21arrow.forward(100)
22arrow.setheading(240)
23arrow.forward(100)
24arrow.setheading(270)
25arrow.end_fill()
26arrow.penup()
27arrow.setheading(0)
28arrow.forward(40)
29arrow.pendown()
30arrow.begin_fill()
31arrow.color("brown")
32arrow.setheading(270)
33arrow.forward(50)
34arrow.setheading(0)
35arrow.forward(20)
36arrow.setheading(90)
37arrow.forward(50)
38arrow.end_fill()
39arrow.penup()
40arrow.forward(150)
41arrow.setheading(180)
42arrow.forward (125)
43arrow.pendown()
44arrow.color ("green")
45arrow.begin_fill()
46arrow.setheading(120)
47arrow.forward(100)
48arrow.setheading(240)
49arrow.forward(100)
50arrow.setheading(270)
51arrow.end_fill()
52arrow.penup()
53arrow.forward(60)
54arrow.pendown()
55arrow.begin_fill()
56arrow.setheading(0)
57arrow.forward(100)
58arrow.setheading(120)
59arrow.forward(100)
60arrow.setheading(240)
61arrow.forward(100)
62arrow.setheading(270)
63arrow.end_fill()
64arrow.penup()
65arrow.setheading(0)
66arrow.forward(40)
67arrow.pendown()
68arrow.begin_fill()
69arrow.color("brown")
70arrow.setheading(270)
71arrow.forward(50)
72arrow.setheading(0)
73arrow.forward(20)
74arrow.setheading(90)
75arrow.forward(50)
76arrow.end_fill()
77arrow.penup()
78arrow.forward(30)
79arrow.color("green")
80turtle.done
Christian
16 Sep 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
Jason
21 Sep 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
similar questions
queries leading to this page
tree python