-
Notifications
You must be signed in to change notification settings - Fork 1
/
PoC2_tree.py
112 lines (87 loc) · 2.84 KB
/
PoC2_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
Python definition of basic Tree class
IMPORTANT: Some class methods assume that instances of the Tree class
always have a single parent (or no parent for the root). See problem #8
on homework #3 for more details.
"""
class Tree:
"""
Recursive definition for trees plus various tree methods
"""
def __init__(self, value, children):
"""
Create a tree whose root has specific value (a string)
Children is a list of references to the roots of the subtrees.
"""
self._value = value
self._children = children
def __str__(self):
"""
Generate a string representation of the tree
Use an pre-order traversal of the tree
"""
ans = "["
ans += str(self._value)
for child in self._children:
ans += ", "
ans += str(child)
return ans + "]"
def get_value(self):
"""
Getter for node's value
"""
return self._value
def children(self):
"""
Generator to return children
"""
for child in self._children:
yield child
def num_nodes(self):
"""
Compute number of nodes in the tree
"""
ans = 1
for child in self._children:
ans += child.num_nodes()
return ans
def num_leaves(self):
"""
Count number of leaves in tree
"""
if len(self._children) == 0:
return 1
ans = 0
for child in self._children:
ans += child.num_leaves()
return ans
def height(self):
"""
Compute height of a tree rooted by self
"""
height = 0
for child in self._children:
height = max(height, child.height() + 1)
return height
def run_examples():
"""
Create some trees and apply various methods to these trees
"""
tree_a = Tree("a", [])
tree_b = Tree("b", [])
print "Tree consisting of single leaf node labelled 'a'", tree_a
print "Tree consisting of single leaf node labelled 'b'", tree_b
tree_cab = Tree("c", [tree_a, tree_b])
print "Tree consisting of three node", tree_cab
tree_dcabe = Tree("d", [tree_cab, Tree("e", [])])
print "Tree consisting of five nodes", tree_dcabe
print
my_tree = Tree("a", [Tree("b", [Tree("c", []), Tree("d", [])]),
Tree("e", [Tree("f", [Tree("g", [])]), Tree("h", []), Tree("i", [])])])
print "Tree with nine nodes", my_tree
print "The tree has", my_tree.num_nodes(), "nodes,",
print my_tree.num_leaves(), "leaves and height",
print my_tree.height()
import poc_draw_tree
poc_draw_tree.TreeDisplay(my_tree)
run_examples()