CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of...

44
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science

Transcript of CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of...

Page 1: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

CSC212 Data Structure

CSC212 Data Structure

Lecture 14

Binary Search Trees

Instructor: Prof. George Wolberg

Department of Computer Science

City College of New York

Page 2: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

One of the tree applications in Chapter 10 is binary search trees.

In Chapter 10, binary search trees are used to implement bags and sets.

This presentation illustrates how another data type called a dictionary is implemented with binary search trees.

Binary Search TreesBinary Search Trees

Page 3: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Binary Search Tree DefinitionBinary Search Tree Definition

In a binary search tree, the entries of the nodes can be compared with a strict weak ordering. Two rules are followed for every node n: The entry in node n is NEVER less than an

entry in its left subtree The entry in the node n is less than every entry

in its right subtree.

Page 4: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

The Dictionary Data TypeThe Dictionary Data Type

A dictionary is a collection of items, similar to a bag.

But unlike a bag, each item has a string attached to it, called the item's key.

Page 5: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

The Dictionary Data TypeThe Dictionary Data Type

A dictionary is a collection of items, similar to a bag.

But unlike a bag, each item has a string attached to it, called the item's key.

Example: The items I am storing are records containing data about a state.

Page 6: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

The Dictionary Data TypeThe Dictionary Data Type

A dictionary is a collection of items, similar to a bag.

But unlike a bag, each item has a string attached to it, called the item's key.

Example: The key for each record is the name of the state. Washington

Page 7: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

The Dictionary Data TypeThe Dictionary Data Type

The insertion procedure for a dictionary has two parameters.

void Dictionary::insert(The key for the new item, The new item);

Washington

Page 8: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

The Dictionary Data TypeThe Dictionary Data Type

When you want to retrieve an item, you specify the key...

Item Dictionary::retrieve("Washington");

Page 9: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Item Dictionary::retrieve("Washington");

The Dictionary Data TypeThe Dictionary Data Type

When you want to retrieve an item, you specify the key... ... and the retrieval procedure returns the item.

Page 10: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

The Dictionary Data TypeThe Dictionary Data Type

We'll look at how a binary tree can be used as the internal storage mechanism for the dictionary.

Page 11: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Colorado

A Binary Search Tree of StatesA Binary Search Tree of States

The data in the dictionary will be stored in a binary tree, with each node containing an item and a key.

Washington

Oklahoma

Florida

Mass.

New

Ham

pshi

re

Wes

tV

irgin

ia

Page 12: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Colorado

Arizona

Arkansas

A Binary Search Tree of StatesA Binary Search Tree of States

Washington

OklahomaColorado

Florida

Mass.

New

Ham

pshi

re

Wes

tV

irgin

ia

Storage rules: Every key to the left of a

node is alphabetically before the key of the node.

Page 13: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Colorado

Arkansas

A Binary Search Tree of StatesA Binary Search Tree of States

Storage rules: Every key to the left of

a node is alphabetically before the key of the node.

Washington

Oklahoma

Florida

Mass.

New

Ham

pshi

re

Wes

tV

irgin

ia

Example: ' Massachusetts' and ' New Hampshire' are alphabetically before 'Oklahoma'

Page 14: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

A Binary Search Tree of StatesA Binary Search Tree of States

Storage rules: Every key to the left of a

node is alphabetically before the key of the node.

Every key to the right of a node is alphabetically after the key of the node.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Page 15: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

A Binary Search Tree of StatesA Binary Search Tree of States

Storage rules: Every key to the left of

a node is alphabetically before the key of the node.

Every key to the right of a node is alphabetically after the key of the node.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Page 16: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Retrieving DataRetrieving Data

Start at the root. If the current node has

the key, then stop and retrieve the data.

If the current node's key is too large, move left and repeat 1-3.

If the current node's key is too small, move right and repeat 1-3.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Page 17: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Retrieve 'New Hampshire'Retrieve 'New Hampshire'

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Start at the root. If the current node has

the key, then stop and retrieve the data.

If the current node's key is too large, move left and repeat 1-3.

If the current node's key is too small, move right and repeat 1-3.

Page 18: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Retrieve 'New Hampshire'Retrieve 'New Hampshire'

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Start at the root. If the current node has

