• No results found

C++ Plus Data Structures

N/A
N/A
Protected

Academic year: 2021

Share "C++ Plus Data Structures"

Copied!
38
0
0

Loading.... (view fulltext now)

Full text

(1)

Nell Dale Chapter 8

Binary Search Trees

Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus

C++ Plus Data

Structures

(2)

Jake’s Pizza Shop

Owner Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

(3)

Owner Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

A Tree Has a Root Node

ROOT NODE

(4)

Owner Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Leaf nodes have no children

(5)

Owner Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

A Tree Has Levels

LEVEL 0

(6)

Owner Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Level One

LEVEL 1

(7)

Owner Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Level Two

LEVEL 2

(8)

Owner Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

A Subtree

(9)

Owner Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Another Subtree

RIGHT SUBTREE OF ROOT NODE

(10)

A binary tree is a structure in which:

Each node can have at most two children, and in which a unique path exists from the root to every other node.

The two children of a node are called the left child and the right child, if they exist.

Binary Tree

(11)

A Binary Tree

Q

V

T

K

S

A

E

L

(12)

How many leaf nodes?

Q

V

T

K

A

E

L

(13)

How many descendants of Q?

Q

V

T

K

S

A

E

L

(14)

How many ancestors of K?

Q

V

T

K

A

E

L

(15)

Implementing a Binary Tree with Pointers and Dynamic Data

Q

V

T

K

S

A

E

L

(16)

Each node contains two pointers

template< class ItemType >

