-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.py
More file actions
140 lines (119 loc) · 3.49 KB
/
AVLTree.py
File metadata and controls
140 lines (119 loc) · 3.49 KB
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
135
136
137
138
139
140
from BinaryTree import *
from BinSrchTree import *
def calc_height(n) :
if n is None : return 0
hLeft = calc_height(n.left)
hRight = calc_height(n.right)
if (hLeft > hRight) : return hLeft + 1
else: return hRight + 1
# 코드 9.13: 노드의 균형인수 계산 함수
def calc_height_diff(n) :
if n==None :
return 0
return calc_height(n.left) - calc_height(n.right)
# 코드 9.14: AVL 트리의 LL회전
def rotateLL(A) :
B = A.left
A.left = B.right
B.right = A
return B
# 코드 9.15: AVL 트리의 RR회전
def rotateRR(A) :
B = A.right
A.right = B.left
B.left = A
return B
# 코드 9.16: AVL 트리의 RL회전
def rotateRL(A) :
B = A.right
A.right = rotateLL(B)
return rotateRR(A)
# 코드 9.17: AVL 트리의 LR회전
def rotateLR(A) :
B = A.left
A.left = rotateRR(B)
return rotateLL(A)
# 코드 9.18: AVL 트리의 재균형 함수
def reBalance (parent) :
hDiff = calc_height_diff(parent)
if hDiff > 1 :
if calc_height_diff( parent.left ) > 0 :
parent = rotateLL( parent )
else :
parent = rotateLR( parent )
elif hDiff < -1 :
if calc_height_diff( parent.right ) < 0 :
parent = rotateRR( parent )
else :
parent = rotateRL( parent )
return parent
# 코드 9.19: AVL 트리의 삽입 연산
def insert_avl(parent, node) :
if node.key < parent.key :
if parent.left != None :
parent.left = insert_avl(parent.left, node)
else :
parent.left = node
return reBalance(parent)
elif node.key > parent.key :
if parent.right != None :
parent.right = insert_avl(parent.right, node)
else :
parent.right = node
return reBalance(parent);
else :
print("중복된 키 에러")
from CircularQueue import CircularQueue
def levelorder(root) :
queue = CircularQueue(100)
queue.enqueue(root)
while not queue.isEmpty() :
n = queue.dequeue()
if n is not None :
print(n.key, end=' ')
queue.enqueue(n.left)
queue.enqueue(n.right)
def delete(root,key):
if not root:
return root
elif key<root.key:
root.left=delete(root.left,key)
elif key > root.key:
root.right=delete(root.right, key)
else:
if not root.left:
temp = root.right
root = None
return temp
elif not root.right:
temp = root.left
root =None
return temp
temp=search_min_bst(root.right)
root.key = temp.key
root.right = delete(root.right, temp.key)
return reBalance(root)
# 코드 9.20: AVL 트리 테스트 프로그램
if __name__ == "__main__":
node = [7,8,9,2,1,5,3,6,4]
# node = [0,1,2,3,4,5,6,7,8,9]
root = None
for i in node :
n = BSTNode(i)
if root == None :
root = n
else :
root = insert_avl(root, n)
print("AVL(%d): "%i, end='')
levelorder(root)
print()
print(" 노드의 개수 =", count_node(root))
print(" 단말의 개수 =", count_leaf(root))
print(" 트리의 높이 =", calc_height(root))
print("\n삭제 연산 테스트:")
to_delete = [5, 9, 2] # 삭제할 노드들
for num in to_delete:
print("\n노드 %d 삭제 후 AVL:" % num)
root = delete(root, num)
levelorder(root)
print()