ceiling function for binary search tree Key ceiling(const Key& x) Returns the smallest key in the tree that is >= x Throws logic_error if all keys in the tree are < x
Example ceiling(45)? ceiling(15)? ceiling(5)?
Example 45 ceiling(45) =
Example 15 ceiling(15) =
Example 5 ceiling(5) = 10
Example 90 ceiling(90)?
Example ceiling(5) Search for 5 Save pointer to the smallest value >= 5 as you search
Example ceiling(5) Search for 5 Save pointer to the smallest value >= 5 as you search
Example ceiling(5) Search for 5 Save pointer to the smallest value >= 5 as you search
Example ceiling(5) Search for 5 Save pointer to the smallest value >= 5 as you search
Example 5 ceiling(5) = 10 Search for 5 Save pointer to the smallest value >= 5 as you search If not found, the last value saved is the ceiling
Example ceiling(35)? Search for 35 Save a pointer to the smallest value >= 35 as you search
Example ceiling(35)? Search for 35 Save a pointer to the smallest value >= 35 as you search
Example ceiling(35)? Search for 35 Save a pointer to the smallest value >= 35 as you search Don t save, it isn t >= 35
Example ceiling(35)? Search for 35 Save a pointer to the smallest value >= 35 as you search Don t save 30, it isn t >= 35
Example 35 ceiling(35) = 40 Search for 35 Save a pointer to the smallest value >= 35 as you search If not found, the last value saved is the ceiling.
private ceiling function Key ceiling(const Key& k, Node *saved, Node *rt) { } Called recursively. If k is < rt->key,?? if k is > rt->key?? if rt is NULL??
public ceiling function Key ceiling(const Key& k) { Node *p = ceiling(k, NULL, root); if (p == NULL) { throw logic_error( no ceiling if key is > all existing keys ); } return p->key; }
Iterator for binary search tree How can we implement the two iterator members of BST iterator beginkey() iterator endkey()
A print member function to print all the values in a binary search tree Write a recursive private print and a public print
Private print method void print(node *rt) { { if (rt == NULL) return; print(rt->left); cout << rt-> key << endl; print(rt->right); } 10 30
Public print method void print() { { print(root); } 10 30
preorder inorder postorder Binary Tree Traversals
Binary Tree Traversals preorder visit the root visit the left subtree (in preorder) visit the right subtree (in preorder)
Binary Tree Traversals inorder visit the left subtree (in inorder) visit the root visit the right subtree (in inorder)
Binary Tree Traversals postorder visit the left subtree (in postorder) visit the right subtree (in postorder) visit the root
Preorder Example List values in preorder: root, left, right, 40, 10 30
Inorder Example List values in inorder: left (in inorder), root, right (in inorder) First item: not the root. First do the left subtree in inorder: left, root, right but not 40, the root of left subtree 10 30
Inorder and binary search trees List values in inorder: 10 30 40 60 10 30 For a binary search tree, inorder lists the values in their sorted order. preorder and postorder do not.
Iterators! But the print member ONLY allows printing and prints ALL values. The iterator members let the application decide what to do with the values in the binary search tree and so is much more useful.
iterator member functions of BST version 1 class BST { Node *root; int sz; vector<e> v; // for iterator member function. public: typedef typename vector<e>::iterator iterator; iterator keysbegin() { v.clear(); visitnodes(root); return v.begin(); }
private visitnodes member template<typename Key, typename Value> class BST { Node *root; int sz; vector<key> v; // for iterator member function. void visitnodes(node *rt) { if (rt == NULL) return; visitnodes(rt->left); v.push_back(rt->key); visitnodes(rt->right); } public: };
Advantages/Disadvantages of version 1 Advantage: Easy to implement (no iterator class needed) Disadvantage: Can t have two or more iterators active for same BST (only one vector member)
Iterator class next Key Next key after 30? After 40? After?
Iterator class next Key If current Node is 30, we need to have saved some values that would be examined when searching for 30 that are > 30 What are they?