struct TreeNode {

ItemType info; // Data member

TreeNode<ItemType>* left; // Pointer to left child TreeNode<ItemType>* right; // Pointer to right child };

(17)

// BINARY SEARCH TREE SPECIFICATION template< class ItemType >

class TreeType {

public:

TreeType ( ); // constructor ~TreeType ( ); // destructor bool IsEmpty ( ) const;

bool IsFull ( ) const;

int NumberOfNodes ( ) const;

void InsertItem ( ItemType item );

void DeleteItem (ItemType item );

void RetrieveItem ( ItemType& item, bool& found );

void PrintTree (ofstream& outFile) const;

. . .

private:

TreeNode<ItemType>* root;

(18)

TreeType<char> CharBST;

‘J’

‘E’

‘A’

‘S’

‘H’

TreeType

~TreeType IsEmpty InsertItem

Private data:

root

RetrieveItem

(19)

A special kind of binary tree in which:

1. Each node contains a distinct data value,

2. The key values in the tree can be compared using

“greater than” and “less than”, and

3. The key value of each node in the tree is

less than every key value in its right subtree, and greater than every key value in its left subtree.

A Binary Search Tree (BST) is . . .

(20)

Depends on its key values and their order of insertion.

Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order.

The first value to be inserted is put into the root node.

Shape of a binary search tree . . .

‘J’

(21)

Thereafter, each value to be inserted begins by comparing itself to the value in the root node,

moving left it is less, or moving right if it is greater.

This continues at each level until it can be inserted as a new leaf.

Inserting ‘E’ into the BST

‘J’

‘E’

(22)

Begin by comparing ‘F’ to the value in the root node, moving left it is less, or moving right if it is greater.

This continues until it can be inserted as a leaf.

Inserting ‘F’ into the BST

‘J’

‘E’

‘F’

(23)

Begin by comparing ‘T’ to the value in the root node, moving left it is less, or moving right if it is greater.

This continues until it can be inserted as a leaf.

Inserting ‘T’ into the BST

‘J’

‘E’

‘F’

‘T’

(24)

Begin by comparing ‘A’ to the value in the root node, moving left it is less, or moving right if it is greater.

This continues until it can be inserted as a leaf.

Inserting ‘A’ into the BST

‘J’

‘E’ ‘T’

(25)

is obtained by inserting

the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order?

What binary search tree . . .

‘A’

(26)

obtained by inserting

the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order.

Binary search tree . . .

‘A’

‘E’

‘F’

‘J’

(27)

Another binary search tree

Add nodes containing these values in this order:

‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’

‘K’ ‘P’

(28)

Is ‘F’ in the binary search tree?

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’

‘K’

‘V’

‘P’ ‘Z’

‘D’

‘L’ ‘Q’

‘B’

(29)

// BINARY SEARCH TREE SPECIFICATION template< class ItemType >

class TreeType {

public:

TreeType ( ) ; // constructor ~TreeType ( ) ; // destructor bool IsEmpty ( ) const ;

bool IsFull ( ) const ;

int NumberOfNodes ( ) const ; void InsertItem ( ItemType item ) ;

void DeleteItem (ItemType item ) ;

void RetrieveItem ( ItemType& item , bool& found ) ; void PrintTree (ofstream& outFile) const ;

. . .

private:

TreeNode<ItemType>* root ;

(30)

// SPECIFICATION (continued)

// - - - - // RECURSIVE PARTNERS OF MEMBER FUNCTIONS

template< class ItemType >

void PrintHelper ( TreeNode<ItemType>* ptr, ofstream& outFile ) ;

template< class ItemType >

void InsertHelper ( TreeNode<ItemType>*& ptr, ItemType item ) ;

template< class ItemType >

void RetrieveHelper ( TreeNode<ItemType>* ptr,

ItemType& item, bool& found ) ;

(31)

// BINARY SEARCH TREE IMPLEMENTATION

// OF MEMBER FUNCTIONS AND THEIR HELPER FUNCTIONS

// - - - template< class ItemType >

TreeType<ItemType> :: TreeType ( ) // constructor {

root = NULL ; }

// - - - - template< class ItemType >

bool TreeType<ItemType> :: IsEmpty( ) const {

return ( root == NULL ) ; }

(32)

template< class ItemType >

void TreeType<ItemType> :: RetrieveItem ( ItemType& item, bool& found ) {

RetrieveHelper ( root, item, found ) ; }

template< class ItemType >

void RetrieveHelper ( TreeNode<ItemType>* ptr, ItemType& item, bool& found)

{ if ( ptr == NULL ) found = false ;

else if ( item < ptr->info ) // GO LEFT RetrieveHelper( ptr->left , item, found ) ;

else if ( item > ptr->info ) // GO RIGHT RetrieveHelper( ptr->right , item, found ) ;

(33)

template< class ItemType >

void TreeType<ItemType> :: InsertItem ( ItemType item ) {

InsertHelper ( root, item ) ; }

template< class ItemType >

void InsertHelper ( TreeNode<ItemType>*& ptr, ItemType item ) { if ( ptr == NULL )

{ // INSERT item HERE AS LEAF

ptr = new TreeNode<ItemType> ; ptr->right = NULL ;

ptr->left = NULL ; ptr->info = item ; }

else if ( item < ptr->info ) // GO LEFT InsertHelper( ptr->left , item ) ;

else if ( item > ptr->info ) // GO RIGHT InsertHelper( ptr->right , item ) ;

(34)

Inorder Traversal: A E H J M T Y

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree Print second

(35)

// INORDER TRAVERSAL template< class ItemType >

void TreeType<ItemType> :: PrintTree ( ofstream& outFile ) const {

PrintHelper ( root, outFile ) ; }

template< class ItemType >

void PrintHelper ( TreeNode<ItemType>* ptr, ofstream& outFile ) { if ( ptr != NULL )

{

PrintHelper( ptr->left , outFile ) ; // Print left subtree outFile << ptr->info ;

PrintHelper( ptr->right, outFile ) ; // Print right subtree }

(36)

Preorder Traversal: J E A H T M Y

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree Print first

(37)

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree

Print left subtree first Print right subtree second Print last

Postorder Traversal: A H E M Y T J

(38)

template< class ItemType >

TreeType<ItemType> :: ~TreeType ( ) // DESTRUCTOR {

DestroyHelper ( root ) ; }

template< class ItemType >

void DestroyHelper ( TreeNode<ItemType>* ptr )

// Post: All nodes of the tree pointed to by ptr are deallocated.

{ if ( ptr != NULL ) {

DestroyHelper ( ptr->left ) ; DestroyHelper ( ptr->right ) ; delete ptr ;

References

Related documents

‘Delivering Change in the Higher Education Sector’, Higher Education Leadership Conference, Dublin.. This Presentation is brought to you for free and open access by the Centre

Hyphae localization in tissue surrounding the wound or inoculation sites indicates that Pch colonizes all cell types, such as vascular tissues, paratracheal parenchyma cells,

• Today: copper circuit paths on the printed circuit boards 03 New developments in fiber optic technology.. Products for

Teleport questions with parents is it phrase is important to do they are better understand grammar quiz and relative clause and organize your team has a quizizz.. Nailed it to use

Do not try more times if the terminal declaims the debit card as it will cost you every time Always store all the receipts and get as much information as you can from the customer

Concur: The peer review process has been strengthened to ensure documentation of corrective action completion is included in the Peer Review Committee minutes. We recommended

• The pros of well made micro filtered isolates, is a high protein content (90% or above), low lactose and fat levels, very low levels of undenatured proteins, and the retention

But if experience does not have, in addition to its subjective character, an objective nature that can be apprehended from many different points of view, then how can it be