1 / 36

Binary Search Tree

Binary Search Tree. สาขาวิชาคอมพิวเตอร์และเทคโนโลยี อ.เลาขวัญ งามประสิทธิ์. นิยามของโครงสร้างข้อมูล Binary Search Tree. ทุกโหนดที่อยู่ทางด้าน sub-tree ด้านซ้ายจะต้องมีค่าน้อยกว่า root ทุกโหนดที่อยู่ทางด้าน sub-tree ด้านขวา จะต้องมีค่ามากกว่าหรือเท่ากับ root

zeno
Télécharger la présentation

Binary Search Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary Search Tree สาขาวิชาคอมพิวเตอร์และเทคโนโลยี อ.เลาขวัญ งามประสิทธิ์

  2. นิยามของโครงสร้างข้อมูล Binary Search Tree • ทุกโหนดที่อยู่ทางด้าน sub-tree ด้านซ้ายจะต้องมีค่าน้อยกว่า root • ทุกโหนดที่อยู่ทางด้าน sub-tree ด้านขวา จะต้องมีค่ามากกว่าหรือเท่ากับ root • แต่ละ sub-tree จะต้องมีคุณสมบัติตามข้อ 1 และ 2

  3. ตัวอย่าง binary search tree 5 12 5 9 7 20 (a) 32 15 43 5 24 (c) 2 8 (b) (d)

  4. ตัวอย่าง binary tree ที่ไม่จัดว่าเป็น binary search tree 2 12 1 4 8 7 20 18 (a) 15 43 24 32 5 (b) (c)

  5. การดำเนินการของ binary search tree • Traversal ท่องไปใน binary search tree เหมือนกับการท่องใน binary tree คือ - Preorder - Inorder - Postorder • Search ค้นหาโหนดที่ต้องการ • Insert แทรกโหนด • Delete ลบโหนด

  6. InorderTraversal • Inorder-tree(x) • If x != NULL • then Inorder-tree(left of x) • print x • Inorder-tree(right of x)

  7. PreorderTraversal • Preorder-tree(x) • If x != NULL • then print x • Preorder-tree(left of x) • Preorder-tree(right of x)

  8. PostorderTraversal • Postorder-tree(x) • If x != NULL • then Postorder-tree(left of x) • Postorder-tree(right of x) • print x

  9. การลบโหนดออกจาก Tree • กรณีที่โหนดนั้นเป็น leaf สามารถลบออกได้เลย • กรณีที่โหนดนั้นมี 1 child • กรณีที่โหนดนั้นมี 2 children

  10. การลบโหนดออกจาก Tree: กรณีที่โหนดนั้นมี 1 child 12 12 7 7 20 20 15 15 before after

  11. การลบโหนดออกจาก Tree: กรณีที่โหนดนั้นมี 2 child 12 12 15 7 7 7 20 20 20 15 43 15 43 43 before after Replace the key of this node with the smallest key of right subtree.

  12. แหล่งความรู้เพิ่มเติมแหล่งความรู้เพิ่มเติม Animated Binary Search Tree 1 http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html Animated Binary Search Tree 2 http://www.qmatica.com/DataStructures/Trees/BST.html

  13. AVL Tree สาขาวิชาคอมพิวเตอร์และเทคโนโลยี อ.เลาขวัญ งามประสิทธิ์

  14. นิยามของ AVL Tree • Tree นั้นจะต้องเป็น binary search tree • Tree นั้นจะต้องเป็น Balanced tree Balanced tree หมายถึง ทุกโหนดใน tree ต้องมีความสูงของ left sub-tree และ right sub-tree ต่างกันไม่เกิน 1 Balance Factor = |HL – HR|  1

  15. ตัวอย่างการตรวจสอบ AVL Tree Node5 HL = 0, HR = 0  BF = |0 - 0| = 0 Node15 HL = 1, HR = 0  BF = |1 - 0| = 1 Node43 HL = 0, HR = 0  BF = |0 - 0| = 0 Node20 HL = 2, HR = 1  BF = |2 - 1| = 1 Node7 HL = 0, HR = 0  BF = |0 - 0| = 0 Node12 HL = 1, HR = 3  BF = |1 - 3| = 2 2 12 0 1 7 20 1 0 15 43 0 เกิด Imbalanceที่โหนด 12 ดังนั้น tree นี้ไม่เป็น AVL Tree 5

  16. 1 11 1 0 3 24 0 1 1 1 18 34 เป็น AVL Tree 0 0 86 13 พิจารณาว่า Tree ต่อไปนี้เป็น AVL Tree หรือไม่

  17. 1 25 2 1 14 43 2 0 1 8 32 56 ดังนั้น ไม่เป็น AVL Tree 0 0 0 0 41 3 74 29 0 0 72 81 พิจารณาว่า Tree ต่อไปนี้เป็น AVL Tree หรือไม่ เกิด Imbalance ที่โหนด 14 และ 56

  18. Unbalanced Tree • Left of Left • Right of Right • Right of Left • Left of Right

  19. Left of Left • เกิดเมื่อมีการแทรกโหนดเข้าไปด้าน sub-tree ฝั่งซ้ายของโหนดลูกทางซ้าย เกิด Imbalance 11 11 5 24 5 24 3 7 3 7 2 2

  20. Right of Right • เกิดเมื่อมีการแทรกโหนดเข้าไปด้าน sub-tree ฝั่งขวาของโหนดลูกทางขวา เกิด Imbalance 11 11 5 24 5 24 15 45 15 45 82 82

  21. Right of Left • เกิดเมื่อมีการแทรกโหนดเข้าไปด้าน sub-tree ฝั่งขวาของโหนดลูกทางซ้าย เกิด Imbalance 11 11 5 24 5 24 3 10 3 10 6 6

  22. Left of Right • เกิดเมื่อมีการแทรกโหนดเข้าไปด้าน sub-tree ฝั่งขวาของโหนดลูกทางซ้าย เกิด Imbalance 11 11 5 24 5 24 15 45 15 45 18 18

  23. การทำ Balancing Tree • Single rotation การหมุน 1 ครั้ง ใช้กรณีเกิด Left of Left และ Right of Right • Double rotation การหมุน 2 ครั้ง ใช้กรณีเกิด Right of Left และ Left of Right

  24. Single rotation : rotate right Rotate Right เกิด Imbalance C A C B A B

  25. 28 ตัวอย่าง 10 56 2 12 49 24 11 30 5 28 11 49 10 5 24 2 12 30 56

  26. ตัวอย่าง Rotate Right 11 5 5 24 3 11 24 3 7 2 7 2

  27. C A B Single rotation : rotate left Rotate Left เกิด Imbalance A B C

  28. 13 ตัวอย่าง 5 20 2 7 26 4 12 38 20 13 12 26 5 4 20 2 7 20 38

  29. 24 11 45 5 15 82 ตัวอย่าง 11 5 24 15 45 82

  30. Double rotation: left of right เกิด Imbalance N1 N2 N1 M N2 N1 N3 N3 M P N M N O P N3 N2 O P N O

  31. ตัวอย่าง 11 15 5 24 11 24 5 15 45 18 45 18

  32. Double rotation: right of left Rotate Right เกิด Imbalance N3 N2 N1 N3 N1 P P M N O M N2 N O

  33. ตัวอย่าง 11 10 5 24 5 11 3 10 3 6 24 6

  34. ตัวอย่าง algorithm : rotate left

  35. จงสร้าง AVL Tree จากข้อมูลที่นำเข้ามาตามลำดับดังต่อไปนี้ โดยให้แสดงผล Balance Factors ด้วย • 23, 54, 34, 2, 14, 7, 26, 16, 87, 21, 9 • 5, 23, 57, 8, 12, 45, 21, 36, 28, 4, 22, 13, 27

  36. แหล่งความรู้เพิ่มเติมแหล่งความรู้เพิ่มเติม Animated AVL Tree 1 http://www.cs.jhu.edu/~goodrich/dsa/trees/avltree.html Animated AVL Tree 2 http://www.qmatica.com/DataStructures/Trees/AVL/AVLTree.swf

More Related