-
Notifications
You must be signed in to change notification settings - Fork 13
/
BinaryTreePL.py
74 lines (63 loc) · 2.79 KB
/
BinaryTreePL.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
class BinaryTree: # O(1) time & O(N) space.
def __init__(self, size):
self.customList = size * [None]
self.maxsize = size
self.lastUsedIdx = 0
def insertNode(self, value): # O(1) time & O(1) space.
if self.lastUsedIdx + 1 == self.maxsize:
return "The Binary Tree is full."
self.customList[self.lastUsedIdx + 1] = value
self.lastUsedIdx += 1
return "The node has been successfully inserted."
def searchNode(self, nodevalue): # O(N) time & O(1) space.
for i in range (len(self.customList)):
if self.customList[i] == nodevalue:
return "Node found!"
else:
return "Node not found!"
'''TRAVERSAL (ALL KINDS) -> TIME - O(N),SPACE- O(1)'''
def preOrderTraversal(self, idx): # O(N) time & O(N) space.
if idx > self.lastUsedIdx:
return
print(self.customList[idx]) #root
self.preOrderTraversal(idx*2) #left
self.preOrderTraversal(idx*2 + 1) #right
def inOrderTraversal(self,idx): # O(N) time & O(N) space.
if idx > self.lastUsedIdx:
return
self.inOrderTraversal(idx*2) #left
print(self.customList[idx]) #root
self.inOrderTraversal(idx*2 + 1) #right
def postOrderTraversal(self,idx): # O(N) time & O(N) space.
if idx > self.lastUsedIdx:
return
self.postOrderTraversal(idx*2) #left
self.postOrderTraversal(idx*2 + 1) #right
print(self.customList[idx]) #root
def levelOrderTraversal(self,idx): # O(N) time & O(N) space.
for i in range(idx, self.lastUsedIdx+1):
print(self.customList[i])
def deleteNode(self, value): # O(N) time & O(N) space.
if self.lastUsedIdx == 0:
return "No node found to be deleted."
# a)find the value we want to delete
#b) replace that value with the value of deepest node(here, deepest node =lastindex)
#c) finally delete the deepest node from the binary tree
for i in range(1, self.lastUsedIdx+1):
if self.customList[i] == value:
self.customList[i] = self.customList[self.lastUsedIdx]
self.customList[self.lastUsedIdx] = None
self.lastUsedIdx -= 1
return "The node has been successfully deleted."
def deleteBT(self): # O(1) time & O(1) space.
self.customList = None
return "The binary tree has been deleted!"
newBT = BinaryTree(8)
print(newBT.insertNode("Drinks"))
print(newBT.insertNode("Hot"))
print(newBT.insertNode("Cold"))
print(newBT.searchNode("Tea"))
newBT.preOrderTraversal(1)
newBT.inOrderTraversal(1)
newBT.postOrderTraversal(1)
newBT.levelOrderTraversal(1)