-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreePL.py
More file actions
77 lines (59 loc) · 2.23 KB
/
Copy pathBinaryTreePL.py
File metadata and controls
77 lines (59 loc) · 2.23 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
# Created by Roshan Jayswal
# Copyright © 2021. All rights reserved.
class BinaryTree:
def __init__(self, size):
self.customList = size * [None]
self.lastUsedIndex = 0
self.maxSize = size
def insertNode(self, value):
if self.lastUsedIndex + 1 == self.maxSize:
return "The Binary Tree is full"
self.customList[self.lastUsedIndex+1] = value
self.lastUsedIndex += 1
return "The value has been successfully inserted"
def searchNode(self, nodeValue):
for i in range(len(self.customList)):
if self.customList[i] == nodeValue:
return "Success"
return "Not found"
def preOrderTraversal(self, index):
if index > self.lastUsedIndex:
return
print(self.customList[index])
self.preOrderTraversal(index*2)
self.preOrderTraversal(index*2 + 1)
def inOrderTraversal(self, index):
if index > self.lastUsedIndex:
return
self.inOrderTraversal(index*2)
print(self.customList[index])
self.inOrderTraversal(index*2+1)
def postOrderTraversal(self, index):
if index > self.lastUsedIndex:
return
self.postOrderTraversal(index*2)
self.postOrderTraversal(index*2+1)
print(self.customList[index])
def levelOrderTraversal(self, index):
for i in range(index, self.lastUsedIndex+1):
print(self.customList[i])
def deleteNode(self, value):
if self.lastUsedIndex == 0:
return "There is not any node to delete"
for i in range(1, self.lastUsedIndex+1):
if self.customList[i] == value:
self.customList[i] = self.customList[self.lastUsedIndex]
self.customList[self.lastUsedIndex] = None
self.lastUsedIndex -= 1
return "The node has been successfully deleted"
def deleteBT(self):
self.customList = None
return "The BT has been successfully deleted"
newBT = BinaryTree(8)
newBT.insertNode("Drinks")
newBT.insertNode("Hot")
newBT.insertNode("Cold")
newBT.insertNode("Tea")
newBT.insertNode("Coffee")
print(newBT.deleteBT())
newBT.levelOrderTraversal(1)