Professional Documents
Culture Documents
Chapter5 Tree
Chapter5 Tree
Yellow Blue
Leaf
CHAPTER 5 2
Definition of Tree
►A tree is a finite set of one or more nodes
such that:
► There is a specially designated node calle
d the root.
► The remaining nodes are partitioned into
n>=0 disjoint sets T1, ..., Tn, where each
of these sets is a tree.
► We call T1, ..., Tn the subtrees of the root.
CHAPTER 5 3
Level and Depth
Level
Number of nodes (13)
degree of a node
leaf (terminal) A 1
nonterminal 3 1
parent
children B
2 1
C
2 3
D
2 2
2
sibling
degree of a tree (3) E F G H I J
ancestor 2 30 30 31 30 30 3 3
level of a node
K L M 4
0 40 4 0 4
CHAPTER 5 4
Terminology
► The degree of a node is the number of subtrees
of the node
The degree of A is 3; the degree of C is 1.
► The node with degree 0 is a leaf or terminal node.
► A node that has subtrees is the parent of the roots of the
subtrees.
► The roots of these subtrees are the children of the node.
► Children of the same parent are siblings.
► The ancestors of a node are all the nodes along the pat
h from the root to the node.
CHAPTER 5 5
Binary Trees
►A binary tree is a finite set of nodes that is
either empty or consists of a root and two
disjoint binary trees called the left subtree
and the right subtree.
► Any tree can be transformed into binary tree.
by left child-right sibling representation
► Theleft subtree and the right subtree are disting
uished.
CHAPTER 5 6
*Figure - Left child-right child tree representation of a tree
E C
F G D
K
L H
M I
J
Samples of Trees
Complete Binary Tree
A A 1 A
B B 2 B C
4 H I
E 5
Skewed = aslant, diagonal, canted, unbalanced CHAPTER 5 8
Maximum Number of Nodes in BT
► The maximum number of nodes on level i of a
binary tree is 2i-1, i>=1.
► The maximum nubmer of nodes in a binary tre
e
of depth k is 2k - 1, k>=1.
Prove by induction.
k
2 i 1
2 k
1
i 1
CHAPTER 5 9
Relations between Number of
Leaf Nodes and Nodes of Degree 2
For any nonempty binary tree, T, if n0 is the
number of leaf nodes and n2 the number of nodes
of degree 2, then n0=n2+1
proof:
Let n and B denote the total number of nodes &
branches in T.
Let n0, n1, n2 represent the nodes with no children,
single child, and two children respectively.
n= n0+n1+n2, B+1=n, B=n1+2n2 ==> n1+2n2+1= n,
n1+2n2+1= n0+n1+n2 ==> n0=n2+1
CHAPTER 5 10
Full BT VS Complete BT
►A full binary tree of depth k is a binary tree of
depth k having 2k -1 nodes, k>=0.
► A binary tree with n nodes and depth k is
complete iff its nodes correspond to the nodes numbered from 1 to
n in the full binary tree of
depth k.
A A
B C B C
D E F G D E F G
H I J K L M N O
H I
Complete binary tree CHAPTER 5
Full binary tree of depth 4
11
Binary Tree Representations
► Ifa complete binary tree with n nodes
(depth = log n + 1) is represented sequentially, then
for any node with index i, 1<=i<=n, we have:
parent(i) is at i/2 if i!=1. If i=1, i is at the root and
has no parent.
left_child(i) is at 2i if 2i<=n. If 2i>n, then i has no
left child.
right_child(i) is at 2i+1 if 2i +1 <=n. If 2i +1 >n,
then i has no right child.
CHAPTER 5 12
Sequential Representation [1]
[2]
A
B
[3] C
(1) waste space
(2) insertion/deletion [4] D
A [1] A [5] E
problem
[2] B [6] F
[3] -- [7]
B G
[4] C
A [8] H
[5] -- [9]
C I
[6] --
[7] -- B C
D [8] D
[9] --
. . D E F G
E
[16] E
CHAPTER 5 13
H I
Linked Representation
typedef struct node *tree_pointer;
typedef struct node {
int data;
tree_pointer left_child, right_child;
};
data
CHAPTER 5 14
Binary Tree Traversals
► Let L, V, and R stand for moving left, visiting
the node, and moving right.
► There are six possible combinations of traversal
LVR, LRV, VLR, VRL, RVL, RLV
► Adopt convention that we traverse left before
right, only 3 traversals remain
LVR, LRV, VLR
inorder, postorder, preorder
CHAPTER 5 15
Inorder Traversal
b c
bac
CHAPTER 5 17
Inorder Example (Visit = print)
a
b c
f
d e
g h i j
gdhbei af j c
CHAPTER 5 18
Inorder By Projection (Squishing)
a
b c
f
d e
g h i j
g d h b e i a f jc
CHAPTER 5 19
Inorder Of Expression Tree
/
* +
e f
+ -
a b c d
a + b * c - d/ e + f
b c
abc
CHAPTER 5 22
Preorder Example (Visit = print)
a
b c
f
d e
g h i j
abdghei cf j
CHAPTER 5 23
Preorder Of Expression Tree
/
* +
e f
+ -
a b c d
/ * +a b - c d +e f
b c
bca
CHAPTER 5 26
Postorder Example (Visit = print)
a
b c
f
d e
g h i j
ghdi ebj f ca
CHAPTER 5 27
Postorder Of Expression Tree
/
* +
e f
+ -
a b c d
a b +c d - * e f + /
CHAPTER 5 29
Level-Order Example (Visit = print)
a
b c
f
d e
g h i j
abcdef ghi j
CHAPTER 5 30
Arithmetic Expression Using BT
+ inorder traversal
A/B*C*D+E
infix expression
* E
preorder traversal
+**/ABCDE
* D prefix expression
postorder traversal
AB/C*D*E+
/ C
postfix expression
A * * 4
/ 2 D 5
C 5
A–C/5*2+D*5%4
CHAPTER 5 32
Tree Traversal (contd.)
► pre-order and post-order:
+
- %
A * * 4
/ 2 D 5
C 5
Preorder: + - A * / C 5 2 % * D 5 4
CHAPTER 5 33
Postorder: A C 5 / 2 * - D 5 * 4 % +
Euler Tour Traversal
► generic traversal of a binary tree
► the preorder, inorder, and postorder
traversals are special cases of the Euler tour
traversal
► “walk around” the
tree and visit each
node three times:
on the left
from below
on the right
CHAPTER 5 34
Euler Tour Traversal (cont’d)
eulerTour(node v) {
perform action for visiting node on the left;
if v is internal then
eulerTour(v->left);
perform action for visiting node from below;
if v is internal then
eulerTour(v->right);
perform action for visiting node on the right;
}
CHAPTER 5 35
Euler Tour Traversal (cont’d)
► preorder traversal = Euler Tour with a “visit” only on the left
► inorder = ?
► postorder = ?
► Other applications: compute number of descendants for
each node v:
counter = 0
increment counter each time node is visited on the left
#descendants = counter when node is visited on the
right – counter when node is visited on the left + 1
CHAPTER 5 36
Propositional Calculus Expression
►A variable is an expression.
► If x and y are expressions, then ¬x, xy,
xy are expressions.
► Parentheses can be used to alter the normal
order of evaluation (¬ > > ).
► Example: x1 (x2 ¬x3)
► satisfiability problem: Is there an assignmen
t to make an expression true?
CHAPTER 5 37
(x1 ¬x2) (¬ x1 x3) ¬x3
(t,t,t)
(t,t,f)
(t,f,t)
(t,f,f)
(f,t,t) X3
(f,t,f)
(f,f,t) X1 X3
(f,f,f)
2n possible combinations X2 X1
for n variables
postorder traversal (postfix evaluation)
Binary Tree Construction
► Suppose that the elements in a binary
tree are distinct.
► Can you construct the binary tree from
which a given traversal sequence came?
► When a traversal sequence has more
than one element, the binary tree is not
uniquely defined.
► Therefore, the tree from which the
sequence was obtained cannot be
reconstructed uniquely.
CHAPTER 5 39
Some Examples
preorde a a
r = ab b b
inorder b a
= ab a b
postorder b b
= ab a a
level order a a
= ab b b
CHAPTER 5 40
Binary Tree Construction
CHAPTER 5 41
Preorder And Postorder
preorder = ab a a
postorder = ba b b
gdhbei fjc
CHAPTER 5 43
Inorder And Preorder
a
gdhbei fjc
►preorder =abdgheicfj
►b is the next root; gdh are in the left
subtree; ei are in the right subtree.
a
b fjc
gdh ei CHAPTER 5 44
Inorder And Preorder
a
b fjc
gdh ei
►preorder =abdgheicfj
►d is the next root; g is in the left
subtree; h is in the right subtree.
a
b fjc
d ei
g h CHAPTER 5 45
Inorder And Postorder
CHAPTER 5 46
Inorder And Level Order
CHAPTER 5 47
Trees Data Structures
► Tree
Nodes
Each node can have 0 or more children
A node can have at most one parent
► Binary tree
Tree with 0–2 children per node
CHAPTER 5 48
Tree Binary Tree
Trees
► Terminology
Root no parent
Leaf no child
Interior non-leaf
Height distance from root to leaf
Root node
Leaf nodes
CHAPTER 5 49
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
CHAPTER 5 50
Binary Search Trees
► Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25
Binary Not a binary
search trees search tree
CHAPTER 5 51
Binary Tree Implementation
Class Node {
int data; // Could be int, a class, etc
Node *left, *right; // null if empty
root root
10 5
10 > 2, left 5 > 2, left
5 30 5 > 2, left 2 45 2 = 2, found
2 = 2, found
2 25 45 30
10
CHAPTER 5
25 55
Example Binary Searches
► Find (root, 25 )
10 5
10 < 25, right 5 < 25, right
5 30 30 > 25, left 2 45 45 > 25, left
25 = 25, found 30 > 25, left
2 25 45 30
10 < 25, right
10 25 = 25, found
25
CHAPTER 5 56
Types of Binary Trees
► Degenerate – only one child
► Balanced – “mostly” two children
more formal definitions exist, above are intuitive ideas
Degenerate CHAPTER 5
Balanced 58
CHAPTER 5 59
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
CHAPTER 5 60
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
CHAPTER 5 61
Example Insertion
► Insert ( 20 )
10 10 < 20, right
30 > 20, left
5 30
25 > 20, left
2 25 45 Insert 20 on left
20
CHAPTER 5 62
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
CHAPTER 5 63
Example Deletion (Leaf)
► Delete ( 25 )
10 10
10 < 25, right
5 30 30 > 25, left 5 30
25 = 25, delete
2 25 45 2 45
CHAPTER 5 64
Example Deletion (Internal Node)
► Delete ( 10 )
10 5 5
5 30 5 30 2 30
2 25 45 2 25 45 2 25 45
5 30 5 30 5 30
2 25 45 2 25 45 2 45
A * * 4
/ 2 D 5
C 5
CHAPTER 5 69
Heap
►A max tree is a tree in which the key value in
each node is no smaller than the key values in
its children. A max heap is a complete binary
tree that is also a max tree.
► A min tree is a tree in which the key value in
each node is no larger than the key values in
its children. A min heap is a complete binary
tree that is also a min tree.
► Operations on heaps
creation of an empty heap
insertion of a new element into the heap;
deletion of the largest element from the heap
CHAPTER 5 70
*Figure - Sample max heaps
Property:
The root of max heap (min heap) contains
the largest (smallest).
*Figure - Sample min heaps
20 20 21
15 2 15 5 15 20
14 10 14 10 2 14 10 2
initial location of new node insert 5 into heap insert 21 into heap
CHAPTER 5 73
Insertion into a Max Heap
void insert_max_heap(element item, int *n)
{
int i;
if (HEAP_FULL(*n)) {
fprintf(stderr, “the heap is full.\n”);
exit(1);
}
i = ++(*n);
while ((i!=1)&&(item.key>heap[i/2].key)) {
heap[i] = heap[i/2];
i /= 2;
}
}
heap[i]= item;
2 -1=n ==> k=log (n+1)
k
2
O(log2n)
CHAPTER 5 74
Example of Deletion from Max Heap
remove
20 10 15
15 2 15 2 14 2
14 10 14 10
CHAPTER 5 75
Deletion from a Max Heap
element delete_max_heap(int *n)
{
int parent, child;
element item, temp;
if (HEAP_EMPTY(*n)) {
fprintf(stderr, “The heap is empty\n”);
exit(1);
}
/* save value of the element with the
highest key */
item = heap[1];
/* use last element in heap to adjust heap */
temp = heap[(*n)--];
parent = 1;
child = 2;
CHAPTER 5 76
while (child <= *n) {
/* find the larger child of the current
parent */
if ((child < *n)&&
(heap[child].key<heap[child+1].key))
child++;
if (temp.key >= heap[child].key) break;
/* move to the next lower level */
heap[parent] = heap[child];
child *= 2;
}
heap[parent] = temp;
return item;
}
CHAPTER 5 77
Binary Search Tree
► Heap
a min (max) element is deleted. O(log2n)
deletion of an arbitrary element O(n)
search for an arbitrary element O(n)
► Binary search tree
Every element has a unique key.
The keys in a nonempty left subtree (right subtree) are s
maller (larger) than the key in the root of subtree.
The left and right subtrees are also binary search trees.
CHAPTER 5 78
Examples of Binary Search Trees
20 30 60
12 25 5 40 70
10 15 22 2 65 80
CHAPTER 5 79
Searching a Binary Search Tree
tree_pointer search(tree_pointer root,
int key)
{
/* return a pointer to the node that contains key. If t
is no such
node, return NULL */
CHAPTER 5 80
Another Searching Algorithm
tree_pointer search2(tree_pointer tree, int key)
{
while (tree) {
if (key == tree->data) return tree;
if (key < tree->data)
tree = tree->left_child;
else tree = tree->right_child;
}
return NULL;
}
O(h)
CHAPTER 5 81
Insert Node in Binary Search Tree
30 30 30
5 40 5 40 5 40
2 2 80 2 35 80
Insert 80 Insert 35
CHAPTER 5 82
Insertion into A Binary Search Tree
void insert_node(tree_pointer *node, int num)
{tree_pointer ptr,
temp = modified_search(*node, num);
if (temp || !(*node)) {
ptr = (tree_pointer) malloc(sizeof(node));
if (IS_FULL(ptr)) {
fprintf(stderr, “The memory is full\n”);
exit(1);
}
ptr->data = num;
ptr->left_child = ptr->right_child = NULL;
if (*node)
if (num<temp->data) temp->left_child=ptr;
else temp->right_child = ptr;
else *node = ptr;
}
}
CHAPTER 5 83
Deletion for A Binary Search Tree
1
leaf 30
node
5 80
T1 T2
1
2
2
T1 X
T2
CHAPTER 5 84
Deletion for A Binary Search Tree
non-leaf
40 40
node
20 60 20 55
10 30 50 70 10 30 50 70
45 55 45 52
52
Before deleting 60 After deleting 60
CHAPTER 5 85
1
2
T1
T2 T3
1
2‘
T1
T2’ T3
CHAPTER 5 86
Forest
►A forest is a set of n >= 0 disjoint trees
A
Forest
A E G
B E
B C D F H I C F G
D H
I
CHAPTER 5 87
A inorder: EFBGCHIJDA
preorder: ABEFCGDHIJ
B
E C
F G D
CHAPTER 5 88
Set Representation
► S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}
0 1 2
6 7 8 4 9 3 5
Si Sj =
► Two operations considered here
Disjoint set union S1 S2={0,6,7,8,1,4,9}
Find(i): Find the set containing the element i.
3 S3, 8 S1
CHAPTER 5 89
Disjoint Set Union
Make one of trees a subtree of the other
0 1
8 1 0 4 9
6 7
4 9 6 7 8
CHAPTER 5 90
*Figure -Data Representation of S1S2and S3
0
set pointer
name
6 7 8
S1
S2 4
S3 1 9
3 5
Array Representation for Set
i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent -1 4 -1 2 -1 2 0 0 0 4
int find1(int i)
{
for (; parent[i]>=0; i=parent[i]);
return i;
}
CHAPTER 5 94
B-Tree Properties‡
► Properties
maximum branching factor of M
the root has between 2 and M children or at most L keys
other internal nodes have between M/2 and M children
internal nodes contain only search keys (no data)
smallest datum between search keys x and y equals x
each (non-root) leaf contains between L/2 and L keys
all leaves are at the same depth
► Result
tree is (log n) deep
all operations run in (log n) time
operations pull in about M or L items at a time
CHAPTER 5 95
CHAPTER 5 96
B-Tree Properties
► Properties
maximum branching factor of M
the root has between 2 and M children or at most L keys
other internal nodes have between M/2 and M children
internal nodes contain only search keys (no data)
smallest datum between search keys x and y equals x
each (non-root) leaf contains between L/2 and L keys
all leaves are at the same depth
► Result
tree is (log n) deep
all operations run in (log n) time
operations pull in about M or L items at a time
CHAPTER 5 97
B-Tree Properties
► Properties
maximum branching factor of M
the root has between 2 and M children or at most L keys
other internal nodes have between M/2 and M children
internal nodes contain only search keys (no data)
smallest datum between search keys x and y equals x
each (non-root) leaf contains between L/2 and L keys
all leaves are at the same depth
► Result
tree is (logM n) deep
all operations run in (logM n) time
operations pull in about M/2 or L/2 items at a time
CHAPTER 5 98
B-Tree Nodes
► Internal node
i search keys; i+1 subtrees; M - i - 1 inactive entries
k1 k2 … ki __ … __
1 2 i M - 1
► Leaf
j data keys; L - j inactive entries
k1 k2 … kj __ … __
1 2 j L
CHAPTER 5 99
Example
B-Tree with M = 4
and L = 4
10 40
3 15 20 30 50
1 2 10 11 12 20 25 26 40 42
3 5 6 9 15 17 30 32 33 36 50 60 70
CHAPTER 5 100
Making a B-Tree
3 3 14
Insert(3) Insert(14)
The empty
B-Tree
M = 3 L = 2
Now, Insert(1)?
CHAPTER 5 101
Splitting the Root
Too many
keys in a leaf!
1 3 14 14
Insert(1) And create
3 14
a new root 1 3 14
1 3 14
CHAPTER 5 102
Insertions and Split Ends Too many
keys in a leaf!
14
14 14
Insert(59) Insert(26)
1 3 14 26 59
1 3 14 1 3 14 59
14 26 59
So, split the leaf.
14 59
And add
a new child
1 3 14 26 59
CHAPTER 5 103
Propagating Splits
14 59
14 59
Insert(5) 1 3 5 14 26 59 Add new
1 3 14 26 59 child
1 3 5
5 59 Create a
5 59
new root
1 3 5 14 26 59 1 3 5 14 26 59
CHAPTER 5 104
Split the leaf into two nodes: ► new one with (M+1)/2 items
5 59 Insert(89)
Insert(79)
1 3 5 14 26 59
14
5 59 89
1 3 5 14 26 59 79 89
CHAPTER 5 106
Deletion
14 14
Delete(59)
5 59 89 5 79 89
1 3 5 14 26 59 79 89 1 3 5 14 26 79 89
CHAPTER 5 107
Deletion and Adoption
A leaf has too few keys!
14 14
Delete(5)
5 79 89 ? 79 89
1 3 5 14 26 79 89 1 3 14 26 79 89
3 79 89
1 3 CHAPTER
3 5 14 26 79 89 108
Deletion with Propagation
A leaf has too few keys!
14 14
Delete(3)
3 79 89 ? 79 89
1 3 14 26 79 89 1 14 26 79 89
14
But now a node
has too few subtrees! So, delete
79 89
the leaf
CHAPTER 5 109
1 14 26 79 89
Finishing the Propagation
(More Adoption)
14 79
Adopt a
79 89 14 89
neighbor
1 14 26 79 89 1 14 26 79 89
CHAPTER 5 110
A Bit More Adoption
79 79
Delete(1)
14 89 26 89
(adopt a
neighbor)
1 14 26 79 89 14 26 79 89
CHAPTER 5 111
Pulling out the Root
A leaf has too few keys!
And no neighbor with surplus!
79 79
14 26 79 89 14 79 89
Delete
79 89 89
the leaf
CHAPTER 5 112
14 79 89 14 79 89
Pulling out the Root
(continued)
The root
has just one subtree!
Just make
the one child
79 89
the new root!
14 79 89
But that’s silly!
79 89
14 79 89
CHAPTER 5 113