Trees
Download
1 / 80

Trees - PowerPoint PPT Presentation


  • 137 Views
  • Uploaded on

Trees. Main and Savitch Chapter 10. Binary Trees. A binary tree has nodes , similar to nodes in a linked list structure. Data of one sort or another may be stored at each node. But it is the connections between the nodes which characterize a binary tree. A Binary Tree of States.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Trees' - balthasar


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Trees

Trees

Main and Savitch

Chapter 10


Binary trees
Binary Trees

  • A binary tree has nodes, similar to nodes in a linked list structure.

  • Data of one sort or another may be stored at each node.

  • But it is the connections between the nodes which characterize a binary tree.


A binary tree of states
A Binary Tree of States

In this example, the data contained at each node is one of the 50 states.


A binary tree of states1
A Binary Tree of States

Each tree has a special node called its root, usually drawn at the top.


A binary tree of states2
A Binary Tree of States

Each tree has a special node called its root, usually drawn at the top.

The example tree

has Washington

as its root.


A binary tree of states3
A Binary Tree of States

Each node is permitted to have two links to other nodes, called the left childand the right child.


A binary tree of states4
A Binary Tree of States

Children are usually drawn below a node.

The right child of

Washington is

Colorado.

The left child of

Washington is

Arkansas.


A binary tree of states5
A Binary Tree of States

Some nodes have only one child.

Arkansas has a

left child, but no

right child.


A quiz
A Quiz

Some nodes have only one child.

Which node has

only a right child?


A quiz1
A Quiz

Some nodes have only one child.

Florida has

only a right child.


A binary tree of states6
A Binary Tree of States

A node with no children is called a leaf.


A binary tree of states7
A Binary Tree of States

Each node is called the parent of its children.

Washington is the

parent of Arkansas

and Colorado.


A binary tree of states8
A Binary Tree of States

Two rules about parents:

  • The root has no parent.

  • Every other node has exactly one parent.


A binary tree of states9
A Binary Tree of States

Two nodes with the same parent are called siblings.

Arkansas

and Colorado

are siblings.


Complete binary trees
Complete Binary Trees

A complete binary tree is a special kind of binary tree which will be useful to us.


Complete binary trees1
Complete Binary Trees

A complete binary tree is a special kind of binary tree which will be useful to us.

When a complete

binary tree is built,

its first node must be

the root.


Complete binary trees2
Complete Binary Trees

The second node of a complete binary tree is always the left child of the root...


Complete binary trees3
Complete Binary Trees

The second node of a complete binary tree is always the left child of the root...

... and the third node is always the right child of the root.


Complete binary trees4
Complete Binary Trees

The next nodes must always fill the next level from left to right.


Complete binary trees5
Complete Binary Trees

The next nodes must always fill the next level from left to right.


Complete binary trees6
Complete Binary Trees

The next nodes must always fill the next level from left to right.


Complete binary trees7
Complete Binary Trees

The next nodes must always fill the next level from left to right.


Complete binary trees8
Complete Binary Trees

The next nodes must always fill the next level from left to right.


Complete binary trees9
Complete Binary Trees

The next nodes must always fill the next level from left to right.






Is this complete4
Is This Complete?

Yes!

It is called the empty tree, and it has no nodes, not even a root.


Implementing a complete binary tree

We will store the data from the nodes in a partially-filled array.

Implementing a Complete Binary Tree

An integer to keep

track of how many nodes are in the tree

3

An array of data

We don't care what's in

this part of the array.


Implementing a complete binary tree1

We will store the date from the nodes in a partially-filled array.

Implementing a Complete Binary Tree

An integer to keep

track of how many nodes are in the tree

3

Read Section 10.2 to

see details of how

the entries are stored.

An array of data

We don't care what's in

this part of the array.


Depth of binary tree
Depth of Binary Tree array.

This example, depth = 3


Depth of binary tree1
Depth of Binary Tree array.

This example, depth = 2


Depth of binary tree2
Depth of Binary Tree array.

This example, depth = 0


Depth of complete binary tree
Depth of Complete Binary Tree array.

Given a complete binary tree of N nodes, what is the depth?

D = 0

N = 1

D = 1

