1 / 19

Chapter 11 : Trees

Chapter 11 : Trees. A. B. C. D. F. E. General trees. มี 1 node r เป็น root Set ของ general trees เรียกว่า subtree ของ r. A. B. C. D. F. E. r. T L. T R. Binary trees. Empty หรือ ไม่มี node มีรูปแบบ โดยที่ r เป็น root มี node ลูก 0-2 node.

Download Presentation

Chapter 11 : Trees

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. Chapter 11 : Trees A B C D F E

  2. General trees • มี 1 node r เป็น root • Set ของ general trees เรียกว่า subtree ของ r A B C D F E

  3. r TL TR Binary trees • Empty หรือ ไม่มี node • มีรูปแบบ โดยที่ r เป็น root มี node ลูก 0-2 node

  4. The ADT Binary Search Tree คุณสมบัติของ Binary Search Tree • ค่าของ node n > ทุกค่าใน left subtree(TL) • ค่าของ node n < ทุกค่าใน right subtree(TR) • TL และ TR เป็น Binary Search Tree สรุป Binary Search Tree left subtree(TL)< Root < right subtree(TR)

  5. ตัวอย่าง Binary Search Tree Jane Tom Bob Alan Ellen Nancy Wendy

  6. ตัวอย่าง Binary Search Tree Tom Jane Wendy Bob Nancy Alan Ellen

  7. ตัวอย่าง Binary Search Tree Jane Bob Nancy Alan Ellen Tom Wendy

  8. ตัวอย่าง Binary Search Tree Alan Bob Ellen Jane Nancy Tom Wendy

  9. Operation of the Binary Search Tree Operation ของ Binary Search Tree • Insert new item ใน binary serch tree • Delete item ที่หาได้จาก search key • Retrieve item หรือแสดงข้อมูลที่หาได้จาก search key • Traverse item หรือการเดินทางใน binary serch tree ในรูปแบบของ preorder,inorder และ postorder

  10. Pseudocode for the Operations of the Binary Search Tree • insert(in newItem:TreeItemType) //Inserts newItem whose items have distinct search //keys that differ from newItem’s search key. • delete(in searchkey:KeyType) throws TreeException //Deletes item whose search key = searchKey //ถ้าไม่มี item นั้น  operation fails • retrieve (in searchkey:KeyType) : TreeItemType //Returns item whose search key = searchKey //ถ้าไม่มี item นั้น  return null

  11. UML diagram for BinarySearchTree

  12. Algorithms for Search Operations +search( in bst : BinarySearchTree, in searchKey:KeyType) //Search หา item ที่ search key เท่ากับ searchKey if (bst is emty) { record not found } else if (searchKey == search key of root’s item) { record is found } else if (searchKey < search key of root’s item) { search(Left subtree of bst, searchKey) } else { search(Right subtree of bst, searchKey) } //end if สรุปการ search 1.ถ้า bst ว่าง  not found 2.ถ้า searchKey = root record is found 3.ถ้า searchKey < root ให้หาจาก left subtree 4.ถ้า searchKey > root ให้หาจาก right subtree

  13. Algorithms for Insert Operations +inserchItem( in treeNode:TreeNode, in newItem:TreeItemType) //Inserts newItem which treeNode is the root. if (treeNode is null) { Create new node & let treeNode reference it & set reference in new node to null } else if (newItem.getKey() < treeNode.getItem( ).getKey()) { treeNode.setLeft(insertItem(treeNode.getLeft(), newItem)) } else { treeNode.setright(insertItem(treeNode.getRight(), newItem)) } //end if return treeNode

  14. Algorithms for Insert Operations สรุปการ Insert 1.Insert empty tree(treeNode is null) สร้าง treeNode reference ไปที่ new node และ reference ใน new node = null 2.ถ้าค่าของ new item < ค่าของ treeNode ให้หาตำแหน่งจากทางซ้าย 3.ถ้าค่าของ new item > ค่าของ treeNode ให้หาตำแหน่งจากทางขวา

  15. Delete Operations Delete item i ที่ Node N มี 3 Case คือ 1. N is a leaf. Link 2. N has only one child. 3. N has two children.

  16. Algorithms for Retrieval Operations +retrieveItem( in treeNode:TreeNode, in serchKey:KeyType):TreeItemType //Returns item ที่ search key เท่ากับ searchKey if (treeNode == null) { treeItem = null // tree is empty } else if (searchKey < treeNode.getItem()) { treeItem = retrieveItem(treeNode.getLeft(), searchKey) } else { treeItem = retrieveItem(treeNode.getRight(), searchKey) } //end if return treeItem

  17. Algorithms for Travasal Operations +inorder( in bst : BinarySearchTree) { //Traverses the binary tree bst in inorder if (bst is not emty) { inorder(Left subtree of bst’s root) process the root of bst inorder(Right subtree of bst’s root) } //end if สรุปการเดินทางใน BSt • เหมือนกับใน binary tree • ถ้าเป็นแบบ inorder ใน BST จะเป็นแบบเรียงลำดับ Left  root  Right

  18. r TL TR InOrder Travasal THEOREM 1 The inorder travasal of a binary search tree T will visit its nodes in sorted search-key order PROOF เนื่องจากคุณสมบัติของ BST : left subtree(TL)< Root < right subtree(TR) การเดินทางแบบ inorder เดินทางจาก TL  root  TR ดังนั้น การเดินทางแบบ inorder เป็นแบบเรียงลำดับ

  19. Treesort

More Related