Skip to content

Commit e308b63

Browse files
committed
Update Binary Tree
1 parent b88465a commit e308b63

File tree

2 files changed

+77
-112
lines changed

2 files changed

+77
-112
lines changed
Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,34 @@
1-
class NodeTree:
2-
1+
class BinaryTree:
32
def __init__(self, data):
43
self.data = data
5-
self.children = []
6-
self.parent = None
7-
8-
def add_child(self, new_node):
9-
if type(new_node) != NodeTree:
10-
new_node = NodeTree(new_node)
11-
self.children.append(new_node)
12-
new_node.parent = self
13-
14-
15-
def get_level(self):
16-
p = self.parent
17-
level = 0
18-
while p:
19-
level += 1
20-
p = p.parent
21-
return level
22-
23-
def print_tree(self):
24-
space = ' ' * self.get_level()
25-
prefix = space + "|--" if self.parent else "|--"
26-
print(prefix , self.data)
27-
for child in self.children:
28-
child.print_tree()
29-
30-
def height(self):
31-
if len(self.children) == 0:
32-
return 0
33-
else:
34-
max_height = -1
35-
for child in self.children:
36-
max_height = max(max_height, child.height())
37-
return max_height + 1
38-
39-
40-
def search(self, key):
4+
self.left_child = None
5+
self.right_child = None
416

42-
# found
43-
if self.data == key:
44-
return True
7+
def add_child(self, data):
8+
if type(data) != BinaryTree:
9+
data = BinaryTree(data)
4510

46-
# not found
47-
else:
48-
for child in self.children:
49-
found = child.search(key)
50-
if found:
51-
return True
52-
53-
return False
11+
if self.left_child == None:
12+
self.left_child = data
5413

14+
elif self.right_child == None:
15+
self.right_child = data
5516

56-
def isbinary(self):
57-
if len(self.children) > 2:
58-
return False
59-
elif len(self.children) == 0:
60-
return True
6117
else:
62-
for child in self.children:
63-
if not child.isbinary():
64-
return False
65-
66-
return True
67-
68-
18+
self.left_child.add_child(data)
19+
20+
6921
if __name__ == "__main__":
22+
root = BinaryTree(1)
23+
root.add_child(5)
24+
root.add_child(12)
25+
root.add_child(3)
26+
root.add_child(6)
27+
7028

71-
ali = NodeTree("Ali")
72-
ali.add_child("Reza")
73-
ali.add_child("Hamed")
74-
ali.children[0].add_child("Arad")
75-
ali.children[0].add_child("Aras")
29+
print(root.left_child.left_child.data)
7630

77-
print(ali.print_tree())
78-
print(ali.isbinary())
79-
print(ali.search("Ebrahim"))
31+
32+
33+
34+
Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
2-
31
class NodeTree:
2+
43
def __init__(self, data):
54
self.data = data
65
self.children = []
76
self.parent = None
87

9-
def add_node(self, node):
10-
self.children.append(node)
11-
node.parent = self
8+
def add_child(self, new_node):
9+
if type(new_node) != NodeTree:
10+
new_node = NodeTree(new_node)
11+
self.children.append(new_node)
12+
new_node.parent = self
1213

13-
def print_tree(self):
1414

15-
space = "\t"
16-
print(space * self.get_level(), self.data)
17-
for child in self.children:
18-
child.print_tree()
19-
2015
def get_level(self):
2116
p = self.parent
22-
lvl = 1
17+
level = 0
2318
while p:
24-
lvl +=1
19+
level += 1
2520
p = p.parent
26-
return lvl
21+
return level
2722

23+
def print_tree(self):
24+
space = ' ' * self.get_level()
25+
prefix = space + "|--" if self.parent else "|--"
26+
print(prefix , self.data)
27+
for child in self.children:
28+
child.print_tree()
29+
2830
def height(self):
2931
if len(self.children) == 0:
3032
return 0
@@ -33,37 +35,45 @@ def height(self):
3335
for child in self.children:
3436
max_height = max(max_height, child.height())
3537
return max_height + 1
38+
39+
40+
def search(self, key):
41+
42+
# found
43+
if self.data == key:
44+
return True
45+
46+
# not found
47+
else:
48+
for child in self.children:
49+
found = child.search(key)
50+
if found:
51+
return True
52+
53+
return False
54+
55+
56+
def isbinary(self):
57+
if len(self.children) > 2:
58+
return False
59+
elif len(self.children) == 0:
60+
return True
61+
else:
62+
for child in self.children:
63+
if not child.isbinary():
64+
return False
65+
66+
return True
3667

3768

3869
if __name__ == "__main__":
3970

40-
# Create a Node (root)
41-
root = NodeTree("Electronics")
42-
43-
# Create Children for root
44-
## first child
45-
laptop = NodeTree("Laptop")
46-
laptop.add_node(NodeTree("Acer"))
47-
laptop.add_node(NodeTree("Asus"))
48-
laptop.add_node(NodeTree("Mac"))
49-
50-
root.add_node(laptop)
51-
52-
## second child
53-
phone = NodeTree("Phone")
54-
phone.add_node(NodeTree("iPhone"))
55-
phone.add_node(NodeTree("Samsung"))
56-
phone.add_node(NodeTree("Xiaomi"))
57-
58-
root.add_node(phone)
59-
60-
## third child
61-
tv = NodeTree("TV")
62-
tv.add_node(NodeTree("Goldiran"))
63-
tv.add_node(NodeTree("LG"))
64-
tv.add_node(NodeTree("SONY"))
65-
66-
root.add_node(tv)
71+
ali = NodeTree("Ali")
72+
ali.add_child("Reza")
73+
ali.add_child("Hamed")
74+
ali.children[0].add_child("Arad")
75+
ali.children[0].add_child("Aras")
6776

68-
root.print_tree()
69-
print(root.height())
77+
print(ali.print_tree())
78+
print(ali.isbinary())
79+
print(ali.search("Ebrahim"))

0 commit comments

Comments
 (0)