-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk.py
101 lines (90 loc) · 3.11 KB
/
k.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
#จงเขียนฟังก์ชัน สำหรับตรวจสอบว่า Tree นี้เป็น binary search tree หรือไม่
#โดยกำหนดให้ 0<=node.data<=100 และฟังก์ชั่นมีparameterมากที่สุด4ตัว
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
self.level = None
def __str__(self):
return str(self.data)
class Tree:
def __init__(self):
self.root = None
self.num = 0
def insert(self, val):
if self.root == None:
self.root = Node(val)
self.num += 1
else:
h = height(self.root)
max_node = pow(2,h+1)-1
current = self.root
if self.num+1 > max_node:
while(current.left != None):
current = current.left
current.left = Node(val)
self.num+=1
elif self.num+1 == max_node:
while(current.right != None):
current = current.right
current.right = Node(val)
self.num+=1
else:
#print(max_node-((max_node-(pow(2,h)-1))/2))
if self.num+1 <= max_node-((max_node-(pow(2,h)-1))/2):
insert_subtree(current.left,self.num - round(pow(2,h)/2),val)
else:
insert_subtree(current.right,self.num - pow(2,h),val)
self.num+=1
def insert_subtree(r,num,val):
if r != None:
h = height(r)
max_node = pow(2,h+1)-1
current = r
if num+1 > max_node:
while(current.left != None):
current = current.left
current.left = Node(val)
return
elif num+1 == max_node:
while(current.right != None):
current = current.right
current.right = Node(val)
return
if num+1 <= max_node-((max_node-(pow(2,h)-1))/2):
insert_subtree(current.left,num - round(pow(2,h)/2),val)
else:
insert_subtree(current.right,num - pow(2,h),val)
else:
return
def height(root):
if root == None:
return -1
else:
left = height(root.left)
right = height(root.right)
if left>right:
return left + 1
else:
return right + 1
def printTree90(node, level = 0):
if node != None:
printTree90(node.right, level + 1)
print(' ' * level, node)
printTree90(node.left, level + 1)
def check_binary_search_tree_(node):
return is_BST(node, 0, 100)
def is_BST(node, _min, _max):
print(_min,_max)
if node is None:
return True
if node.data < _min or node.data > _max:
return False
return is_BST(node.left,_min,node.data-1) and is_BST(node.right,node.data+1,_max)
tree = Tree()
data = input("Enter Input : ").split()
for e in data:
tree.insert(int(e))
printTree90(tree.root)
print(check_binary_search_tree_(tree.root))