1 / 30

Review of Chapter 5

Review of Chapter 5. 張啟中. Threaded Binary Tree. 利用 Binary Tree 節點中的 0-links ,指向中序的先行者或後繼者,以方便中序追蹤。 Threading Rules

dora
Download Presentation

Review of Chapter 5

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. Review of Chapter 5 張啟中

  2. Threaded Binary Tree • 利用 Binary Tree 節點中的 0-links,指向中序的先行者或後繼者,以方便中序追蹤。 • Threading Rules • A 0 RightChild field at node p is replaced by a pointer to the node that would be visited after p when traversing the tree in inorder. That is, it is replaced by the inorder successor of p. (右樹指標指向該節點中序的後繼者) • A 0 LeftChild link at node p is replaced by a pointer to the node that immediately precedes node p in inorder (i.e., it is replaced by the inorder predecessor of p). (左樹指標指向該節點中序的先行者)

  3. Threaded Binary Tree A B C D E F G H I Inorder sequence: H, D, I, B, E, A, F, C, G

  4. D f f Threaded Binary Tree with Head Node LeftThread LeftChild RightChild RightThread data Head Node TRUE FALSE - f f A f f B f f B f f t E t f D f t E t H t I t t t

  5. Manipulation of Threaded Binary Tree • Traversal (Inorder 不需 Stack) • Insert a Node • Delete a Node

  6. Insert a Node to Threaded Binary Tree s s r r before after

  7. r Insert a Node to Threaded Binary Tree s s r r after before

  8. Heaps • Definition • A max (min) tree is a tree in which the key value in each node is no smaller (larger) than the key values in its children (if any). • A max heap is a complete binary tree that is also a max tree. • A min heap is a complete binary tree that is also a min tree. • We can use the max heap to implement the priority Queues.

  9. Priority Queues • A data structure supports the below two operations is called max (min) priority queue. • In a priority queue, the element to be deleted is the one with highest (or lowest) priority. • An element with arbitrary priority can be inserted into the queue according to its priority.

  10. Priority Queues 的運用 • Suppose a server that serve multiple users. Each user may request different amount of server time. A priority queue is used to always select the request with the smallest time. Hence, any new user’s request is put into the priority queue. This is the min priority queue. • If each user needs the same amount of time but willing to pay more money to obtain the service quicker, then this is max priority queue.

  11. Representation of The Priority Queues • Unorder Linear List • Addition complexity: O(1) • Deletion complexity: O(n) • Chain • Addition complexity: O(1) • Deletion complexity: O(n) • Ordered List • Addition complexity: O(n) • Deletion complexity: O(1)

  12. Max Heap Examples 9 14 3 6 12 7 8 10 6 5

  13. Manipulation of The Heap • Create of an empty heap • Insertion of a new element into the heap. • Deletion of the largest element from the heap Please see book p286 ADT 5.2

  14. 5 Insertion into a Max Heap O(logn) 20 20 2 15 5 15 2 5 2 14 10 10 14

  15. Insertion into a Max Heap 20 20 2 15 5 15 2 21 14 10 10 14 請自己練習

  16. Deletion from a Max Heap O(logn) 20 20 15 2 15 2 14 10 14 10 請同學自己繼續練習刪除 15

  17. Binary Search Tree • Definition • A binary serach tree is a binary tree. It may be empty. If it is not empty then it satisfies the following properties: • Every element has a key and no two elements have the same key (i.e., the keys are distinct) • The keys (if any) in the left subtree are smaller than the key in the root. • The keys (if any) in the right subtree are larger than the key in the root. • The left and right subtrees are also binary search trees.

  18. 60 70 80 65 Binary Search Tree Examples 30 20 5 40 15 25 2 14 10 22 Yes! Binary search trees Not binary search tree

  19. Manipulation of The Binary Search Tree • Searching a Binary Search Tree • Insertion into a Binary Search Tree • Deletion from a Binary Search Tree • Joining and Splitting Binary Search Tree

  20. Searching A Binary Search Tree • If the root is 0, then this is an empty tree. No search is needed. • If the root is not 0, compare the x with the key of root. • If x equals to the key of the root, then it’s done. • If x is less than the key of the root, then only need to search the left tree. • If x larger than the key of the root, only the right subtree is to be searched.

  21. Insertion into a Binary Search Tree 30 30 40 5 40 5 2 80 35 2 80

  22. Deletion from a Binary Search Tree • Delete a leaf node • A leaf node which is a right child of its parent • A leaf node which is a left child of its parent • Delete a non-leaf node • A node that has one child • A node that has two children • Replaced by the largest element in its left subtree, or • Replaced by the smallest element in its right subtree • Again, the delete function has complexity of O(h)

  23. Deletion from a Binary Search Tree 30 5 5 40 80 80 2 35 35

  24. Deletion from a Binary Search Tree 30 30 37 5 40 5 35 2 80 35 2 80 32 32 37 90 90

  25. Deletion from a Binary Search Tree 30 30 5 80 40 5 35 2 90 35 2 80 32 37 32 37 90

  26. Joining and Splitting Binary Trees • C.ThreeWayJoin(A, x, B) • Creates a binary search tree C that consists of binary search tree A, B, and element x. • C.TwoWayJoin(A, B) • Joins two binary search trees A and B to obtain a single binary search tree C. • A.Split(i, B, x, C) • Binary search tree A splits into three parts: B (a binary search tree that contains all elements of A that have key less than i); if A contains a key i than this element is copied into x and a pointer to x returned; C is a binary search tree that contains all records of A that have key larger than i

  27. ThreeWayJoin(A, x, B) 81 30 90 81 40 5 94 85 2 80 35 84 92 A x B

  28. C.TwoWayJoin(A, B) 84 80 30 90 40 5 94 85 2 80 35 84 92 A B

  29. A.Split(i, B, x, C) x i = 30 30 40 5 2 80 35 B 81 75 A C

  30. A.Split(i, B, x, C) Z Y L i = 80 30 t R 30 L 81 40 5 R t C 5 40 L 2 80 35 t 2 80 35 75 81 75 x A B

More Related