Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSE 326 Binary Search Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001 Binary Trees Binary tree is A a root left subtree (maybe empty) right subtree (maybe empty) B C D Properties E max # of leaves: max # of nodes: average depth for N nodes: Representation: Data F G I H J left right pointer pointer Binary Search Trees CSE 326 Autumn 2001 2 Binary Tree Representation A A left right child child B B C left right child child left right child child D E F left right child child left right child child left right child child Binary Search Trees CSE 326 Autumn 2001 D C E F 3 Dictionary ADT Dictionary operations create destroy insert find delete Donald insert l33t haxtor Adrien Roller-blade demon Hannah C++ guru Adrien find(Adrien) Dave Older than dirt Roller-blade demon … Stores values associated with userspecified keys values may be any (homogeneous) type keys may be any (homogeneous) comparable type Binary Search Trees CSE 326 Autumn 2001 4 Dictionary ADT: Used Everywhere Arrays Sets Dictionaries Router tables Page tables Symbol tables C++ structures … Anywhere we need to find things fast based on a key Binary Search Trees CSE 326 Autumn 2001 5 Search ADT Dictionary operations create destroy insert find delete Donald Adrien insert find(Adrien) Adrien Hannah Dave … Stores only the keys keys may be any (homogenous) comparable quickly tests for membership Simplified dictionary, useful for examples (e.g. CSE 326) Binary Search Trees CSE 326 Autumn 2001 6 Dictionary Data Structure: Requirements Fast insertion runtime: Fast searching runtime: Fast deletion runtime: Binary Search Trees CSE 326 Autumn 2001 7 Naïve Implementations unsorted array sorted array linked list insert O(n) find + O(n) O(1) find O(n) O(log n) delete find + O(1) find + O(1) find + O(1) O(n) (mark-as-deleted) (mark-as-deleted) Binary Search Trees CSE 326 Autumn 2001 8 Binary Search Tree Dictionary Data Structure Binary tree property each node has 2 children result: 8 storage is small operations are simple average depth is small Search tree property all keys in left subtree smaller than root’s key all keys in right subtree larger than root’s key result: easy to find any given key Insert/delete by changing links Binary Search Trees CSE 326 Autumn 2001 5 2 11 6 4 10 7 12 9 14 13 9 Example and Counter-Example 8 5 5 4 8 2 1 11 7 7 6 10 18 11 4 15 20 3 BINARY SEARCH TREE Binary Search Trees NOT A BINARY SEARCH TREE CSE 326 Autumn 2001 10 21 Complete Binary Search Tree Complete binary search tree (aka binary heap): Links are completely filled, 8 except possibly bottom level, which is filled left-to-right. 5 15 3 1 Binary Search Trees CSE 326 Autumn 2001 7 4 9 17 6 11 In-Order Traversal 10 5 visit left subtree visit node visit right subtree 15 2 9 7 20 What does this guarantee with a BST? 17 30 In order listing: 25791015172030 Binary Search Trees CSE 326 Autumn 2001 12 Recursive Find 10 5 15 2 9 7 20 17 30 Runtime: Best-worse case? Worst-worse case? f(depth)? Binary Search Trees Node * find(Comparable key, Node * t) { if (t == NULL) return t; else if (key < t->key) return find(key, t->left); else if (key > t->key) return find(key, t->right); else return t; } CSE 326 Autumn 2001 13 Iterative Find 10 5 15 2 9 7 20 17 30 Node * find(Comparable key, Node * t) { while (t != NULL && t->key != key) { if (key < t->key) t = t->left; else t = t->right; } return t; } Binary Search Trees CSE 326 Autumn 2001 14 Insert Concept: Proceed down tree as in Find If new key not found, then insert a new node at last spot traversed void insert(Comparable x, Node * t) { if ( t == NULL ) { t = new Node(x); } else if (x < t->key) { insert( x, t->left ); } else if (x > t->key) { insert( x, t->right ); } else { // duplicate // handling is app-dependent } Binary Search Trees CSE 326 Autumn 2001 15 BuildTree for BSTs Suppose the data 1, 2, 3, 4, 5, 6, 7, 8, 9 is inserted into an initially empty BST: in order in reverse order median first, then left median, right median, etc. Binary Search Trees CSE 326 Autumn 2001 16 Analysis of BuildTree Worst case is O(n2) 1 + 2 + 3 + … + n = O(n2) Average case assuming all orderings equally likely: O(n log n) averaging over all insert sequences (not over all binary trees) equivalently: average depth of a node is log n proof: see Introduction to Algorithms, Cormen, Leiserson, & Rivest Binary Search Trees CSE 326 Autumn 2001 17 BST Bonus: FindMin, FindMax Find minimum 10 5 15 Find maximum 2 9 7 Binary Search Trees CSE 326 Autumn 2001 20 17 30 18 Successor Node Next larger node in this node’s subtree Node * succ(Node * t) { if (t->right == NULL) return NULL; else return min(t->right); } 10 5 15 2 9 20 17 30 7 How many children can the successor of a node have? Binary Search Trees CSE 326 Autumn 2001 19 Predecessor Node Next smaller node in this node’s subtree Node * pred(Node * t) { if (t->left == NULL) return NULL; else return max(t->left); } Binary Search Trees CSE 326 Autumn 2001 10 5 15 2 9 20 17 30 7 20 Deletion 10 5 15 2 9 7 20 17 30 Why might deletion be harder than insertion? Binary Search Trees CSE 326 Autumn 2001 21 Lazy Deletion Instead of physically deleting nodes, just mark them as deleted + simpler + physical deletions done in batches + some adds just flip deleted flag - extra memory for deleted flag many lazy deletions slow finds some operations may have to be modified (e.g., min and max) 10 5 15 2 9 20 17 30 7 Binary Search Trees CSE 326 Autumn 2001 22 Lazy Deletion Delete(17) 10 Delete(15) 5 Delete(5) Find(9) 15 2 9 20 Find(16) 7 17 30 Insert(5) Find(17) Binary Search Trees CSE 326 Autumn 2001 23 Deletion - Leaf Case 10 Delete(17) 5 15 2 9 7 Binary Search Trees CSE 326 Autumn 2001 20 17 30 24 Deletion - One Child Case 10 Delete(15) 5 15 2 9 7 Binary Search Trees CSE 326 Autumn 2001 20 30 25 Deletion - Two Child Case Replace node with descendant whose value is guaranteed to be between left and right subtrees: the successor 10 5 20 2 Delete(5) 9 30 7 Could we have used predecessor instead? Binary Search Trees CSE 326 Autumn 2001 26 Delete Code void delete(Comparable key, Node *& root) { Node *& handle(find(key, root)); Node * toDelete = handle; if (handle != NULL) { if (handle->left == NULL) { // Leaf or one child handle = handle->right; delete toDelete; } else if (handle->right == NULL) { // One child handle = handle->left; delete toDelete; } else { // Two children successor = succ(root); handle->data = successor->data; delete(successor->data, handle->right); } } } Binary Search Trees CSE 326 Autumn 2001 27 Thinking about Binary Search Trees Observations Each operation views two new elements at a time Elements (even siblings) may be scattered in memory Binary search trees are fast if they’re shallow Realities For large data sets, disk accesses dominate runtime Some deep and some shallow BSTs exist for any data Binary Search Trees CSE 326 Autumn 2001 28 Beauty is Only (log n) Deep Binary Search Trees are fast if they’re shallow: perfectly complete complete – possibly missing some “fringe” (leaves) any other good cases? What matters? Problems occur when one branch is much longer than another i.e. when tree is out of balance Binary Search Trees CSE 326 Autumn 2001 29 Dictionary Implementations unsorted array insert O(n) sorted linked array list find + O(n) O(1) BST find O(log n) O(Depth) O(n) O(n) O(Depth) delete find + O(1) find + O(1) find + O(1) O(Depth) (mark-as-deleted) (mark-as-deleted) BST’s looking good for shallow trees, i.e. if Depth is small (log n); otherwise as bad as a linked list! Binary Search Trees CSE 326 Autumn 2001 30 Digression: Tail Recursion Tail recursion: when the tail (final operation) of a function recursively calls the function Why is tail recursion especially bad with a linked list? Why might it be a lot better with a tree? Why might it not? Binary Search Trees CSE 326 Autumn 2001 31 Making Trees Efficient: Possible Solutions Keep BSTs shallow by maintaining “balance” AVL trees … also exploit most-recently-used (mru) info Splay trees Reduce disk access by increasing branching factor B-trees Binary Search Trees CSE 326 Autumn 2001 32