270 likes | 412 Views
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
E N D
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 Nodes in right subtree have keys > the root’s key. K >K <K
Searching M B Q J O U
Add as a leaf M B Q J O U
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
Inorder Traversal void printTree( TreeNode root) if ( root != null ) printTree( root.getLeft () ); println(root.getRootItem()); printTree( root.getRight() ); } M B Q J O U
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
Preorder Traversal void printTree( TreeNode root) if ( root != null ) println( root.getRootItem()); printTree( root.getLeft () ); printTree( root.getRight() ); } M B Q J O U
Postorder Traversal void printTree( TreeNode root) if ( root != null ) printTree( root.getLeft () ); printTree( root.getRight() ); println(root.getRootItem()); } M B Q J O U
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
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
BST Delete M B Q J O U
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
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.
M B Q J O U BST Efficiency
M B Q U J O U Q O BST Efficiency M J B
M B Q U J O U Q O Average Case? M J B
Full and Complete Trees M Q G O U B J A full tree
Full and Complete Trees M M Q Q G G O U B J B A full tree A complete tree
Minimising Tree Height M B Q A J O
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
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
ADT Table Unsorted Array Sorted Array Program that uses a priority queue Sorted Linked List ADT Priority Queue Unsorted Linked List Binary Search Tree
BST’s vs Heaps M U Q J G Q G U B J B M BST heap
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
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
BST’s vs Heaps M U Q J G Q G U B J B M BST heap