Powerpoint Slides for the Standard Version of Starting Out

Download Report

Transcript Powerpoint Slides for the Standard Version of Starting Out

Trees
1
Outline
–
–
–
–
–
Tree Structures
Tree Node Level and Path Length
Binary Tree Definition
Binary Tree Nodes
Binary Search Trees
2
Tree Structures
Arrays, vectors, lists are linear structures, one element
follows another. Trees are hierarchical structure.
President -CEO
Product ion
Manager
Personnel
Manager
Purchasing
Supervisor
Warehouse
Supervisor
Sales
Manager
Shipping
Supervisor
HIERARC HIC AL TREE STRUC TURE
3
Tree Structures
+
*
a
/
e
-
b
c
BINARY EXPRES S ION TREE FOR
d
"a*b + (c-d) / e "
4
Tree Terminology
Tree is a set of nodes. The set may be empty. If not
empty, then there is a distinguished node r, called
root, all other nodes originating from it, and zero or
more non-empty subtrees T1,T2, …,Tk, each of whose
roots are connected by a directed edge from r.
(inductive definition)
r
T1
T2
…
Tk
5
Tree Terminology
 root
A
Nodes in subtrees are called successors or
descendents of the root node
An immediate successor is called a child
B
C
E
F
D
G
H
A leaf node is a node without any children while
an interior node has at least one child.
The link between node describes the parentchild relation.
The link between parent and child is called edge
(b)
I
J
(a)
A path between a parent P and any node N in its
subtrees is a sequence of nodes P= X0,X1,
…,Xk=N, where k is the length of the path. Each
node Xi in the path is the parent of Xi+1, (0≤i<k)
 Size of tree is the number of nodes in the tree
A GENERAL TREE
6
Tree Node Level and Path
Length
The level of a node N in a tree is the length of the path from root to N.
Root is at level 0.
The depth of a tree is the length of the longest path from root to any
node.
Level: 0
A
B
C
E
G
D
F
H
Level: 1
Level: 2
Level: 3
7
Binary Tree
 In a binary tree, no node has more than two
children, and the children are distinguished as
left and right.
 A binary tree T is a finite set of nodes with one
the following properties:
1. T is a tree if the set of nodes is empty.
2. The set consists of a root R and exactly two
distinct binary trees. The left subtree TL and
the right subtree TR, either or both subtree
may be empty.
8
Examples of Binary Trees
A
A
B
B
C
C
D
E
F
G
D
H
E
I
Tree A
Size 9 Depth 3
Size? depth?
Tree B
Size 5 Depth 4
9
Full Binary Tree
• full binary tree: a binary tree is which each node
was exactly 2 or 0 children
10
Density of a Binary Tree
 At any level n, a binary tree may contain from
1 to 2n nodes. The number of nodes per level
contributes to the density of the tree.
 Degenerate tree: there is a single leaf node
and each interior node has only one child.
 An n-node degenerate tree has depth n-1
 Equivalent to a linked list
 A complete binary tree of depth n is a tree in
which each level from 0 to n-1 has all possible
nodes and all leaf nodes at level n occupy the
leftmost positions in the tree.
11
Complete Binary Tree
• complete binary tree: a binary tree in which every
level, except possibly the deepest is completely
filled. At depth n, the height of the tree, all nodes
are as far left as possible
12
Complete or noncomplete?
A
B
D
C
E
F
Complete Tree (Depth 2)
Full with all possible nodes
G
13
Complete or noncomplete?
A
B
D
H
C
E
I
Non-Complete Tree (Depth 3)
Level 2 is missing nodes
14
Complete or noncomplete?
A
B
D
H
C
E
I
F
G
K
Non-CompleteTree (Depth 3)
occup leftmost positions
Nodes at level 3 do not occurpy
y
15
Complete or noncomplete?
A
B
C
D
H
E
I
F
G
J
Complete Tree (Depth 3)
16
Perfect Binary Tree
• perfect binary tree: a binary tree with all leaf nodes
at the same depth. All internal nodes have exactly
two children.
• a perfect binary tree has the maximum number of
nodes for a given height
• a perfect binary tree has 2(n+1) - 1 nodes where n is
the height of a tree
–
–
–
–
height = 0 -> 1 node
height = 1 -> 3 nodes
height = 2 -> 7 nodes
height = 3 -> 15 nodes
17
Binary Search Trees
• Key property
– Value at node
• Smaller values in left subtree
• Larger values in right subtree
– Example
X
• X>Y
• X<Z
Y
Z
Binary Search Trees
• Examples
5
10
2
5
10
45
30
5
45
30
2
25
45
2
10
25
30
25
Binary
search trees
Not a binary
search tree
Iterative Search of Binary Tree
Node *Find( Node *n, int key) {
while (n != NULL) {
if (n->data == key)
// Found it
return n;
if (n->data > key)
// In left subtree
n = n->left;
else
// In right subtree
n = n->right;
}
return null;
}
Node * n = Find( root, 5);
Recursive Search of Binary
Tree
Node *Find( Node *n, int key) {
if (n == NULL)
// Not found
return( n );
else if (n->data == key) // Found it
return( n );
else if (n->data > key)
// In left subtree
return Find( n->left, key );
else
// In right subtree
return Find( n->right, key );
}
Node * n = Find( root, 5);
Example Binary Searches
• Find ( root, 2 )
root
10
5
5
10 > 2, left
30
5 > 2, left
2
45
2 = 2, found
2
25
30
45
5 > 2, left
10
25
2 = 2, found
Example Binary Searches
• Find (root, 25 )
10
5
5
10 < 25, right
30
30 > 25, left
2
45
25 = 25, found
2
25
5 < 25, right
45 > 25, left
30 > 25, left
30
45
10
10 < 25, right
25 = 25, found
25
Types of Binary Trees
• Degenerate – only one child
• Complete – always two children
• Balanced – “mostly” two children
– more formal definitions exist, above are intuitive ideas
Degenerate
binary tree
Balanced
binary tree
Complete
binary tree
Binary Search Tree
Construction
• How to build & maintain binary trees?
– Insertion
– Deletion
• Maintain key property (invariant)
– Smaller values in left subtree
– Larger values in right subtree
Binary Search Tree – Insertion
•
•
Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left subtree for Y
4. If X > Y, insert new leaf X as new right subtree for Y
Observations
– O( log(n) ) operation for balanced tree
– Insertions may unbalance tree
Example Insertion
• Insert ( 20 )
10
5
10 < 20, right
30 > 20, left
30
25 > 20, left
2
25
20
45
Insert 20 on left
Binary Search Tree – Deletion
•
Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from subtree
Observation
– O( log(n) ) operation for balanced tree
– Deletions may unbalance tree
Example Deletion (Leaf)
• Delete ( 25 )
10
5
10
10 < 25, right
30
5
30 > 25, left
30
25 = 25, delete
2
25
45
2
45
Example Deletion (Internal
Node)
• Delete ( 10 )
10
5
2
5
30
25
5
45
Replacing 10
with largest
value in left
subtree
2
5
30
25
2
45
Replacing 5
with largest
value in left
subtree
2
30
25
45
Deleting leaf
Example Deletion (Internal
Node)
• Delete ( 10 )
10
5
2
25
30
25
5
45
Replacing 10
with smallest
value in right
subtree
2
25
30
25
5
45
Deleting leaf
2
30
45
Resulting tree