N = 3

D = 1

N = 2

D = 2

N = 4

D = 2

N = 7


Depth of complete binary tree1
Depth of Complete Binary Tree array.

Given a complete binary tree of N nodes, what is the depth?

D = 0

N = 1

D = 1

N = 3

D = 1

N = 2

D = 2

N = 4

D = 2

N = 7

D = floor(log N) = O(log N)


Depth of binary tree3
Depth of Binary Tree array.

Given a binary tree of N nodes, what is the maximum possible depth?

D = 0

N = 1

D = 2

N = 3

D = 4

N = 5

D = O(N)


Summary
Summary array.

  • Binary trees contain nodes.

  • Each node may have a left child and a right child.

  • If you start from any node and move upward, you will eventually reach the root.

  • Every node except the root has one parent. The root has no parent.

  • Complete binary trees require the nodes to fill in each level from left-to-right before starting the next level.


Binary search trees
Binary Search Trees array.

  • 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.


The dictionary data type
The Dictionary Data Type array.

  • 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.


The dictionary data type1
The Dictionary Data Type array.

  • 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.


The dictionary data type2
The Dictionary Data Type array.

  • 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


The dictionary data type3
The Dictionary Data Type array.

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

  • The insertion procedure for a dictionary has two parameters.

Washington


The dictionary data type4
The Dictionary Data Type array.

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

Item Dictionary::retrieve("Washington");


The dictionary data type5
The Dictionary Data Type array.

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

Item Dictionary::retrieve("Washington");


The dictionary data type6
The Dictionary Data Type array.

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


A binary search tree of states

Arizona array.

Arkansas

A Binary Search Tree of States

Florida

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

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia


A binary search tree of states1

Colorado array.

Arizona

Arkansas

A Binary Search Tree of States

Florida

Storage rules:

  • Every key to the left of a node is alphabetically before the key of the node.

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia


A binary search tree of states2

Arizona array.

A Binary Search Tree of States

Florida

Storage rules:

  • Every key to the left of a node is alphabetically before the key of the node.

Oklahoma

Colorado

Mass.

Washington

Example:

' Massachusetts' and

' New Hampshire'

are alphabetically

before 'Oklahoma'

New

Hampshire

West

Virginia

Arkansas


A binary search tree of states3

Arizona array.

A Binary Search Tree of States

Florida

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.

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


A binary search tree of states4

Arizona array.

A Binary Search Tree of States

Florida

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.

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Retrieving data

Arizona array.

Retrieving Data

Florida

Start at the root.

1. If the current node has the key, then stop and retrieve the data.

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

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

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Retrieve new hampshire

Arizona array.

Retrieve ' New Hampshire'

Florida

Start at the root.

1. If the current node has the key, then stop and retrieve the data.

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

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

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Retrieve new hampshire1

Arizona array.

Retrieve 'New Hampshire'

Florida

Start at the root.

1. If the current node has the key, then stop and retrieve the data.

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

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

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Retrieve new hampshire2

Arizona array.

Retrieve 'New Hampshire'

Florida

Start at the root.

1. If the current node has the key, then stop and retrieve the data.

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

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

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Retrieve new hampshire3

Arizona array.

Retrieve 'New Hampshire'

Florida

Start at the root.

1. If the current node has the key, then stop and retrieve the data.

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

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

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Retrieval complexity
Retrieval: Complexity array.

  • Given a complete tree of N items, the depth D = O(log N).

  • What is the maximum number of nodes tested to retrieve an item from the binary search tree if we use a complete tree?

  • What is the maximum number of nodes tested (worst case) to retrieve an item from a binary search tree that is not complete or balanced?


Adding a new item with a given key

Arizona array.

Adding a New Item with aGiven Key

Florida

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

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

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Adding

Arizona array.

Adding

Iowa

Florida

  • 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.

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Adding1

Arizona array.

Adding

Iowa

Florida

  • 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.

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Adding2

Arizona array.

Adding

Iowa

Florida

  • 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.

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Adding3

Arizona array.

Adding

Iowa

Florida

  • 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.

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Adding4

Arizona array.

Adding

Iowa

Florida

  • 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.

Oklahoma

Colorado

