-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlab8_1.py
123 lines (98 loc) · 3.09 KB
/
lab8_1.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
import random
class Node:
def __init__(self,data,left=None,right=None):
self.data = data
self.left = left
self.right = right
class BST:
def __init__(self,data=None):
self.root = Node(data)
self.level = -1
def printSideway(root,level):
if root != None:
BST.printSideway(root.right,level+1)
print(' '*(3*level),root.data)
BST.printSideway(root.left,level+1)
def addI(self,data):
q = []
q.append(self.root)
while len(q) != 0:
n = q.pop(0)
if not n.left < data:
q.append(n.left)
if not n.right >= data:
q.append(n.right)
if n.left == None:
pass
else:q.append(n.left)
if n.right == None:
passs
else:q.append(n.right)
def insertR(root,data):
if root.data == None:
root.data = data
return
if data < root.data:
if root.left != None:
BST.insertR(root.left,data)
else:
root.left = Node(data)
else:
if root.right != None:
BST.insertR(root.right,data)
else:
root.right = Node(data)
def inOrder(root,lamb=None):
if root.left != None:
BST.inOrder(root.left,lamb)
if root.data != None:
if lamb == None:
print(root.data,end=' ')
else:
lamb(root.data)
if root.right != None:
BST.inOrder(root.right,lamb)
def _search(root,data):
if root.left != None:
return BST._search(root.left,data)
if root.data != None:
if root.data == data:
return root.data
if root.right != None:
return BST._search(root.right,data)
def search(self,data):
return BST._search(self.root,data)
def _path(root,data):
if root.left != None:
res = BST._path(root.left,data)
if res!=None:
print(root.data)
return res
if root.data != None:
if root.data == data:
print(root.data)
return root.data
if root.right != None:
res = BST._path(root.right,data)
if res!=None:
print(root.data)
return res
def path(self,data):
BST._path(self.root,data)
def _del(root,data):
if root.left != None:
BST._del(root.left,data)
if root.data != None:
if root.data == data:
pass
# root.
if root.right != None:
BST._del(root.right,data)
def dele(self,data):
BST._del(self.root,data)
# datainput = [int(e) for e in input('insert integers : ').split()]
datainput = [x for x in range(10)]
print(datainput)
trees = BST()
for element in datainput:BST.insertR(trees.root,element)
BST.inOrder(trees.root,lambda x:print(x,end=' '))