1
- '''
1
+ """
2
2
A binary search Tree
3
- '''
3
+ """
4
+
5
+
4
6
class Node :
5
7
def __init__ (self , value , parent ):
6
8
self .value = value
@@ -13,16 +15,11 @@ def __repr__(self):
13
15
14
16
if self .left is None and self .right is None :
15
17
return str (self .value )
16
- return pformat (
17
- {
18
- "%s"
19
- % (self .value ): (self .left , self .right )
20
- },
21
- indent = 1 ,
22
- )
18
+ return pformat ({"%s" % (self .value ): (self .left , self .right )}, indent = 1 ,)
19
+
23
20
24
21
class BinarySearchTree :
25
- def __init__ (self , root = None ):
22
+ def __init__ (self , root = None ):
26
23
self .root = root
27
24
28
25
def __str__ (self ):
@@ -32,10 +29,10 @@ def __str__(self):
32
29
return str (self .root )
33
30
34
31
def __reassign_nodes (self , node , newChildren ):
35
- if ( newChildren is not None ) : # reset its kids
32
+ if newChildren is not None : # reset its kids
36
33
newChildren .parent = node .parent
37
- if ( node .parent is not None ) : # reset its parent
38
- if ( self .is_right (node ) ): # If it is the right children
34
+ if node .parent is not None : # reset its parent
35
+ if self .is_right (node ): # If it is the right children
39
36
node .parent .right = newChildren
40
37
else :
41
38
node .parent .left = newChildren
@@ -55,10 +52,10 @@ def __insert(self, value):
55
52
new_node = Node (value , None ) # create a new Node
56
53
if self .empty (): # if Tree is empty
57
54
self .root = new_node # set its root
58
- else : # Tree is not empty
59
- parent_node = self .root # from root
55
+ else : # Tree is not empty
56
+ parent_node = self .root # from root
60
57
while True : # While we don't get to a leaf
61
- if value < parent_node .value : # We go left
58
+ if value < parent_node .value : # We go left
62
59
if parent_node .left == None :
63
60
parent_node .left = new_node # We insert the new node in a leaf
64
61
break
@@ -87,60 +84,65 @@ def search(self, value):
87
84
node = node .left if value < node .value else node .right
88
85
return node
89
86
90
- def get_max (self , node = None ):
87
+ def get_max (self , node = None ):
91
88
"""
92
89
We go deep on the right branch
93
90
"""
94
91
if node is None :
95
92
node = self .root
96
93
if not self .empty ():
97
- while ( node .right is not None ) :
94
+ while node .right is not None :
98
95
node = node .right
99
96
return node
100
97
101
- def get_min (self , node = None ):
98
+ def get_min (self , node = None ):
102
99
"""
103
100
We go deep on the left branch
104
101
"""
105
- if ( node is None ) :
102
+ if node is None :
106
103
node = self .root
107
- if ( not self .empty () ):
104
+ if not self .empty ():
108
105
node = self .root
109
- while ( node .left is not None ) :
106
+ while node .left is not None :
110
107
node = node .left
111
108
return node
112
109
113
110
def remove (self , value ):
114
- node = self .search (value ) # Look for the node with that label
115
- if ( node is not None ) :
116
- if ( node .left is None and node .right is None ): # If it has no children
111
+ node = self .search (value ) # Look for the node with that label
112
+ if node is not None :
113
+ if node .left is None and node .right is None : # If it has no children
117
114
self .__reassign_nodes (node , None )
118
115
node = None
119
- elif ( node .left is None ): # Has only right children
116
+ elif node .left is None : # Has only right children
120
117
self .__reassign_nodes (node , node .right )
121
- elif ( node .right is None ): # Has only left children
118
+ elif node .right is None : # Has only left children
122
119
self .__reassign_nodes (node , node .left )
123
120
else :
124
- tmpNode = self .get_max (node .left ) # Gets the max value of the left branch
121
+ tmpNode = self .get_max (
122
+ node .left
123
+ ) # Gets the max value of the left branch
125
124
self .remove (tmpNode .value )
126
- node .value = tmpNode .value # Assigns the value to the node to delete and keesp tree structure
127
-
125
+ node .value = (
126
+ tmpNode .value
127
+ ) # Assigns the value to the node to delete and keesp tree structure
128
+
128
129
def preorder_traverse (self , node ):
129
130
if node is not None :
130
131
yield node # Preorder Traversal
131
132
yield from self .preorder_traverse (node .left )
132
133
yield from self .preorder_traverse (node .right )
133
134
134
- def traversal_tree (self , traversalFunction = None ):
135
+ def traversal_tree (self , traversalFunction = None ):
135
136
"""
136
137
This function traversal the tree.
137
138
You can pass a function to traversal the tree as needed by client code
138
139
"""
139
- if ( traversalFunction is None ) :
140
+ if traversalFunction is None :
140
141
return self .preorder_traverse (self .root )
141
142
else :
142
143
return traversalFunction (self .root )
143
144
145
+
144
146
def postorder (curr_node ):
145
147
"""
146
148
postOrder (left, right, self)
@@ -150,8 +152,9 @@ def postorder(curr_node):
150
152
nodeList = postorder (curr_node .left ) + postorder (curr_node .right ) + [curr_node ]
151
153
return nodeList
152
154
155
+
153
156
def binary_search_tree ():
154
- r'''
157
+ r"""
155
158
Example
156
159
8
157
160
/ \
@@ -170,36 +173,38 @@ def binary_search_tree():
170
173
Traceback (most recent call last):
171
174
...
172
175
IndexError: Warning: Tree is empty! please use another.
173
- '''
176
+ """
174
177
testlist = (8 , 3 , 6 , 1 , 10 , 14 , 13 , 4 , 7 )
175
178
t = BinarySearchTree ()
176
179
for i in testlist :
177
180
t .insert (i )
178
181
179
182
# Prints all the elements of the list in order traversal
180
183
print (t )
181
-
182
- if ( t .search (6 ) is not None ) :
184
+
185
+ if t .search (6 ) is not None :
183
186
print ("The value 6 exists" )
184
187
else :
185
188
print ("The value 6 doesn't exist" )
186
189
187
- if ( t .search (- 1 ) is not None ) :
190
+ if t .search (- 1 ) is not None :
188
191
print ("The value -1 exists" )
189
192
else :
190
193
print ("The value -1 doesn't exist" )
191
194
192
- if ( not t .empty () ):
195
+ if not t .empty ():
193
196
print ("Max Value: " , t .get_max ().value )
194
197
print ("Min Value: " , t .get_min ().value )
195
198
196
199
for i in testlist :
197
200
t .remove (i )
198
201
print (t )
199
202
203
+
200
204
二叉搜索树 = binary_search_tree
201
205
202
206
if __name__ == "__main__" :
203
207
import doctest
208
+
204
209
doctest .testmod ()
205
210
binary_search_tree ()
0 commit comments