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

Post on 17-Dec-2015

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

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.

4

Program Running

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

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;

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

}

}

8

main.cpp

#include <iostream>

...

cout << endl << endl;

my_BST.display(cout);

cin.get();

return 0;

}

9

Display Method Output

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

A Lopsided Tree

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

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

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

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?

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

}

}

17

Using Preorder Traversal

At end of main.cpp:

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

my_BST.preorder();

cout << endl;

18

Output from Preorder Traversal

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

}

}

20

Using Inorder Traversal

In main.cpp:

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

my_BST.inorder();

cout << endl;

21

Inorder Traversal Output

Note that elements are in numerical order.

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

}

}

23

Using Postorder Traversal

In main.cpp:

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

my_BST.postorder();

cout << endl;

24

Postorder Traversal Output

End of Section

top related