SlideShare a Scribd company logo
Binary Search Tree
สาขาวิชาคอมพิวเตอร ์และเทคโนโลยี
อ.เลาขวัญ งามประสิทธิ์
นิยามของโครงสร้างข้อมูล Binary
Search Tree
• ทุกโหนดที่อยู่ทางด้าน sub-tree ด้านซ้ายจะต้อง
มีค่าน้อยกว่า root
• ทุกโหนดที่อยู่ทางด้าน sub-tree ด้านขวา
จะต้องมีค่ามากกว่าหรือเท่ากับ root
• แต่ละ sub-tree จะต้องมีคุณสมบัติตามข้อ 1
และ 2
ตัวอย่าง binary search tree
5
5
2 8
12
7 20
15 43
5
9
32
24
(a)
(b)
(c)
(d)
ตัวอย่าง binary tree
ที่ไม่จัดว่าเป็ น binary search
tree
5
12
7 20
15 43
1
18
32
24
(a)
(c)
2
4 8
(b)
การดาเนินการของ binary
search tree
• Traversal ท่องไปใน binary search
tree
เหมือนกับการท่องใน binary
tree คือ
- Preorder
- Inorder
- Postorder
• Search ค้นหาโหนดที่ต้องการ
• Insert แทรกโหนด
Inorder Traversal
• Inorder-tree(x)
1. If x != NULL
2. then Inorder-tree(left of x)
3. print x
4. Inorder-tree(right of x)
Preorder Traversal
• Preorder-tree(x)
1. If x != NULL
2. then print x
3. Preorder-tree(left of x)
4. Preorder-tree(right of x)
Postorder Traversal
• Postorder-tree(x)
1. If x != NULL
2. then Postorder-tree(left of x)
3. Postorder-tree(right of x)
4. print x
การลบโหนดออกจาก Tree
1. กรณีที่โหนดนั้นเป็ น leaf สามารถลบออกได้
เลย
2. กรณีที่โหนดนั้นมี 1 child
3. กรณีที่โหนดนั้นมี 2 children
การลบโหนดออกจาก Tree:
กรณีที่โหนดนั้นมี 1 child
12
7 20
15
12
7 20
15
before after
การลบโหนดออกจาก Tree:
กรณีที่โหนดนั้นมี 2 child
before after
12
7 20
15 43
15
7 20
43
Replace the key of this node with the smallest key of right subtree.
12
7 20
15 43
แหล่งความรู ้เพิ่มเติม
Animated Binary Search Tree
1
http://www.cs.jhu.edu/~goodrich/
dsa/trees/btree.html
Animated Binary Search Tree 2
http://www.qmatica.com/DataStructu
res/Trees/BST.html
AVL Tree
สาขาวิชาคอมพิวเตอร ์และเทคโนโลยี
อ.เลาขวัญ งามประสิทธิ์
นิยามของ AVL Tree
• Tree นั้นจะต้องเป็ น binary search tree
• Tree นั้นจะต้องเป็ น Balanced tree
Balanced tree หมายถึง ทุกโหนดใน tree
ต้องมีความสูงของ
left sub-tree และ right sub-tree
ต่างกันไม่เกิน 1
เกิด Imbalance ที่โหนด 12
ดังนั้น tree นี้ไม่เป็ น AVL Tree
ตัวอย่างการตรวจสอบ AVL Tree
5
12
7 20
15 43
Node5 HL = 0, HR = 0  B
Node15 HL = 1, HR = 0  B
Node43 HL = 0, HR = 0  B
Node20 HL = 2, HR = 1  B
Node7 HL = 0, HR = 0  B
Node12 HL = 1, HR = 3  B
2
1
1 0
0
0
พิจารณาว่า Tree ต่อไปนี้เป็ น AVL
Tree หรือไม่
1
11
3 24
18 34
13 86
1
0
1 1
0
1
0 0
เป็ น AVL Tree
พิจารณาว่า Tree ต่อไปนี้เป็ น AVL
Tree หรือไม่
8
25
14 43
32 56
29 74
3
81
41
72
1
1
0 2
1
2
0 0
0 0
0 0
ดังนั้น ไม่เป็ น AVL Tree
เกิด Imbalance ที่โหนด 14 และ
Unbalanced Tree
• Left of Left
• Right of Right
• Right of Left
• Left of Right
Left of Left
• เกิดเมื่อมีการแทรกโหนดเข้าไปด้าน sub-tree ฝั่งซ ้าย
ของโหนดลูกทางซ้าย
3
11
5 24
7
2
3
11
5 24
7
2
เกิด Imbalance
Right of Right
• เกิดเมื่อมีการแทรกโหนดเข้าไปด้าน sub-tree ฝั่งขวา
ของโหนดลูกทางขวา
15
11
5 24
45
82
15
11
5 24
45
82
เกิด Imbalance
Right of Left
• เกิดเมื่อมีการแทรกโหนดเข้าไปด้าน sub-tree ฝั่งขวา
ของโหนดลูกทางซ้าย
3
11
5 24
10
6
3
11
5 24
10
6
เกิด Imbalance
Left of Right
• เกิดเมื่อมีการแทรกโหนดเข้าไปด้าน sub-tree ฝั่งขวา
ของโหนดลูกทางซ้าย
15
11
5 24
45
18
15
11
5 24
45
18
เกิด Imbalance
การทา Balancing Tree
• Single rotation
การหมุน 1 ครั้ง ใช้กรณีเกิด Left of Left
และ Right of Right
• Double rotation
การหมุน 2 ครั้ง ใช้กรณีเกิด Right of Left
และ Left of Right
Single rotation : rotate
right
A
C
B
A
C
B
Rotate Right
เกิด Imbalance
ตัวอย่าง
11
5
24
11
5 24
10
2 12
28
56
49
30
10
2 12
28
56
49
30
ตัวอย่าง
3
11
5 24
7
2
2
5
3 11
7 24
Rotate Right
Single rotation : rotate
left
C
A
B
C
A B
Rotate Left
เกิด Imbalance
ตัวอย่าง
5
2 7
13
20
26
38
5
2 7
13
38
26
20
12
20
4
12
20
4
ตัวอย่าง
15
11
5 24
45
82
5
24
11 45
82
15
Double rotation : left of
right
N3
N2
P
O
N
N1
M
N1
N2
M
N O
N3
P
เกิด Imbalance
N1
N2
M
N
O
N3
P
ตัวอย่าง
15
11
5 24
45
18
18
15
11 24
45
5
Double rotation : right of
left
N3
N2
P
O
Rotate Right
N
N1
M
N3
N2
P
O
N
N1
M
เกิด Imbalance
ตัวอย่าง
3
11
5 24
10
6
3
10
5 11
6 24
ตัวอย่าง algorithm : rotate left
จงสร้าง AVL Tree จากข้อมูลที่นาเข้า
มาตามลาดับดังต่อไปนี้ โดยให้
แสดงผล Balance Factors ด้วย
1) 23, 54, 34, 2, 14, 7, 26, 16, 87, 21, 9
2) 5, 23, 57, 8, 12, 45, 21, 36, 28, 4, 22, 13, 27
แหล่งความรู ้เพิ่มเติม
Animated AVL Tree 1
http://www.cs.jhu.edu/~goodrich/ds
a/trees/avltree.html
Animated AVL Tree 2
http://www.qmatica.com/DataStructures/
Trees/AVL/AVLTree.swf

More Related Content

PPT
ch10_EffiBinSearchTrees.ppt
PPT
BinarySearchTrees.ppt
PPT
BinarySearchTrees (1).ppt
PPT
bst.ppt
PPT
avl-tree-animation.ppt
PPT
avl.ppt
PPT
15-btrees.ppt
PPT
chapt10b.ppt
ch10_EffiBinSearchTrees.ppt
BinarySearchTrees.ppt
BinarySearchTrees (1).ppt
bst.ppt
avl-tree-animation.ppt
avl.ppt
15-btrees.ppt
chapt10b.ppt
Ad

dokumen.tips_binary-search-tree-5698bcbd37e20.ppt