SlideShare a Scribd company logo
1 of 36
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

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

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