- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTreeList.py
More file actions
Latest commit
74 lines (54 loc) · 2.1 KB
/
Copy pathbinaryTreeList.py
File metadata and controls
74 lines (54 loc) · 2.1 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
# Binary Tree implementation using List
# Create Traverse Search Insert Delete
# Binary tree has always at most 2 children
# Creating is O(n), rest is O(1)
# Linked list Insert is O(n) but also spcae complexity is O(n)
classBinaryTree:
def__init__(self, size):
self.customList=size* [None]
self.last_used_index=0
self.capacity=size
definsertNode(self, value):
ifself.last_used_index+1==self.capacity:
return"Binary Tree is full"
self.custom_list[self.last_used_index+1] =value
self.last_used_index+=1
return"Inserted"
defsearchNode(self, nodeValue):
foriinrange(len(self.custom_list)):
ifself.custom_list[i] ==nodeValue:
return"Success"
return"Not found"
defpreOrderTraversal(self, index):
ifindex>self.last_used_index:
return
print(self.custom_list[index])
self.preOrderTraversal(index*2)
self.preOrderTraversal(index*2+1)
definOrderTraversal(self, index):
ifindex>self.last_used_index:
return
self.inOrderTraversal(index*2)
print(self.custom_list[index])
self.inOrderTraversal(index*2+1)
defpostOrderTraversal(self, index):
ifindex>self.last_used_index:
return
self.postOrderTraversal(index*2)
self.postOrderTraversal(index*2+1)
print(self.custom_list[index])
deflevelOrderTraversal(self, index):
foriinrange(index, self.last_used_index+1):
print(self.custom_list[i])
defdeleteNode(self, value):
ifself.last_used_index==0:
return"There is not any node to delete"
foriinrange(1, self.last_used_index+1):
ifself.custom_list[i] ==value:
self.custom_list[i] =self.custom_list[self.last_used_index]
self.custom_list[self.last_used_index] =None
self.last_used_index-=1
return"Deleted"
defdeleteBinaryTree(self):
self.custom_list=None
return"Deleted"