240 likes | 351 Views
Binary Search Trees II. Chapter 6. Objectives. You will be able to use a binary search tree template with your own classes. Getting Started. Download project from last class: http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/2011_03_07_Binary_Search_Tree/ File BST_Demo.zip
E N D
Binary Search Trees II Chapter 6
Objectives • You will be able to use a binary search tree template with your own classes.
Getting Started • Download project from last class: • http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/2011_03_07_Binary_Search_Tree/ File BST_Demo.zip • Build and run.
Add Display() • Let's add a function to display the tree showing its structure. • Not in the book. • For ease of coding • Root at left side of the screen. • Successive levels indented to the right. • Right child will be above a node. • Left child will be below. • This permits a simple recursive implementation.
genBST1.h #include <iostream> #include <iomanip> ... public: ... // Display the tree on the screen in graphical format void display(std::ostream & out) const {display(out, 0, root);}; protected: // Display any subtree in graphical format void display(std::ostream & out, int indent, const BSTNode<T>* subtreeRoot) const;
genBST1.h // Display any subtree in graphical format template<class T> void BST<T>::display(ostream & out, int indent, const BSTNode<T>* subtreeRoot) const { if (subtreeRoot != 0) { display(out, indent + 8, subtreeRoot->right); out << setw(indent) << " " << subtreeRoot->key << endl << endl; display(out, indent + 8, subtreeRoot->left); } }
main.cpp #include <iostream> ... cout << endl << endl; my_BST.display(cout); cin.get(); return 0; }
Inserting Nodes in Different Order • What if we had added the items in increasing numerical order? my_BST.insert(2); my_BST.insert(10); my_BST.insert(12); my_BST.insert(13); my_BST.insert(20); my_BST.insert(25); my_BST.insert(29); my_BST.insert(31);
A Lopsided Tree • What will be the search time in this tree? • Efficiency of BST depends on keeping the tree reasonably well balanced. • A lopsided tree is no better than a linked list. Replace original insert code. End of Section
Tree Traversal • Visit each node of the tree exactly once. • Many possible orders. • Only a few are of practical interest. • Broad categories: • Depth first • Breadth first
Depth First Traversal • Three Versions • Inorder • LNR: Left Subtree, Node, Right Subtree • Preorder • NLR: Node, Left Subtree, Right Subtree • Postorder • LRN: Left Subtree, Right Subree, Node • Name ("In", "Pre", "Post") indicates where the Node is visited relative to its subtrees.
genBST1.h public: ... // Traversal methods void preorder() { preorder(root); } void inorder() { inorder(root); } void postorder() { postorder(root);} protected: ... void preorder(BSTNode<T>*); void inorder(BSTNode<T>*); void postorder(BSTNode<T>*); virtual void visit(BSTNode<T>* p) { cout << p->key << ' '; } Why virtual?
Preorder Traversal At end of genBST1.h: template<class T> void BST<T>::preorder(BSTNode<T> *p) { if (p != 0) { visit(p); preorder(p->left); preorder(p->right); } }
Using Preorder Traversal At end of main.cpp: cout << endl << endl << "Preorder traversal: " << endl; my_BST.preorder(); cout << endl;
Inorder Traversal At end of genBST1.h: template<class T> void BST<T>::inorder(BSTNode<T> *p) { if (p != 0) { inorder(p->left); visit(p); inorder(p->right); } }
Using Inorder Traversal In main.cpp: cout << endl << endl << "Inorder traversal: " << endl; my_BST.inorder(); cout << endl;
Inorder Traversal Output Note that elements are in numerical order.
Postorder Traversal At end of genBST1.h: template<class T> void BST<T>::postorder(BSTNode<T>* p) { if (p != 0) { postorder(p->left); postorder(p->right); visit(p); } }
Using Postorder Traversal In main.cpp: cout << endl << endl << "Postorder traversal: " << endl; my_BST.postorder(); cout << endl;
Postorder Traversal Output End of Section