Mass.

Washington

New

Hampshire

West

Virginia

Arkansas


Adding5

Arizona array.

Adding

Florida

  • 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.

Oklahoma

Colorado

Mass.

Washington

Iowa

New

Hampshire

West

Virginia

Arkansas


Adding6

Arizona array.

Adding

Kazakhstan

Florida

Where would you

add this state?

Oklahoma

Colorado

Mass.

Washington

Iowa

New

Hampshire

West

Virginia

Arkansas


Adding7

Arizona array.

Adding

Florida

Kazakhstan is the

new right child

of Iowa?

Oklahoma

Colorado

Mass.

Washington

Iowa

New

Hampshire

West

Virginia

Arkansas

Kazakhstan


Adding complexity
Adding: Complexity array.

  • Given a complete tree of N items, the depth D = O(log N).

  • What is the maximum number of nodes tested to add an item to the binary search tree if we use a complete tree?

  • What is the maximum number of nodes tested (worst case) to add an item from a binary search tree that is not complete or balanced?


Removing an item with a given key

Arizona array.

Removing an Item with a Given Key

Florida

  • Find the item.

  • If necessary, swap the item with one that is easier to remove.

  • Remove the item.

Oklahoma

Colorado

Mass.

Washington

Iowa

New

Hampshire

West

Virginia

Arkansas

Kazakhstan


Removing florida

Arizona array.

Removing 'Florida'

Florida

  • Find the item.

Oklahoma

Colorado

Mass.

Washington

Iowa

New

Hampshire

West

Virginia

Arkansas

Kazakhstan


Removing florida1

Arizona array.

Removing 'Florida'

Florida

Oklahoma

Colorado

Mass.

Florida cannot be

removed at the

moment...

Washington

Iowa

New

Hampshire

West

Virginia

Arkansas

Kazakhstan


Removing florida2

Arizona array.

Removing 'Florida'

Oklahoma

Colorado

Mass.

... because removing

Florida would

break the tree into

two pieces.

Washington

Iowa

New

Hampshire

West

Virginia

Arkansas

Kazakhstan


Removing florida3

Arizona array.

Removing 'Florida'

Florida

  • If necessary, do some rearranging.

Oklahoma

Colorado

Mass.

The problem of

breaking the tree

happens because

Florida has 2 children.

Washington

Iowa

New

Hampshire

West

Virginia

Arkansas

Kazakhstan


Removing florida4

Arizona array.

Removing 'Florida'

Florida

If necessary, do some rearranging.

Oklahoma

Colorado

Mass.

For the rearranging,

take the smallest item

in the right subtree...

Washington

Iowa

New

Hampshire

West

Virginia

Arkansas

Kazakhstan


Removing florida5

Iowa array.

Arizona

Removing 'Florida'

If necessary, do some rearranging.

Oklahoma

Colorado

Mass.

...copy that smallest

item onto the item

that we're removing...

Washington

Iowa

New

Hampshire

West

Virginia

Arkansas

Kazakhstan


Removing florida6

Iowa array.

Arizona

Removing 'Florida'

If necessary, do some rearranging.

Oklahoma

Colorado

Mass.

... and then remove

the extra copy of the

item we copied...

Washington

New

Hampshire

West

Virginia

Arkansas

Kazakhstan


Removing florida7

Iowa array.

Arizona

Removing 'Florida'

If necessary, do some rearranging.

Oklahoma

Colorado

Mass.

Washington

... and reconnect

the tree

Kazakhstan

New

Hampshire

West

Virginia

Arkansas


Removing florida8

Arizona array.

Removing 'Florida'

Florida

Oklahoma

Colorado

Mass.

Why did we choose

the smallestitem

in the right subtree?

Washington

New

Hampshire

West

Virginia

Arkansas

Kazakhstan


Removing florida9

Arizona array.

Removing 'Florida'

Iowa

Oklahoma

Colorado

Mass.

Washington

Because every key

must be smaller than

the keys in its

right subtree

Kazakhstan

New

Hampshire

West

Virginia

Arkansas


Removing an item with a given key1
Removing an Item with a Given Key array.

  • 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.


Summary1
Summary array.

  • 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?


ad