-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertRecursive.py
More file actions
39 lines (33 loc) · 973 Bytes
/
InsertRecursive.py
File metadata and controls
39 lines (33 loc) · 973 Bytes
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
class _Node:
__slots__ = '_element', '_left', '_right'
def __init__(self, element, left=None, right=None):
self._element = element
self._left = left
self._right = right
class BinarySearchTree:
def __init__(self):
self._root = None
def rinsert(self,troot,e):
if troot:
if e < troot._element:
troot._left = self.rinsert(troot._left,e)
elif e > troot._element:
troot._right = self.rinsert(troot._right,e)
else:
n = _Node(e)
troot = n
return troot
def inorder(self,troot):
if troot:
self.inorder(troot._left)
print(troot._element,end=' ')
self.inorder(troot._right)
B = BinarySearchTree()
B._root= B.rinsert(B._root,50)
B.rinsert(B._root,30)
B.rinsert(B._root,80)
B.rinsert(B._root,10)
B.rinsert(B._root,40)
B.rinsert(B._root,60)
B.rinsert(B._root,90)
B.inorder(B._root)