-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.py
128 lines (108 loc) · 3.56 KB
/
BST.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
# Created by Elshad Karimov
# Copyright © 2020 AppMillers. All rights reserved.
import QueueLinkedList as queue
class BSTNode:
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None
def insertNode(rootNode, nodeValue):
if rootNode.data == None:
rootNode.data = nodeValue
elif nodeValue <= rootNode.data:
if rootNode.leftChild is None:
rootNode.leftChild = BSTNode(nodeValue)
else:
insertNode(rootNode.leftChild, nodeValue)
else:
if rootNode.rightChild is None:
rootNode.rightChild = BSTNode(nodeValue)
else:
insertNode(rootNode.rightChild, nodeValue)
return "The node has been successfully inserted"
def preOrderTraversal(rootNode):
if not rootNode:
return
print(rootNode.data)
preOrderTraversal(rootNode.leftChild)
preOrderTraversal(rootNode.rightChild)
def inOrderTraversal(rootNode):
if not rootNode:
return
inOrderTraversal(rootNode.leftChild)
print(rootNode.data)
inOrderTraversal(rootNode.rightChild)
def postOrderTraversal(rootNode):
if not rootNode:
return
postOrderTraversal(rootNode.leftChild)
postOrderTraversal(rootNode.rightChild)
print(rootNode.data)
def levelOrderTraversal(rootNode):
if not rootNode:
return
else:
customQueue = queue.Queue()
customQueue.enqueue(rootNode)
while not(customQueue.isEmpty()):
root = customQueue.dequeue()
print(root.value.data)
if root.value.leftChild is not None:
customQueue.enqueue(root.value.leftChild)
if root.value.rightChild is not None:
customQueue.enqueue(root.value.rightChild)
def searchNode(rootNode, nodeValue):
if rootNode.data == nodeValue:
print("The value is found")
elif nodeValue < rootNode.data:
if rootNode.leftChild.data == nodeValue:
print("The value is found")
else:
searchNode(rootNode.leftChild, nodeValue)
else:
if rootNode.rightChild.data == nodeValue:
print("The value is found")
else:
searchNode(rootNode.rightChild, nodeValue)
def minValueNode(bstNode):
current = bstNode
while (current.leftChild is not None):
current = current.leftChild
return current
def deleteNode(rootNode, nodeValue):
if rootNode is None:
return rootNode
if nodeValue < rootNode.data:
rootNode.leftChild = deleteNode(rootNode.leftChild, nodeValue)
elif nodeValue > rootNode.data:
rootNode.rightChild = deleteNode(rootNode.rightChild, nodeValue)
else:
if rootNode.leftChild is None:
temp = rootNode.rightChild
rootNode = None
return temp
if rootNode.rightChild is None:
temp = rootNode.leftChild
rootNode = None
return temp
temp = minValueNode(rootNode.rightChild)
rootNode.data = temp.data
rootNode.rightChild = deleteNode(rootNode.rightChild, temp.data)
return rootNode
def deleteBST(rootNode):
rootNode.data = None
rootNode.leftChild = None
rootNode.rightChild = None
return "The BST has been successfully deleted"
newBST = BSTNode(None)
insertNode(newBST, 70)
insertNode(newBST,50)
insertNode(newBST,90)
insertNode(newBST, 30)
insertNode(newBST,60)
insertNode(newBST,80)
insertNode(newBST,100)
insertNode(newBST,20)
insertNode(newBST,40)
print(deleteBST(newBST))
levelOrderTraversal(newBST)