1 / 27

Lecture 21: Binary Search Tree

CompSci 105 SS 2006 Principles of Computer Science. Lecture 21: Binary Search Tree. Binary Search Trees (BSTs). As with a Binary Tree, except Root node has a special data element called a key Nodes in left subtree have keys < the root’s key

Download Presentation

Lecture 21: 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. CompSci 105 SS 2006 Principles of Computer Science Lecture 21: Binary Search Tree

  2. Binary Search Trees (BSTs) As with a Binary Tree, except Root node has a special data element called a key Nodes in left subtree have keys < the root’s key Nodes in right subtree have keys > the root’s key. K >K <K

  3. Searching M B Q J O U

  4. Add as a leaf M B Q J O U

  5. C 4 7 C 3 B A 5 2 A B 6 • Draw the BST that results from inserting the values 4, 3, 2, 7, 5, 6 in order. • Draw as many different BST’s shapes as possible with nodes A, B, and C. B A A A C B C C B

  6. Inorder Traversal void printTree( TreeNode root) if ( root != null ) printTree( root.getLeft () ); println(root.getRootItem()); printTree( root.getRight() ); } M B Q J O U

  7. C C B A A B Inorder Traversal void printTree( TreeNode root) if ( root != null ) printTree( root.getLeft () ); println(root.getRootItem()); printTree( root.getRight() ); } B A A A C B C C B

  8. Preorder Traversal void printTree( TreeNode root) if ( root != null ) println( root.getRootItem()); printTree( root.getLeft () ); printTree( root.getRight() ); } M B Q J O U

  9. Postorder Traversal void printTree( TreeNode root) if ( root != null ) printTree( root.getLeft () ); printTree( root.getRight() ); println(root.getRootItem()); } M B Q J O U

  10. C C B A A B Exercise • For each of the trees below, what is the preorder traversal? What is the postorder traversal? B A A A C B C C B

  11. Level Order Traversal • Difficult • print nodes as if reading tree from a book (left to right, top to bottom) • Use a queue to keep track...”homework” M B Q J O U

  12. BST Delete M B Q J O U

  13. Deleting the root root root root root M M M M Q B Q B U J U J O O No children No right child No left child Two children

  14. Exercise • Draw the BST that results from inserting the values D, C, G, B, E, F in order. Now draw the result of deleting the root.

  15. M B Q J O U BST Efficiency

  16. M B Q U J O U Q O BST Efficiency M J B

  17. M B Q U J O U Q O Average Case? M J B

  18. Full and Complete Trees M Q G O U B J A full tree

  19. Full and Complete Trees M M Q Q G G O U B J B A full tree A complete tree

  20. Minimising Tree Height M B Q A J O

  21. Priority Queue Operations void create () boolean isEmpty() void insert( item) item delete() • To do: • Study for 105 Exam • Play • Eat • 4. Sleep Textbook, p. 518

  22. Heap Like a binary search tree, but • Always keep it a complete tree • Don’t completely sort data … just make sure that each node’s key is bigger than its children’s keys. Textbook, p. 520

  23. ADT Table Unsorted Array Sorted Array Program that uses a priority queue Sorted Linked List ADT Priority Queue Unsorted Linked List Binary Search Tree

  24. BST’s vs Heaps M U Q J G Q G U B J B M BST heap

  25. Maxheap vs. minheap Heap • A complete binary tree • Each node’s key is bigger than its children’s keys. U J Q G B M Textbook, p. 520

  26. Which are maxheaps? 8 M 7 5 G Q 4 O U 1 2 B J J M C E G J C A D A B

  27. BST’s vs Heaps M U Q J G Q G U B J B M BST heap

More Related