-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinTreeLL.py
149 lines (135 loc) · 5.04 KB
/
BinTreeLL.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import QueueLL as q
class TreeNode:
def __init__(self, data):
self.data = data
self.left_child = None
self.right_child = None
def pre_order(self):
print(self.data)
if self.left_child:
self.left_child.pre_order()
if self.right_child:
self.right_child.pre_order()
def in_order(self):
if self.left_child:
self.left_child.in_order()
print(self.data)
if self.right_child:
self.right_child.in_order()
def post_order(self):
if self.left_child:
self.left_child.post_order()
if self.right_child:
self.right_child.post_order()
print(self.data)
def level_order(self):
order = q.Queue()
order.enqueue(self) #self(TreeNode) gets enqueued inside Queue node
while not(order.isEmpty()):
node = order.dequeue().data #order.dequeue() returns Queue node
print(str(node.data)) #node.data stores TreeNode; we print TreeNode.data
if node.left_child:
order.enqueue(node.left_child)
if node.right_child:
order.enqueue(node.right_child)
def search(self, item):
order = q.Queue()
order.enqueue(self)
while not(order.isEmpty()):
node = order.dequeue().data
if node.data == item:
return f'{item} found'
if node.left_child:
order.enqueue(node.left_child)
if node.right_child:
order.enqueue(node.right_child)
return 'Not found'
def insert(self, item):
'''create an item(string) node and insert it in tree'''
item_node = TreeNode(item)
order = q.Queue()
order.enqueue(self)
while not(order.isEmpty()):
node = order.dequeue().data
if not(node.left_child):
node.left_child = item_node
return f'{item} inserted!!!'
else:
order.enqueue(node.left_child)
if not(node.right_child):
node.right_child = item_node
return f'{item} inserted!!!'
else:
order.enqueue(node.right_child)
def get_deepest_node(self):
'''return the deepest node in TreeNode (self)'''
order = q.Queue()
order.enqueue(self)
while not(order.isEmpty()):
node = order.dequeue().data
if node.left_child:
order.enqueue(node.left_child)
if node.right_child:
order.enqueue(node.right_child)
return node
def delete_deepest_node(self):
deepest_node = self.get_deepest_node()
order = q.Queue()
order.enqueue(self)
while not(order.isEmpty()):
node = order.dequeue().data
if node is deepest_node:
node = None
return
if node.right_child:
if node.right_child is deepest_node:
node.right_child = None
else:
order.enqueue(node.right_child)
if node.left_child:
if node.left_child is deepest_node:
node.left_child = None
else:
order.enqueue(node.left_child)
def delete_node(self, node_data):
'''delete node_data(string) from a binary tree(self)'''
deepest_node_data = self.get_deepest_node().data #get data of deepest node
order = q.Queue()
order.enqueue(self)
while not(order.isEmpty()):
node = order.dequeue().data
if node.data == node_data: #if current node's data matches with data of node to be deleted
node.data = deepest_node_data #replace current node's data with that of deepest node
self.delete_deepest_node() #delete deepest node
return f'{node_data} successfully deleted'
if node.left_child:
order.enqueue(node.left_child)
if node.right_child: #right child will be present iff left child exists
order.enqueue(node.right_child)
return f'{node_data} could not be deleted'
def delete_tree(self):
'''Delete the entire tree'''
self.data = None #root node = None
if self.left_child:
self.left_child = None #left child of root node = None
if self.right_child:
self.right_child = None #right child of root node = None
return 'Tree Successfully deleted'
drinks = TreeNode("Drinks")
hot = TreeNode("Hot")
cold = TreeNode("Cold")
drinks.left_child = hot
drinks.right_child = cold
tea = TreeNode("Tea")
coffee = TreeNode("coffee")
hot.left_child = tea
hot.right_child = coffee
cola = TreeNode("Cola")
fanta = TreeNode("Fanta")
cold.left_child = cola
cold.right_child = fanta
drinks.level_order()
drinks.insert('Milk Tea')
drinks.insert('Green Tea')
drinks.in_order()
print(drinks.get_deepest_node().data)