1 / 68

Lecture Objectives

Lecture Objectives. To understand the difference between binary trees, binary search trees, and heaps To learn how to implement binary trees, binary search trees, and heaps using linked data structures and arrays

nam
Download Presentation

Lecture Objectives

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. Lecture Objectives • To understand the difference between binary trees, binary search trees, and heaps • To learn how to implement binary trees, binary search trees, and heaps using linked data structures and arrays • To learn how to use a binary search tree to store information so that it can be retrieved in an efficient manner CS340

  2. Binary Search Trees CS340

  3. Overview of a Binary Search Tree • Recall the definition of a binary search tree: A set of nodes T is a binary search tree if either of the following is true • T is empty • If T is not empty, its root node has two subtrees, TL and TR, such that • TLand TR are binary search trees • the value in the root node of T is greater than all values in TL and less than all values in TR CS340

  4. Overview of a Binary Search Tree (cont.) rings Elven three shadows where kings Dwarf Mordor sky land dark stone nine lords bring bind doomed one find CS340

  5. Recursive Algorithm for Searching a Binary Search Tree 5 • if the root is null • the item is not in the tree; return null • Compare the value of target with root.data • if they are equal • the target has been found; return the data at the root else if the target is less than root.data • return the result of searching the left subtree else • return the result of searching the right subtree CS340

  6. Searching a Binary Tree (looking for word “one”) rings Elven three shadows where kings Dwarf Mordor sky land dark stone nine lords bring bind doomed one find CS340

  7. Performance 7 • Search a tree is generally O(log n) • If a tree is not very full, performance will be worse • Searching a tree with only right subtrees, for example, is O(n) CS340

  8. InterfaceSearchTree<E> CS340

  9. BinarySearchTree<E> Class CS340

  10. ImplementingfindMethods CS340

  11. Insertion into a Binary Search Tree CS340

  12. Implementing the addMethods 12 /** Starter method add. pre: The object to insert must implement the Comparable interface. @param item The object being inserted @return true if the object is inserted, false if the object already exists in the tree */ public boolean add(E item) { root = add(root, item); return addReturn; } CS340

  13. Implementing the addMethods (cont.) /** Recursive add method. post: The data field addReturn is set true if the item is added to the tree, false if the item is already in the tree. @param localRoot The local root of the subtree @param item The object to be inserted @return The new local root that now contains the inserted item */ private Node<E> add(Node<E> localRoot, E item) { if (localRoot == null) { // item is not in the tree — insert it. addReturn = true; return new Node<E>(item); } else if (item.compareTo(localRoot.data) == 0) { // item is equal to localRoot.data addReturn = false; return localRoot; } else if (item.compareTo(localRoot.data) < 0) { // item is less than localRoot.data localRoot.left = add(localRoot.left, item); return localRoot; } else { // item is greater than localRoot.data localRoot.right = add(localRoot.right, item); return localRoot; } } CS340

  14. Removal from a Binary Search Tree • If the item to be removed has two children, replace it with the inorder predecessor rings Elven three shadows where kings Dwarf doomed Mordor sky land dark stone nine lords bring bind doomed one find

  15. Algorithm for Removing from a Binary Search Tree CS340

  16. Implementing the deleteMethod 16 /** * Starter method delete. * @post The object is not in the tree. * @param target The object to be deleted * @return The object deleted from the tree * or null if the object was not in the tree * @throws ClassCastException if target does not implement * Comparable */ public E delete(E target) { root = delete(root, target); return deleteReturn; } CS340

  17. Implementing the deleteMethod (cont.) 17 /*** Recursive delete method. */ private Node<E> delete(Node<E> localRoot, E item) { if (localRoot == null) { // item is not in the tree. deleteReturn = null; return localRoot; } // Search for item to delete. intcompResult = item.compareTo(localRoot.data); if (compResult < 0) {// item is smaller than localRoot.data. localRoot.left = delete(localRoot.left, item); return localRoot; } else if (compResult > 0) { localRoot.right= delete(localRoot.right, item); return localRoot; }… CS340

  18. …} else {// item is at local root. deleteReturn = localRoot.data; if (localRoot.left == null) { // If there is no left child, return right child // which can also be null. return localRoot.right; } else if (localRoot.right == null) { // If there is no right child, return left child. return localRoot.left; } else { // Node being deleted has 2 children if (localRoot.left.right == null) { // The left child has no right child. localRoot.data= localRoot.left.data; // Replace the left child with its left child. localRoot.left = localRoot.left.left; return localRoot; } else { // Search for the inorder predecessor (ip) and localRoot.data= findLargestChild(localRoot.left); return localRoot; } } } } 18

  19. MethodfindLargestChild 19

  20. Testing a Binary Search Tree 20 • Verify that an inorder traversal will display the tree contents in ascending order after • a series of insertions • a series of deletions are performed CS340

  21. Heaps and Priority Queues CS340

  22. Heaps and Priority Queues • A heap is a complete binary tree with the following properties • The value in the root is the smallest item in the tree • Every subtree is a heap 6 18 29 20 39 66 28 37 26 76 32 74 89 CS340

  23. Inserting an Item into a Heap 23 6 18 29 20 39 66 28 37 26 76 32 74 89 CS340

  24. Inserting an Item into a Heap (cont.) 24 6 18 29 20 39 66 28 8 37 26 76 32 74 89 CS340

  25. Inserting an Item into a Heap (cont.) 25 6 18 29 20 39 8 28 66 37 26 76 32 74 89 CS340

  26. Inserting an Item into a Heap (cont.) 26 6 18 8 20 39 29 28 66 37 26 76 32 74 89 CS340

  27. Inserting an Item into a Heap (cont.) 27 6 18 8 20 39 29 28 66 37 26 76 32 74 89 CS340

  28. Removing an Item from a Heap 28 6 18 8 20 39 29 28 66 37 26 76 32 74 89 CS340

  29. Removing an Item from a Heap (cont.) 29 6 18 8 20 39 29 28 66 37 26 76 32 74 89 CS340

  30. Removing an Item from a Heap (cont.) 30 66 18 8 20 39 29 28 37 26 76 32 74 89 CS340

  31. Removing an Item from a Heap (cont.) 31 8 18 66 20 39 29 28 37 26 76 32 74 89 CS340

  32. Removing an Item from a Heap (cont.) 32 8 18 29 20 39 66 28 37 26 76 32 74 89 CS340

  33. Removing an Item from a Heap (cont.) 33 8 18 29 20 39 66 28 37 26 76 32 74 89 CS340

  34. Implementing a Heap 34 • A heap is a complete binary tree • Implemented efficiently using an array rather than a linked data structure CS340

  35. Implementing a Heap (cont.) 35 8 8 29 29 18 18 39 66 66 20 20 28 28 39 37 37 26 26 76 76 32 32 74 74 89 89 0 1 2 3 4 5 6 7 8 9 10 11 12 20 66 32 28 37 26 76 74 89 8 18 29 39 CS340

  36. Implementing a Heap (cont.) 36 For a node at position p,L. child position: 2p + 1 R. child position: 2p + 2 8 18 29 20 39 66 28 0 1 2 3 4 5 6 7 8 9 10 11 12 20 66 32 28 37 26 76 74 89 8 18 29 39 37 26 76 32 Parent L. Child R. Child 74 89 CS340

  37. Implementing a Heap (cont.) 37 For a node at position p,L. child position: 2p + 1 R. child position: 2p + 2 8 18 29 20 39 66 28 0 1 2 3 4 5 6 7 8 9 10 11 12 20 66 32 28 37 26 76 74 89 8 18 29 39 37 26 76 32 Parent L. Child R. Child 74 89 CS340

  38. Implementing a Heap (cont.) 38 For a node at position p,L. child position: 2p + 1 R. child position: 2p + 2 8 18 29 20 39 66 28 0 1 2 3 4 5 6 7 8 9 10 11 12 20 66 32 28 37 26 76 74 89 8 18 29 39 37 26 76 32 Parent L. Child R. Child 74 89 CS340

  39. Implementing a Heap (cont.) 39 For a node at position p,L. child position: 2p + 1 R. child position: 2p + 2 8 18 29 20 39 66 28 0 1 2 3 4 5 6 7 8 9 10 11 12 20 66 32 28 37 26 76 74 89 8 18 29 39 37 26 76 32 Parent L. Child R. Child 74 89 CS340

  40. Implementing a Heap (cont.) For a node at position p,L. child position: 2p + 1 R. child position: 2p + 2 8 18 29 20 39 66 28 0 1 2 3 4 5 6 7 8 9 10 11 12 20 66 32 28 37 26 76 74 89 8 18 29 39 37 26 76 32 Parent L. Child R. Child 74 89 CS340

  41. Implementing a Heap (cont.) A node at position c can find its parent at(c – 1)/2 8 18 29 20 39 66 28 0 1 2 3 4 5 6 7 8 9 10 11 12 20 66 32 28 37 26 76 74 89 8 18 29 39 37 26 76 32 Child Parent 74 89 CS340

  42. Inserting into a Heap Implemented as an ArrayList 1. Insert the new element at the end of the ArrayList and set child to table.size() - 1 8 18 29 20 39 66 28 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 32 66 28 37 26 76 74 89 8 18 29 39 37 26 76 32 74 89 CS340

  43. Inserting into a Heap Implemented as an ArrayList (cont.) 1. Insert the new element at the end of the ArrayList and set child to table.size() - 1 6 18 29 8 20 39 66 28 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 66 32 28 37 26 76 74 89 8 6 18 29 39 37 26 76 32 Child 74 89 CS340

  44. Inserting into a Heap Implemented as an ArrayList (cont.) 2. Set parent to (child – 1)/ 2 6 18 29 8 20 39 66 28 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 66 32 28 37 26 76 74 89 8 6 18 29 39 37 26 76 32 Parent Child 74 89 CS340

  45. Inserting into a Heap Implemented as an ArrayList (cont.) • 3. while (parent >= 0 and table[parent] > table[child]) • Swap table[parent] and table[child] • Set child equal to parent • Set parent equal to (child-1)/2 6 18 29 8 20 39 66 28 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 66 32 28 37 26 76 74 89 8 6 18 29 39 37 26 76 32 Parent Child 74 89 CS340

  46. Inserting into a Heap Implemented as an ArrayList (cont.) • 3. while (parent >= 0 and table[parent] > table[child]) • Swap table[parent] and table[child] • Set child equal to parent • Set parent equal to (child-1)/2 6 18 29 66 20 39 8 28 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 8 32 28 37 26 76 74 89 66 6 18 29 39 37 26 76 32 Parent Child 74 89 CS340

  47. Inserting into a Heap Implemented as an ArrayList (cont.) • 3. while (parent >= 0 and table[parent] > table[child]) • Swap table[parent] and table[child] • Set child equal to parent • Set parent equal to (child-1)/2 6 18 29 66 20 39 8 28 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 8 32 28 37 26 76 74 89 66 6 18 29 39 37 26 76 32 Child Parent 74 89 CS340

  48. Inserting into a Heap Implemented as an ArrayList (cont.) • 3. while (parent >= 0 and table[parent] > table[child]) • Swap table[parent] and table[child] • Set child equal to parent • Set parent equal to (child-1)/2 6 18 29 66 20 39 8 28 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 8 32 28 37 26 76 74 89 66 6 18 29 39 37 26 76 32 Child Parent 74 89 CS340

  49. Inserting into a Heap Implemented as an ArrayList (cont.) • 3. while (parent >= 0 and table[parent] > table[child]) • Swap table[parent] and table[child] • Set child equal to parent • Set parent equal to (child-1)/2 6 18 29 66 20 39 8 28 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 8 32 28 37 26 76 74 89 66 6 18 29 39 37 26 76 32 Child Parent 74 89 CS340

  50. Inserting into a Heap Implemented as an ArrayList (cont.) • 3. while (parent >= 0 and table[parent] > table[child]) • Swap table[parent] and table[child] • Set child equal to parent • Set parent equal to (child-1)/2 6 18 8 66 20 39 29 28 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 29 32 28 37 26 76 74 89 66 6 18 8 39 37 26 76 32 Child Parent 74 89 CS340

More Related