-
Notifications
You must be signed in to change notification settings - Fork 2
/
BinarySearchTree.py
136 lines (116 loc) · 3.35 KB
/
BinarySearchTree.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
128
129
130
131
132
133
134
import Queue
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def insert(self, data):
if data < self.data:
if self.left == None:
self.left = Node(data)
else:
self.left.insert(data)
else:
if self.right == None:
self.right = Node(data)
else:
self.right.insert(data)
def find(self, data, parent=None):
if data == self.data:
return self, parent
if data < self.data:
if self.left == None:
return None, parent
else:
return self.left.find(data, self)
else:
if self.right == None:
return None, parent
else:
return self.right.find(data, self)
def delete(self, data):
node, parent = self.find(data)
if not node == None:
numChildren = node.numberOfChildren()
if numChildren == 0:
if parent.left is node:
parent.left = None
else:
parent.right = None
del node
elif numChildren == 1:
if node.left:
n = node.left
else:
n = node.right
if parent.left is node:
parent.left = n
else:
parent.right = n
del node
else:
parent = node
successor = parent.right
while successor.left:
succesor = sucessor.left
node = successor
parent.data = successor.data
if successor.right:
node.left = successor.right
del successor
def numberOfChildren(self):
if self.left == None:
if self.right == None:
return 0
else:
return 1
else:
if self.right == None:
return 1
else:
return 2
def printInOrder(self):
if self.left:
self.left.printInOrder()
print self.data
if self.right:
self.right.printInOrder()
def printPreOrder(self):
print self.data
if self.left:
self.left.printPreOrder()
if self.right:
self.right.printPreOrder()
def printPostOrder(self):
if self.left:
self.left.printPostOrder()
if self.right:
self.right.printPostOrder()
print self.data
def printBreadthFirst(self):
q = Queue.Queue()
q.put(self)
while not q.empty():
node = q.get()
print node.data
if node.left:
q.put(node.left)
if node.right:
q.put(node.right)
root = Node(8)
root.insert(11)
root.insert(6)
root.insert(9)
root.insert(3)
root.insert(10)
root.insert(7)
root.insert(13)
node, parent = root.find(10)
root.delete(10)
root.printInOrder()
print 'Pre Order'
root.printPreOrder()
print 'Post Order'
root.printPostOrder()
print 'Breadth First'
root.printBreadthFirst()