Binary Search Trees What is a binary tree
Binary Search Trees
What is a binary tree? • Property 1: each node can have up to two successor nodes.
What is a binary tree? (cont. ) • Property 2: a unique path exists from the root to every other node Not a valid binary tree!
Some terminology • • The successor nodes of a node are called its children The predecessor node of a node is called its parent The "beginning" node is called the root (has no parent) A node without children is called a leaf
Some terminology (cont’d) • Nodes are organize in levels (indexed from 0). • Level (or depth) of a node: number of edges in the path from the root to that node. • Height of a tree h: #levels = L (Warning: some books define h as #levels-1). • Full tree: every node has exactly two children and all the leaves are on the same level. not full!
What is the max #nodes at some level l? The max #nodes at level is where l=0, 1, 2, . . . , L-1
What is the total #nodes N of a full tree with height h? l=0 l=1 l=h-1 using the geometric series:
What is the height h of a full tree with N nodes?
Why is h important? • Tree operations (e. g. , insert, delete, retrieve etc. ) are typically expressed in terms of h. • So, h determines running time!
• What is the max height of a tree with max height N nodes? N (same as a linked list) • What is the min height of a tree with min height N nodes? log(N+1)
How to search a binary tree? (1) Start at the root (2) Search the tree level by level, until you find the element you are searching for or you reach a leaf. Is this better than searching a linked list? No O(N)
Binary Search Trees (BSTs) Search • Binary Search Tree Property: The value stored at a node is greater than the value stored at its left child and less than the value stored at its right child
Binary Search Trees (BSTs) Search In a BST, the value stored at the root of a subtree is greater than any value in its left subtree and less than any value in its right subtree!
Binary Search Trees (BSTs) Search Where is the smallest element? Ans: leftmost element Where is the largest element? Ans: rightmost element
How to search a binary search tree? (1) Start at the root (2) Compare the value of the item you are searching for with the value stored at the root (3) If the values are equal, then item found; otherwise, if it is a leaf node, then not found
How to search a binary search tree? (4) If it is less than the value stored at the root, then search the left subtree (5) If it is greater than the value stored at the root, then search the right subtree (6) Repeat steps 2 -6 for the root of the subtree chosen in the previous step 4 or 5
How to search a binary search tree? Is this better than searching a linked list? Yes !! ---> O(log. N)
Tree node structure template<class Item. Type> struct Tree. Node<Item. Type> { Item. Type info; Tree. Node<Item. Type>* left; Tree. Node<Item. Type>* right; };
Binary Search Tree Specification #include <fstream. h> struct Tree. Node<Item. Type>; enum Order. Type {PRE_ORDER, IN_ORDER, POST_ORDER}; template<class Item. Type> class Tree. Type { public: Tree. Type(); ~Tree. Type(); Tree. Type(const Tree. Type<Item. Type>&); void operator=(const Tree. Type<Item. Type>&); void Make. Empty(); bool Is. Empty() const; bool Is. Full() const; int Number. Of. Nodes() const;
Binary Search Tree Specification void Retrieve. Item(Item. Type&, bool& found); void Insert. Item(Item. Type); void Delete. Item(Item. Type); void Reset. Tree(Order. Type); void Get. Next. Item(Item. Type&, Order. Type, bool&); void Print. Tree(ofstream&) const; private: Tree. Node<Item. Type>* root; }; (cont. )
Function Number. Of. Nodes • Recursive implementation #nodes in a tree = #nodes in left subtree + #nodes in right subtree + 1 • What is the size factor? Number of nodes in the tree we are examining • What is the base case? The tree is empty • What is the general case? Count. Nodes(Left(tree)) + Count. Nodes(Right(tree)) + 1
Function Number. Of. Nodes (cont. ) template<class Item. Type> int Tree. Type<Item. Type>: : Number. Of. Nodes() const { return Count. Nodes(root); } template<class Item. Type> int Count. Nodes(Tree. Node<Item. Type>* tree) { Running Time? if (tree == NULL) O(N) return 0; else return Count. Nodes(tree->left) + Count. Nodes(tree->right) + 1; }
Function Retrieve. Item
Function Retrieve. Item • • • What is the size of the problem? Number of nodes in the tree we are examining What is the base case(s)? 1) When the key is found 2) The tree is empty (key was not found) What is the general case? Search in the left or right subtrees
Function Retrieve. Item (cont. ) template <class Item. Type> void Tree. Type<Item. Type>: : Retrieve. Item(Item. Type& item, bool& found) { Retrieve(root, item, found); } template<class Item. Type> void Retrieve(Tree. Node<Item. Type>* tree, Item. Type& item, bool& found) { if (tree == NULL) // base case 2 found = false; else if(item < tree->info) Retrieve(tree->left, item, found); else if(item > tree->info) Retrieve(tree->right, item, found); else { // base case 1 item = tree->info; found = true; } } Running Time? O(h)
Function Insert. Item • Use the binary search tree property to insert the new item at the correct place
Function Insert. Item (cont. ) • Implementing insertion resursively e. g. , insert 11
Function Insert. Item (cont. ) • What is the size of the problem? Number of nodes in the tree we are examining • What is the base case(s)? The tree is empty • What is the general case? Choose the left or right subtree
Function Insert. Item (cont. ) template<class Item. Type> void Tree. Type<Item. Type>: : Insert. Item(Item. Type item) { Insert(root, item); } template<class Item. Type> void Insert(Tree. Node<Item. Type>*& tree, Item. Type item) { if(tree == NULL) { // base case tree = new Tree. Node<Item. Type>; tree->right = NULL; tree->left = NULL; Running Time? tree->info = item; } O(h) else if(item < tree->info) Insert(tree->left, item); else Insert(tree->right, item); }
Function Insert. Item (cont. ) Insert 11
Does the order of inserting elements into a tree matter? • Yes, certain orders might produce very unbalanced trees!
Does the order of inserting elements into a tree matter? (cont. )
Does the order of inserting elements into a tree matter? (cont’d) • Unbalanced trees are not desirable because • search time increases! Advanced tree structures, such as red-black trees, guarantee balanced trees.
Function Delete. Item • First, find the item; then, delete it • Binary search tree property must be • preserved!! We need to consider three different cases: (1) Deleting a leaf (2) Deleting a node with only one child (3) Deleting a node with two children
(1) Deleting a leaf
(2) Deleting a node with only one child
(3) Deleting a node with two children
(3) Deleting a node with two children (cont. ) • Find predecessor (i. e. , rightmost node in the • • left subtree) Replace the data of the node to be deleted with predecessor's data Delete predecessor node
Function Delete. Item (cont. ) • What is the size of the problem? Number of nodes in the tree we are examining • What is the base case(s)? Key to be deleted was found • What is the general case? Choose the left or right subtree
Function Delete. Item (cont. ) template<class Item. Type> void Tree. Type<Itme. Type>: : Delete. Item(Item. Type item) { Delete(root, item); } template<class Item. Type> void Delete(Tree. Node<Item. Type>*& tree, Item. Type item) { if(item < tree->info) Delete(tree->left, item); else if(item > tree->info) Delete(tree->right, item); else Delete. Node(tree); }
Function Delete. Item (cont. ) template <class Item. Type> void Delete. Node(Tree. Node<Item. Type>*& tree) { Item. Type item; Tree. Node<Item. Type>* temp. Ptr; temp. Ptr = tree; if(tree->left == NULL) { // right child tree = tree->right; 0 children or delete temp. Ptr; } 1 child else if(tree->right == NULL) { // left child tree = tree->left; 0 children or delete temp. Ptr; } 1 child else { Get. Predecessor(tree->left, item); tree->info = item; 2 children Delete(tree->left, item); } }
Function Delete. Item (cont. ) template<class Item. Type> void Get. Predecessor(Tree. Node<Item. Type>* tree, Item. Type& item) { while(tree->right != NULL) tree = tree->right; item = tree->info; }
Function Delete. Item (cont. ) template<class Item. Type> void Tree. Type<Itme. Type>: : Delete. Item(Item. Type item) { Delete(root, item); } template<class Item. Type> void Delete(Tree. Node<Item. Type>*& tree, Item. Type item) { if(item < tree->info) Delete(tree->left, item); else if(item > tree->info) Running Time? Delete(tree->right, item); else Delete. Node(tree); O(h) }
Function Delete. Item (cont. )
Tree Traversals There are mainly three ways to traverse a tree: 1) Inorder Traversal 2) Postorder Traversal 3) Preorder Traversal
Inorder Traversal: A E H J M T Y Visit second tree ‘J’ ‘T’ ‘E’ ‘A’ ‘H’ Visit left subtree first ‘M’ ‘Y’ Visit right subtree last 46
Inorder Traversal • Visit the nodes in the left subtree, then visit the root of the tree, then visit the nodes in the right subtree Inorder(tree) If tree is not NULL Inorder(Left(tree)) Visit Info(tree) Inorder(Right(tree)) Warning: "visit" implies do something with the value at Warning the node (e. g. , print, save, update etc. ).
Preorder Traversal: J E A H T M Y Visit first tree ‘J’ ‘T’ ‘E’ ‘A’ ‘H’ Visit left subtree second ‘M’ ‘Y’ Visit right subtree last 48
Preorder Traversal • Visit the root of the tree first, then visit the nodes in the left subtree, then visit the nodes in the right subtree Preorder(tree) If tree is not NULL Visit Info(tree) Preorder(Left(tree)) Preorder(Right(tree))
Postorder Traversal: A H E M Y T J Visit last tree ‘J’ ‘T’ ‘E’ ‘A’ ‘H’ Visit left subtree first ‘M’ ‘Y’ Visit right subtree second 50
Postorder Traversal • Visit the nodes in the left subtree first, then visit the nodes in the right subtree, then visit the root of the tree Postorder(tree) If tree is not NULL Postorder(Left(tree)) Postorder(Right(tree)) Visit Info(tree)
Tree Traversals: another example
Function Print. Tree • We use "inorder" to print out the node values. • Keys will be printed out in sorted order. • Hint: binary search could be used for sorting! A D J M Q R T
Function Print. Tree (cont. ) void Tree. Type: : Print. Tree(ofstream& out. File) { Print(root, out. File); } template<class Item. Type> void Print(Tree. Node<Item. Type>* tree, ofstream& out. File) { if(tree != NULL) { Print(tree->left, out. File); out. File << tree->info; // “visit” Print(tree->right, out. File); } } to overload “<<“ or “>>”, see: http: //www. fredosaurus. com/notes-cpp/oop-friends/overload-io. html
Class Constructor template<class Item. Type> Tree. Type<Item. Type>: : Tree. Type() { root = NULL; }
Class Destructor How should we delete the nodes of a tree? Use postorder! Delete the tree in a "bottom-up" fashion
Class Destructor (cont’d) Tree. Type: : ~Tree. Type() { Destroy(root); } void Destroy(Tree. Node<Item. Type>*& tree) { if(tree != NULL) { Destroy(tree->left); Destroy(tree->right); delete tree; // “visit” } } postorder
Copy Constructor How should we create a copy of a tree? Use preorder!
Copy Constructor (cont’d) template<class Item. Type> Tree. Type<Item. Type>: : Tree. Type(const Tree. Type<Item. Type>& original. Tree) { Copy. Tree(root, original. Tree. root); } template<class Item. Type) void Copy. Tree(Tree. Node<Item. Type>*& copy, Tree. Node<Item. Type>* original. Tree) { if(original. Tree == NULL) copy = NULL; else { copy = new Tree. Node<Item. Type>; // “visit” copy->info = original. Tree->info; Copy. Tree(copy->left, original. Tree->left); Copy. Tree(copy->right, original. Tree->right); } } preorder
Reset. Tree and Get. Next. Item • • • User needs to specify the tree traversal order. For efficiency, Reset. Tree stores in a queue the results of the specified tree traversal. Then, Get. Next. Item, dequeues the node values from the queue. void Reset. Tree(Order. Type); void Get. Next. Item(Item. Type&, Order. Type, bool&);
Revise Tree Class Specification enum Order. Type {PRE_ORDER, IN_ORDER, POST_ORDER}; template<class Item. Type> class Tree. Type { new member public: functions // previous member functions void Pre. Order(Tree. Node<Item. Type>, Que. Type<Item. Type>&) void In. Order(Tree. Node<Item. Type>, Que. Type<Item. Type>&) void Post. Order(Tree. Node<Item. Type>, Que. Type<Item. Type>&) private: Tree. Node<Item. Type>* root; Que. Type<Item. Type> pre. Que; new private data Que. Type<Item. Type> in. Que; Que. Type<Item. Type> post. Que; };
Reset. Tree and Get. Next. Item (cont. ) template<class Item. Type> void Pre. Order(Tree. Node<Item. Type>tree, Que. Type<Item. Type>& pre. Que) { if(tree != NULL) { pre. Que. Enqueue(tree->info); // “visit” Pre. Order(tree->left, pre. Que); Pre. Order(tree->right, pre. Que); } }
Reset. Tree and Get. Next. Item (cont. ) template<class Item. Type> void In. Order(Tree. Node<Item. Type>tree, Que. Type<Item. Type>& in. Que) { if(tree != NULL) { In. Order(tree->left, in. Que); in. Que. Enqueue(tree->info); // “visit” In. Order(tree->right, in. Que); } }
Reset. Tree and Get. Next. Item (cont. ) template<class Item. Type> void Post. Order(Tree. Node<Item. Type>tree, Que. Type<Item. Type>& post. Que) { if(tree != NULL) { Post. Order(tree->left, post. Que); Post. Order(tree->right, post. Que); post. Que. Enqueue(tree->info); // “visit” } }
Reset. Tree template<class Item. Type> void Tree. Type<Item. Type>: : Reset. Tree(Order. Type order) { switch(order) { case PRE_ORDER: Pre. Order(root, pre. Que); break; case IN_ORDER: In. Order(root, in. Que); break; case POST_ORDER: Post. Order(root, post. Que); break; } }
Get. Next. Item template<class Item. Type> void Tree. Type<Item. Type>: : Get. Next. Item(Item. Type& item, Order. Type order, bool& finished) { finished = false; switch(order) { case PRE_ORDER: pre. Que. Dequeue(item); if(pre. Que. Is. Empty()) finished = true; break; case IN_ORDER: in. Que. Dequeue(item); if(in. Que. Is. Empty()) finished = true; break; case POST_ORDER: post. Que. Dequeue(item); if(post. Que. Is. Empty()) finished = true; break; } }
Iterative Insertion and Deletion • Reading Assignment (see textbook)
Comparing Binary Search Trees to Linear Lists Big-O Comparison Binary Array-based Operation List Search Tree Constructor O(1) Linked List O(1) Destructor O(N) O(1) O(N) Is. Full O(1) Is. Empty O(1) Retrieve. Item O(log. N)* O(log. N) O(N) Insert. Item O(log. N)* O(N) Delete. Item O(log. N)* O(N) *assuming h=O(log. N)
Exercises 37 -41 (p. 539)
Exercise 17 (p. 537)
Exercise 18 (p. 537)
- Slides: 71