47
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures

Nell Dale Chapter 8 Binary Search Trees

  • Upload
    moses

  • View
    24

  • Download
    0

Embed Size (px)

DESCRIPTION

C++ Plus Data Structures. Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus. Jake’s Pizza Shop. Owner Jake Manager Chef Brad Carol - PowerPoint PPT Presentation

Citation preview

Page 1: Nell Dale Chapter 8 Binary Search Trees

1

Nell DaleChapter 8

Binary Search Trees

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

C++ Plus Data Structures

Page 2: Nell Dale Chapter 8 Binary Search Trees

2

Jake’s Pizza Shop

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Page 3: Nell Dale Chapter 8 Binary Search Trees

3

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

A Tree Has a Root Node

ROOT NODE

Page 4: Nell Dale Chapter 8 Binary Search Trees

4

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Leaf nodes have no children

LEAF NODES

Page 5: Nell Dale Chapter 8 Binary Search Trees

5

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

A Tree Has Levels

LEVEL 0

Page 6: Nell Dale Chapter 8 Binary Search Trees

6

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Level One

LEVEL 1

Page 7: Nell Dale Chapter 8 Binary Search Trees

7

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Level Two

LEVEL 2

Page 8: Nell Dale Chapter 8 Binary Search Trees

8

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

A Subtree

LEFT SUBTREE OF ROOT NODE

Page 9: Nell Dale Chapter 8 Binary Search Trees

9

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Another Subtree

RIGHT SUBTREE OF ROOT NODE

Page 10: Nell Dale Chapter 8 Binary Search Trees

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

Page 11: Nell Dale Chapter 8 Binary Search Trees

11

A Binary Tree

Q

V

T

K S

A E

L

Page 12: Nell Dale Chapter 8 Binary Search Trees

12

How many leaf nodes?

Q

V

T

K S

A E

L

Page 13: Nell Dale Chapter 8 Binary Search Trees

13

How many descendants of Q?

Q

V

T

K S

A E

L

Page 14: Nell Dale Chapter 8 Binary Search Trees

14

How many ancestors of K?

Q

V

T

K S

A E

L

Page 15: Nell Dale Chapter 8 Binary Search Trees

15

Implementing a Binary Tree with Pointers and Dynamic Data

Q

V

T

K S

A E

L

Page 16: Nell Dale Chapter 8 Binary Search Trees

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

. left . info . right

NULL ‘A’ 6000

Page 17: Nell Dale Chapter 8 Binary Search Trees

// 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;}; 17

Page 18: Nell Dale Chapter 8 Binary Search Trees

18

TreeType<char> CharBST;

‘J’

‘E’

‘A’

‘S’

‘H’

TreeType

~TreeType

IsEmpty

InsertItem

Private data:

root

RetrieveItem

PrintTree . . .

Page 19: Nell Dale Chapter 8 Binary Search Trees

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 isless than every key value in its right subtree, and greater than every key value in its left subtree.

A Binary Search Tree (BST) is . . .

Page 20: Nell Dale Chapter 8 Binary Search Trees

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’

Page 21: Nell Dale Chapter 8 Binary Search Trees

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’

Page 22: Nell Dale Chapter 8 Binary Search Trees

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’

Page 23: Nell Dale Chapter 8 Binary Search Trees

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’

Page 24: Nell Dale Chapter 8 Binary Search Trees

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’

‘F’

‘T’

‘A’

Page 25: Nell Dale Chapter 8 Binary Search Trees

25

is obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order?

What binary search tree . . .

‘A’

Page 26: Nell Dale Chapter 8 Binary Search Trees

26

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

Binary search tree . . .

‘A’

‘E’

‘F’

‘J’

‘T’

Page 27: Nell Dale Chapter 8 Binary Search Trees

27

Exercise on Building a BST

Build a binary search tree from the following numbers. Use the first number as the root node and process the numbers from left to right.

[23,3,34,15,18,12,2,45,48,46,49,33]

Page 28: Nell Dale Chapter 8 Binary Search Trees

28

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’

Page 29: Nell Dale Chapter 8 Binary Search Trees

29

Is ‘F’ in the binary search tree?

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’

‘K’

‘V’

‘P’ ‘Z’‘D’

‘Q’‘L’‘B’

‘S’

Page 30: Nell Dale Chapter 8 Binary Search Trees

// BINARY SEARCH TREE SPECIFICATION

class TreeType { public: TreeType ( ) ; // constructor ~TreeType ( ) ; // destructor bool IsEmpty ( ) const ; bool IsFull ( ) const ; int NumberOfNodes ( ) const ; void InsertItem ( int item ) ; void DeleteItem (int item ) ; void RetrieveItem ( int& item , bool& found ) ; void PrintTree (ofstream& outFile) const ; . . .private:

TreeNode* root ;};

30

Page 31: Nell Dale Chapter 8 Binary Search Trees

