trees main and savitch chapter 10. binary trees a binary tree has nodes, similar to nodes in a...
Post on 22-Dec-2015
222 views
TRANSCRIPT
Binary TreesBinary 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 StatesA Binary Tree of States
In this example, the data contained at each node is one of the 50 states.
A Binary Tree of StatesA Binary Tree of States
Each tree has a special node called its root, usually drawn at the top.
A Binary Tree of StatesA Binary Tree of States
Each tree has a special node called its root, usually drawn at the top. The example tree
has Washingtonas its root.
The example treehas Washington
as its root.
A Binary Tree of StatesA Binary Tree of States
Each node is permitted to have two links to other nodes, called the left child and the right child.
A Binary Tree of StatesA Binary Tree of States
Children are usually drawn below a node.
The right child ofWashington is
Colorado.
The right child ofWashington is
Colorado.
The left child ofWashington is
Arkansas.
The left child ofWashington is
Arkansas.
A Binary Tree of StatesA Binary Tree of States
Some nodes have only one child.
Arkansas has aleft child, but no
right child.
Arkansas has aleft child, but no
right child.
A QuizA Quiz
Some nodes have only one child.
Which node hasonly a right child?
Which node hasonly a right child?
A QuizA Quiz
Some nodes have only one child.
Florida hasonly a right child.
Florida hasonly a right child.
A Binary Tree of StatesA Binary Tree of States
Each node is called the parent of its children.
Washington is theparent of Arkansas
and Colorado.
Washington is theparent of Arkansas
and Colorado.
A Binary Tree of StatesA Binary Tree of States
Two rules about parents:
The root has no parent.
Every other node has exactly one parent.
A Binary Tree of StatesA Binary Tree of States
Two nodes with the same parent are called siblings.
Arkansasand Coloradoare siblings.
Arkansasand Coloradoare siblings.
Complete Binary TreesComplete Binary Trees
A complete binary tree is a special kind of binary tree which will be useful to us.
Complete Binary TreesComplete Binary Trees
A complete binary tree is a special kind of binary tree which will be useful to us.
When a completebinary tree is built,
its first node must bethe root.
When a completebinary tree is built,
its first node must bethe root.
Complete Binary TreesComplete Binary Trees
The second node of a complete binary tree is always the left child of the root...
Complete Binary TreesComplete 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 TreesComplete Binary Trees
The next nodes must always fill the next level from left to right.
Complete Binary TreesComplete Binary Trees
The next nodes must always fill the next level from left to right.
Complete Binary TreesComplete Binary Trees
The next nodes must always fill the next level from left to right.
Complete Binary TreesComplete Binary Trees
The next nodes must always fill the next level from left to right.
Complete Binary TreesComplete Binary Trees
The next nodes must always fill the next level from left to right.
Complete Binary TreesComplete Binary Trees
The next nodes must always fill the next level from left to right.
Is This Complete?Is This Complete?
Yes!
It is called the empty tree, and it has no nodes, not even a root.
Implementing a Complete Binary Tree
Implementing a Complete Binary Tree
• We will store the data from the nodes in a partially-filled array.
An array of dataWe don't care what's inthis part of the array.
An integer to keeptrack of how many nodes are in the tree
3
Implementing a Complete Binary Tree
Implementing a Complete Binary Tree
• We will store the date from the nodes in a partially-filled array.
An array of dataWe don't care what's inthis part of the array.
An integer to keeptrack of how many nodes are in the tree
3
Read Section 10.2 tosee details of how
the entries are stored.
Read Section 10.2 tosee details of how
the entries are stored.
Depth of Complete Binary Tree
Given a complete binary tree of N nodes, what is the depth?
D = 0N = 1 D = 1
N = 3 D = 2N = 7
D = 1N = 2 D = 2
N = 4
Depth of Complete Binary Tree
Given a complete binary tree of N nodes, what is the depth?
D = 0N = 1 D = 1
N = 3 D = 2N = 7
D = 1N = 2 D = 2
N = 4
D = floor(log N) = O(log N)
Depth of Binary Tree
Given a binary tree of N nodes, what is the maximum possible depth?
D = 0N = 1 D = 2
N = 3 D = 4N = 5
D = O(N)
• 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.
Summary Summary
• 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
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.
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.
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
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
The Dictionary Data TypeThe Dictionary Data Type
• When you want to retrieve an item, you specify the key...
Item Dictionary::retrieve("Washington");
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.
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.
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
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:
1. Every key to the left of a node is alphabetically before the key of the node.
Arizona
Colorado
Arkansas
A Binary Search Tree of StatesA Binary Search Tree of States
Storage rules:
1. 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'
Arizona
Arkansas
A Binary Search Tree of StatesA Binary Search Tree of States
Storage rules:
1. Every key to the left of a node is alphabetically before the key of the node.
2. 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
Arizona
Arkansas
A Binary Search Tree of StatesA Binary Search Tree of States
Storage rules:
1. Every key to the left of a node is alphabetically before the key of the node.
2. 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
Arizona
Arkansas
Retrieving DataRetrieving Data
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.
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
Arizona
Arkansas
Retrieve ' New Hampshire'Retrieve ' New Hampshire'
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
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.
Arizona
Arkansas
Retrieve 'New Hampshire'Retrieve 'New Hampshire'
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
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.
Arizona
Arkansas
Retrieve 'New Hampshire'Retrieve 'New Hampshire'
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
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.
Arizona
Arkansas
Retrieve 'New Hampshire'Retrieve 'New Hampshire'
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
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.
Retrieval: Complexity
• 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?
Arizona
Arkansas
Adding a New Item with aGiven Key
Adding a New Item with aGiven Key
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.
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
Arizona
Arkansas
AddingAdding
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.
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
Iowa
Arizona
Arkansas
AddingAdding
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.
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
Iowa
Arizona
Arkansas
AddingAdding
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.
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
Iowa
Arizona
Arkansas
AddingAdding
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.
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
Iowa
Arizona
Arkansas
AddingAdding
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.
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
Iowa
Arizona
Arkansas
AddingAdding
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.
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
reIowa
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
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
Adding: Complexity
• 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?
Arizona
Arkansas
Removing an Item with a Given Key
Removing an Item with a Given Key
1. Find the item.
2. If necessary, swap the item with one that is easier to remove.
3. Remove the item.Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
reIowa
Kazakhstan
Arizona
Arkansas
Removing 'Florida'Removing 'Florida'
1. Find the item.
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
reIowa
Kazakhstan
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...
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.
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.
1. If necessary, do some rearranging.
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...
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.
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.
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.
Arizona
Arkansas
Removing 'Florida'Removing 'Florida'
Washington
OklahomaColorado
Florida
Wes
tV
irgin
ia
Mass.
New
Ham
pshi
re
Kazakhstan
Why did we choosethe smallest item
in the right subtree?
Why did we choosethe smallest item
in the right subtree?
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
Removing an Item with a Given Key
Removing an Item with a Given Key
1. Find the item.
2. If the item has a right child, rearrange the tree:1. Find smallest item in the right subtree
2. Copy that smallest item onto the one that you want to remove
3. Remove the extra copy of the smallest item (making sure that you keep the tree connected)
3. else just remove the item.
• 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