the key, then stop and retrieve the data.

If the current node's key is too large, move left and repeat 1-3.

If the current node's key is too small, move right and repeat 1-3.

Page 19: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Retrieve 'New Hampshire'Retrieve 'New Hampshire'

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Start at the root. If the current node has

the key, then stop and retrieve the data.

If the current node's key is too large, move left and repeat 1-3.

If the current node's key is too small, move right and repeat 1-3.

Page 20: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Retrieve 'New Hampshire'Retrieve 'New Hampshire'

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Start at the root. If the current node has

the key, then stop and retrieve the data.

If the current node's key is too large, move left and repeat 1-3.

If the current node's key is too small, move right and repeat 1-3.

Page 21: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Adding a New Item with aGiven KeyAdding a New Item with aGiven Key

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Page 22: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Page 23: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Page 24: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Page 25: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Page 26: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Page 27: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

AddingAdding

Pretend that you are trying to find the key, but stop when there is no node to move to.

Add the new node at the spot where you would have moved to if there had been a node.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Page 28: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Adding Adding

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Where would youadd this state?

Where would youadd this state?

Kazakhstan

Page 29: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Adding Adding

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan is thenew right child

of Iowa?

Kazakhstan is thenew right child

of Iowa?

Kazakhstan

Page 30: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Removing an Item with a Given KeyRemoving an Item with a Given Key

Find the item. If necessary, swap the

item with one that is easier to remove.

Remove the item.Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

Page 31: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Find the item.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

Page 32: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

Florida cannot beremoved at the

moment...

Florida cannot beremoved at the

moment...

Page 33: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

... because removingFlorida would

break the tree intotwo pieces.

... because removingFlorida would

break the tree intotwo pieces.

Page 34: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

The problem ofbreaking the treehappens because

Florida has 2 children.

The problem ofbreaking the treehappens because

Florida has 2 children.

If necessary, do some rearranging.

Page 35: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

If necessary, do some rearranging.

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

Kazakhstan

For the rearranging,take the smallest itemin the right subtree...

For the rearranging,take the smallest itemin the right subtree...

Work for multi-set?

Page 36: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Kazakhstan

Iowa...copy that smallest

item onto the itemthat we're removing...

...copy that smallestitem onto the item

that we're removing...

If necessary, do some rearranging.

Page 37: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Kazakhstan

... and then removethe extra copy of the

item we copied...

... and then removethe extra copy of the

item we copied...

If necessary, do some rearranging.

Page 38: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Kazakhstan... and reconnect

the tree

If necessary, do some rearranging.

Page 39: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Kazakhstan

Why did I choosethe smallest item

in the right subtree?

Why did I choosethe smallest item

in the right subtree?

Page 40: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Arizona

Arkansas

Removing 'Florida'Removing 'Florida'

Washington

OklahomaColorado

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

re

Iowa

Kazakhstan

Because every keymust be smaller than

the keys in itsright subtree

Page 41: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Removing an Item with a Given KeyRemoving an Item with a Given Key

Find the item. If the item has a right child, rearrange the tree:

Find smallest item in the right subtree Copy that smallest item onto the one that you

want to remove Remove the extra copy of the smallest item

(making sure that you keep the tree connected)

else just remove the item.

Page 42: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

Binary search trees are a good implementation of data types such as sets, bags, and dictionaries.

Searching for an item is generally quick since you move from the root to the item, without looking at many other items.

Adding and deleting items is also quick. But as you'll see later, it is possible for the

quickness to fail in some cases -- can you see why?

Summary Summary

Page 43: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

AssignmentAssignment

Read Section 10.5 Assignment – Bag class with a BST

Memeber functions void insert(const Item& entry); size_type count (const Item& target);

Non-member functions viod bst_remove_all(binary_tree_node<Item>*& root

const Item& target); void bst_remove_max(binary_tree_node<Item>*& root,

Item& removed);

Page 44: CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.

THE ENDTHE END

Presentation copyright 1997 Addison Wesley Longman,For use with Data Structures and Other Objects Using C++by Michael Main and Walter Savitch.

Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

Students and instructors who use Data Structures and Other Objects Using C++ are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.