Top Banner
1 Binary Search Trees II Chapter 6
24

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

Dec 17, 2015

Download

Documents

Hilary Copeland
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Binary Search Trees II Chapter 6. 2 Objectives You will be able to use a binary search tree template with your own classes.

1

Binary Search Trees II

Chapter 6

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

2

Objectives

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

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

3

Getting Started

Download project from last class: http://www.cse.usf.edu/~turnerr/Data_Structures/Down

loads/2011_03_07_Binary_Search_Tree/ File BST_Demo.zip

Build and run.

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

4

Program Running

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

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.

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

6

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;

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

7

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

}

}

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

8

main.cpp

#include <iostream>

...

cout << endl << endl;

my_BST.display(cout);

cin.get();

return 0;

}

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

9

Display Method Output

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

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

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

11

A Lopsided Tree

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

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

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

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

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

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.

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

15

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?

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

16

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

}

}

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

17

Using Preorder Traversal

At end of main.cpp:

cout << endl << endl << "Preorder traversal: " << endl;

my_BST.preorder();

cout << endl;

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

18

Output from Preorder Traversal

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

19

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

}

}

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

20

Using Inorder Traversal

In main.cpp:

cout << endl << endl << "Inorder traversal: " << endl;

my_BST.inorder();

cout << endl;

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

21

Inorder Traversal Output

Note that elements are in numerical order.

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

22

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

}

}

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

23

Using Postorder Traversal

In main.cpp:

cout << endl << endl << "Postorder traversal: " << endl;

my_BST.postorder();

cout << endl;

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

24

Postorder Traversal Output

End of Section