// SPECIFICATION (continued) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// RECURSIVE PARTNERS OF MEMBER FUNCTIONS WHY HELPERS?

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

void InsertHelper ( TreeNode* & ptr, int item ) ;

void RetrieveHelper ( TreeNode* ptr, int& item, bool& found ) ;

void DestroyHelper ( TreeNode*& ptr ) ;

31

Page 32: Nell Dale Chapter 8 Binary Search Trees

// BINARY SEARCH TREE IMPLEMENTATION // OF MEMBER FUNCTIONS AND THEIR HELPER FUNCTIONS

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - TreeType :: TreeType ( ) // constructor { root = NULL ;}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -bool TreeType :: IsEmpty( ) const{ return ( root == NULL ) ;}

32

Page 33: Nell Dale Chapter 8 Binary Search Trees

void TreeType :: RetrieveItem ( int& item, bool& found )

{ RetrieveHelper ( root, item, found ) ; }

void RetrieveHelper ( TreeNode* ptr, int& 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 ) ; else { item = ptr->info ;

found = true ; }}

33

Page 34: Nell Dale Chapter 8 Binary Search Trees

void TreeType :: InsertItem ( int item ){

InsertHelper ( root, item ) ; }

void InsertHelper ( TreeNode*& ptr, int item ){ if ( ptr == NULL ) { // INSERT item HERE AS LEAF

ptr = new TreeNode ;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

Page 35: Nell Dale Chapter 8 Binary Search Trees

35

Inorder Traversal: A E H J M T Y

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree

Print left subtree first Print right subtree last

Print second

Page 36: Nell Dale Chapter 8 Binary Search Trees

// INORDER TRAVERSALvoid TreeType :: PrintTree ( ofstream& outFile ) const{

PrintHelper ( root, outFile ) ; }

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

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

outFile << ptr->info ;

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

}}

36

Page 37: Nell Dale Chapter 8 Binary Search Trees

37

Preorder Traversal: J E A H T M Y

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree

Print left subtree second Print right subtree last

Print first

Page 38: Nell Dale Chapter 8 Binary Search Trees

38

‘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

Page 39: Nell Dale Chapter 8 Binary Search Trees

TreeType:: ~TreeType ( ) // DESTRUCTOR{ DestroyHelper ( root ) ; }

void DestroyHelper ( TreeNode* ptr )// Post: All nodes of the tree pointed to by ptr are deallocated.

{ if ( ptr != NULL ) {

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

}}

39

Page 40: Nell Dale Chapter 8 Binary Search Trees

40

Deleting Nodes from a BST

Deleting a node from a BST is not straightforward!!

We deal with three different cases, assuming we have already found the node (A) Deleting a leaf node (B) Deleting a node with one child (C) Deleting a node with two children

Page 41: Nell Dale Chapter 8 Binary Search Trees

41

Deleting A Leaf Node

Deleting a leaf node is only two-step operation -Find the node -Set its parent’s appropriate pointer to

NULL and delete the node

Page 42: Nell Dale Chapter 8 Binary Search Trees

42

Deleting A Node With One Child

Deleting a node with one child is somewhat tricky. We do not want to lose the descendents

We let the parent of the node being deleted skip over it and point to its child

Then the node is deleted

Page 43: Nell Dale Chapter 8 Binary Search Trees

43

Deleting A Node With Two Children

Deleting a node with two children is the most difficult task!!!!

Its parent cannot be made to point to the two children with only one pointer

Instead, we can “copy” a selected node to the node being deleted and get rid of the other node

Page 44: Nell Dale Chapter 8 Binary Search Trees

44

The “Selected” Node

The selected node is the node with highest value in the left subtree of the node to be deleted

This value will be found at the extreme right of the left subtree of the node to be deleted

For example, consider the diagram shown:

Page 45: Nell Dale Chapter 8 Binary Search Trees

45

Deleting A Node With Two Children

Thus we will retain the BST structure without violating any of its formation rules

Now we can start developing the delete function that can detect and deal with the cases shown

Page 46: Nell Dale Chapter 8 Binary Search Trees

46

Delete Function

We pass the pointer *to the item* being deleted to the function

If leftsubtree is NULL and rightsubtree is NULL, we can set the passed pointer to NULL

If one of the subtrees is not NULL, we set the pointer to that subtree

If both subtrees are not NULL, we have to find the predecessor and copy the information from the predecessor. Then the predecessor gets deleted. The program in the textbook is unnecessarily long

Page 47: Nell Dale Chapter 8 Binary Search Trees

47

Delete Function Hierarchy

A public member function is invoked by the client code passing the key of the element to be deleted as a parameter

This function calls a helper function passing it the key and the root pointer

The helper function searches for the key and calls an auxiliary function when the key is matched

This auxiliary function deletes the node containing the key using matches to identify cases A, B or C