-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLTree.py
More file actions
188 lines (160 loc) · 4.9 KB
/
LLTree.py
File metadata and controls
188 lines (160 loc) · 4.9 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from queue import LLQueue
class TreeNode:
def __init__(self, data) -> None:
self.data = data
self.left = None
self.right = None
def __str__(self, level = 0):
ret = " " * level + str(self.data) + '\n'
if self.left:
ret += self.left.__str__(level + 1)
if self.right:
ret += self.right.__str__(level + 1)
return ret
# preOrder traversal:
def preOrderTraversal(rootNode):
if not rootNode:
return
print(rootNode.data)
preOrderTraversal(rootNode.left)
preOrderTraversal(rootNode.right)
# preOrderTraversal(tree)
def inOrderTraversal(rootNode):
if not rootNode:
return
inOrderTraversal(rootNode.left)
print(rootNode.data)
inOrderTraversal(rootNode.right)
# inOrderTraversal(tree)
def postOrderTraversal(rootNode):
if not rootNode:
return
postOrderTraversal(rootNode.left)
postOrderTraversal(rootNode.right)
print(rootNode.data)
# postOrderTraversal(tree)
def levelOrderTraversal(rootNode):
if not rootNode:
return
else:
queue = LLQueue()
queue.enQueue(rootNode)
while not queue.isEmpty():
current = queue.deQueue()
print(current.data)
if current.left:
queue.enQueue(current.left)
if current.right:
queue.enQueue(current.right)
# levelOrderTraversal(tree)
# searching Binary Tree
def searchBT(rootNode, nodeValue):
if not rootNode:
return
else:
queue = LLQueue()
queue.enQueue(rootNode)
while not queue.isEmpty():
current = queue.deQueue()
if current.data == nodeValue:
return 'Success!'
if current.left:
queue.enQueue(current.left)
if current.right:
queue.enQueue(current.right)
return 'Node Not Found!'
# insertion a newNode to the binary tree
def insertNewNode(rootNode, nodeValue):
newNode = TreeNode(nodeValue)
if not rootNode:
rootNode = newNode
return
else:
queue = LLQueue()
queue.enQueue(rootNode)
while not queue.isEmpty():
current = queue.deQueue()
if current.left:
queue.enQueue(current.left)
else:
current.left = newNode
return
if current.right:
queue.enQueue(current.right)
else:
current.right = newNode
return
tree = TreeNode('Drinks')
insertNewNode(tree, 'Juice')
insertNewNode(tree, 'Hot')
insertNewNode(tree, 'orange')
insertNewNode(tree, 'lemon')
insertNewNode(tree, 'coffee')
insertNewNode(tree, 'Tea')
levelOrderTraversal(tree)
# delete tree node.
# find and remove the deepest node.
def findDeepestNode(rootNode):
if not rootNode:
return 'the tree does not exits!'
else:
queue = LLQueue()
queue.enQueue(rootNode)
while not queue.isEmpty():
current = queue.deQueue()
if current.left:
queue.enQueue(current.left)
if current.right:
queue.enQueue(current.right)
return current.data
def removeDeepestNode(rootNode, dNode):
if not rootNode:
return 'the tree does not exits!'
else:
queue = LLQueue()
queue.enQueue(rootNode)
while not queue.isEmpty():
current = queue.deQueue()
if current.left:
if current.left.data == dNode:
current.left.data = None
current.left = None
return 'removed'
else:
queue.enQueue(current.left)
if current.right:
if current.right.data == dNode:
current.right.data = None
current.right = None
return 'removed'
else:
queue.enQueue(current.right)
return 'was not removed'
def deleteNode(rootNode, nodeValue):
if not rootNode:
return 'There is no tree!'
else:
queue = LLQueue()
queue.enQueue(rootNode)
while not queue.isEmpty():
current = queue.deQueue()
if current.data == nodeValue:
deepestNode = findDeepestNode(tree)
removeDeepestNode(tree, deepestNode)
current.data = deepestNode
return 'Node was deleted!'
if current.left:
queue.enQueue(current.left)
if current.right:
queue.enQueue(current.right)
return 'Tree Node was not found!'
# deleteNode(tree, 'Drinks')
# delete entire node.
def deleteBT(rootNode):
if not rootNode:
return 'There is no tree!'
else:
rootNode.left = None
rootNode.right = None
rootNode.data= None
# deleteBT(tree)