Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Binary Search Trees II Chapter 6. 2 Objectives You will be able to use a binary search tree template with your own classes.

Similar presentations


Presentation on theme: "1 Binary Search Trees II Chapter 6. 2 Objectives You will be able to use a binary search tree template with your own classes."— Presentation transcript:

1 1 Binary Search Trees II Chapter 6

2 2 Objectives You will be able to use a binary search tree template with your own classes.

3 3 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 http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/ 2011_03_07_Binary_Search_Tree/ Build and run.

4 4 Program Running

5 5 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.

6 6 genBST1.h #include... 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 * subtreeRoot) const;

7 7 genBST1.h // Display any subtree in graphical format template void BST ::display(ostream & out, int indent, const BSTNode * subtreeRoot) const { if (subtreeRoot != 0) { display(out, indent + 8, subtreeRoot->right); out key << endl << endl; display(out, indent + 8, subtreeRoot->left); }

8 8 main.cpp #include... cout << endl << endl; my_BST.display(cout); cin.get(); return 0; }

9 9 Display Method Output

10 10 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);

11 11 A Lopsided Tree

12 12 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

13 13 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

14 14 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.

15 15 genBST1.h public:... // Traversal methods void preorder() { preorder(root); } void inorder() { inorder(root); } void postorder() { postorder(root);} protected:... void preorder(BSTNode *); void inorder(BSTNode *); void postorder(BSTNode *); virtual void visit(BSTNode * p) { cout key << ' '; } Why virtual?

16 16 Preorder Traversal At end of genBST1.h: template void BST ::preorder(BSTNode *p) { if (p != 0) { visit(p); preorder(p->left); preorder(p->right); }

17 17 Using Preorder Traversal At end of main.cpp: cout << endl << endl << "Preorder traversal: " << endl; my_BST.preorder(); cout << endl;

18 18 Output from Preorder Traversal

19 19 Inorder Traversal At end of genBST1.h: template void BST ::inorder(BSTNode *p) { if (p != 0) { inorder(p->left); visit(p); inorder(p->right); }

20 20 Using Inorder Traversal In main.cpp: cout << endl << endl << "Inorder traversal: " << endl; my_BST.inorder(); cout << endl;

21 21 Inorder Traversal Output Note that elements are in numerical order.

22 22 Postorder Traversal At end of genBST1.h: template void BST ::postorder(BSTNode * p) { if (p != 0) { postorder(p->left); postorder(p->right); visit(p); }

23 23 Using Postorder Traversal In main.cpp: cout << endl << endl << "Postorder traversal: " << endl; my_BST.postorder(); cout << endl;

24 24 Postorder Traversal Output End of Section


Download ppt "1 Binary Search Trees II Chapter 6. 2 Objectives You will be able to use a binary search tree template with your own classes."

Similar presentations


Ads by Google