200 likes | 363 Views
CompSci 105 SS 2005 Principles of Computer Science. Lecture 22: Efficiency of BST Operations. Lecturer: Santokh Singh. Inorder Traversal. void printTree( TreeNode root) if ( root != null ) printTree( root.getLeft () ); println(root.getRootItem()); printTree( root.getRight() ); }.
E N D
CompSci 105 SS 2005 Principles of Computer Science Lecture 22: Efficiency of BST Operations Lecturer: Santokh Singh
Inorder Traversal void printTree( TreeNode root) if ( root != null ) printTree( root.getLeft () ); println(root.getRootItem()); printTree( root.getRight() ); } M B Q J O U Textbook, p. 434
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 Textbook, p. 462
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.
Treesort Please refer to the prescribed Textbook, pages 478-479, now.
ADT Table Textbook, p. 498-499
ADT Table Operations void create () int length() void insert( item) void delete( key ) item find(key) Textbook, p. 498-499
ADT Table Unsorted Array Sorted Array Program that uses a table Sorted Linked List ADT Table Unsorted Linked List Binary Search Tree Textbook, p. 504-517
M B Q J O U BST Efficiency Textbook, p. 474-478
M B Q U J O U Q O BST Efficiency M J B Textbook, p. 474-478
M B Q U J O U Q O Average Case? M J B Textbook, p. 474-478
Full and Complete Trees M Q G O U B J A full tree Textbook, p. 427
Full and Complete Trees M M Q Q G G O U B J B A full tree A complete tree Textbook, p. 427
Minimising Tree Height M B Q A J O Textbook, p. 480-481
ADT Priority Queue • To do: • Study for 105 Exam • Play • Eat • 4. Sleep Textbook, p. 517
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
ADT Table Unsorted Array Sorted Array Program that uses a priority queue Sorted Linked List ADT Priority Queue Unsorted Linked List Binary Search Tree
Heap Like a binary search tree, but • Always keep it a complete tree • Don’t completely sort data Textbook, p. 520
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
BST’s vs Heaps M U Q J G Q G U B J B M BST heap