Binary Trees - ODU Computer Science

advertisement
Week 6
– Binary Trees
1
Main Index
Contents
Sequence and Associative Containers
• Sequence containers access data by position
1. Array (index)
2. Vector (index)
3. List (iterator)
• Associate containers can access data by value
1. Set and Map / Binary search tree
Arrays
Vectors
Linked
lists
Trees
Tree in Nature
Tree in our life
• Need to turn it upside down
President-CEO
Production
Manager
Personnel
Manager
Purchasing
Supervisor
Warehouse
Supervisor
Sales
Manager
Shipping
Supervisor
HIERARCHICAL TREE STRUCTURE
Tree in Computer Science
• Similar to tree in nature
Root
Leaves
Root
Parent
Child
Edge
Leaf
Tree Structures
A
B
C
E
Interior node
Subtree
Level
F
D
G
(b)
I
J
(a)
Depth = max level
A GENERAL TREE
6
Main Index
Contents
H
Terminologies used in Trees - Wiki
Tree Node Level and Path Length
Level: 0
A
B
C
E
G
D
Level: 2
F
H
Level: 3
What is the Depth?
8
Level: 1
Main Index
Contents
Binary Tree Definition


9
A binary tree T is a finite set of nodes with one
of the following properties:
–
(a) T is a tree if the set of nodes is empty.
(An empty tree is a tree, size=0.)
–
(b) The set consists of a root, R, and exactly two
distinct binary trees, the left subtree TL and the
right subtree TR. The nodes in T consist of node
R and all the nodes in TL and TR.
Any node in a binary tree has at most two
children
Main Index
Contents
Selected Samples of Binary Trees
A
A
B
B
C
C
D
E
F
G
D
H
E
I
Tree B
Size 5 Depth 4
Tree A
Size 9 Depth 3
Does tree B have left subtree TL?
10
Main Index
Contents
Density of a Binary Tree
• Intuitively, density is a measure of the size of a tree (number
of nodes) relative to the depth of the tree.
• Trees with a higher density are important as data structures,
because they can “pack” more nodes near the root.
• Access to the nodes is along relatively short paths from the
root.
Complete binary tree
Degenerate tree
• A degenerate (or pathological) tree is where each
parent node has only one associated child node.
This means that performance-wise, the tree will
behave like a linked list data structure.
Evaluating Tree Density
• Complete binary trees are an ideal storage structure,
because of their ability to pack a large number of
nodes near the root
• Assume we want to store n elements in a complete
binary tree. We would like to know the depth d of
such a tree.
Depth d --- Size n in complete binary tree
20 = 1 nodes
21 = 2 nodes
Geometric series
…
…
…
level d
has 2d nodes
…………………
#𝒏𝒐𝒅𝒆𝒔 𝒂𝒕 𝒍𝒆𝒗𝒆𝒍 𝒅 ≤ # 𝑎𝑙𝑙 𝑛𝑜𝑑𝑒𝑠 𝑖𝑛 𝑇 ≤
𝟐𝒅
≤
𝑛
≤
𝒅
𝒊=𝟎 #
𝒏𝒐𝒅𝒆𝒔 𝒂𝒕 𝒍𝒆𝒗𝒆𝒍 𝒊
𝒅
𝒅
𝒊=𝟎 𝟐
=
𝟏−𝟐𝒅+𝟏
𝟏−𝟐
= 𝟐𝒅+𝟏 -1
A
Binary Tree Nodes
B
C
D
E
F
H
G
Abstract Tree Model
left
left
B
G
right
right
left
left
A
left
D
right
left
E
right
left
Main Index
right
right
Tree Node Model
17
C
Contents
left
H
right
F
right
Node class
template <typename T>
{
public:
T nodeValue;
tnode<T> *left, *right;
tnode()
{}
tnode(const T& item, tnode<T> *lptr=NULL,
tnode<T> *rptr=NULL):nodeValue(item),
left(lptr), right(rptr)
{}
};
Node structure
Building a Binary Tree
• A binary tree consists of a collection of
dynamically allocated tnode objects whose
pointer values specify links to their children.
Recursion
• Solution to a problem depends on solutions to smaller
instances of the same problem.
• As a tree is a self-referential (recursively defined) data
structure, traversal can naturally be described by recursion.
• Recursive function: a function that calls itself.
21
BINARY TREE SCAN ALGORITHMS
• How to traverse the tree so that each node is visited
exactly once?
1. Depth-first
• Pre-order
• In-order
• Post-order
2. Breadth-first (level-order)
Depth-first
• Defined as operations recursively at each node.
• The actions include:
 visiting the node and performing some task (N),
 making a recursive descent to the left subtree (L),
 making a recursive descent to the right subtree (R).
• The different scanning strategies depend on the order in which
we perform the tasks.
In-order Scan
• The prefix “in” comes from the fact that the visit
occurs between the descents into the two subtrees.
• In this example, we use the order LNR.
1. Traverse the left subtree (“go left”).
2. Visit the node.
3. Traverse the right subtree (“go right”).
N
R
L
Recursively!
In-order example
N
L
N

R
L
L N
R

L N R

Recursively!
R
L N R

In-order scan: B, D, A, E, C
In-order output
Post-order scan
• 1. Traverse the left subtree (“go left”).
• 2. Traverse the right subtree (“go right”).
• 3. Visit the node.
3.
D, B, E, C, A
1.
2.
Post-order output
Pre-order scan
1. Visit the node.
2. Traverse the left subtree (“go left”).
3. Traverse the right subtree (“go right”).
1.
A, B, D, C, E
2.
3.
Breadth-first (Level-Order) Scan
Example Question
F
B
A
G
D
C
I
E
H
Pre-order? In-order? Post-order? Level-order ?
WIKI
Computing the Leaf Count
Pre-order scan
Computing the Depth of a Tree
Post-order scan
Deleting Tree Nodes
Post-order scan
Reading
• Chapter 4
Download