Trees I
Trees-I
Linear Lists and Trees
Linear lists are useful for serially ordered data
– (e1,e2,e3,…,en)
– Days of week
– Months in a year
– Students in a class
Trees are useful for hierarchically ordered data
– Joe’s descendants
– Corporate structure
– G
Government Subdivisions
S bdi i i
– Software structure
2
Joe’s Descendants
What are other examples of hierarchically ordered data?
3
Real Tree vs Tree Data Structure
4
Real Tree vs Tree Data Structure
Branch Root
Leaf
5
Real Tree vs Tree Data Structure
Branch Root
Leaf
6
Definition of Tree
A tree t is a finite nonempty set of elements
One of these elements is called the root
The remaining elements, if any, are partitioned
into trees
trees, which are called the subtrees of t.
t
7
Subtrees
8
Tree Terminology
The element at the top of the
hierarchy is the root.
Elements next in the hierarchy
are the children of the root.
Elements next in the hierarchy
are the grandchildren of the
root, and so on.
Elements at the lowest level of
the hierarchy are the leaves.
9
Other Definitions
Leaves, Parent, Grandparent, Siblings,
Ancestors,, Descendents
Leaves = {Mike,AI,Sue,Chris}
P
Parent(Mary)
t(M )=J
Joe
Grandparent(Sue) = Mary
Siblings(Mary) = {Ann
{Ann,John}
John}
Ancestors(Mike) = {Ann,Joe}
Descendents(Mary)={Mark,Sue}
esce de ts( a y) { a ,Sue}
10
Levels and Height
Root is at level 0 and its children are at level 1.
Height = depth = maximum level index
L
Level
l0
Level 1
Level 2
Level 3
11
Node Degree
Node degree is the number of children it has
12
Tree Degree
Tree degree is the maximum of node degrees
tree degree = 3
13
Binary Tree
A finite (possibly empty) collection of elements
A nonempty binary tree has a root element and
the remaining elements (if any) are partitioned into
two binary trees
They are called the left and right subtrees of the
binaryy tree
All the nodes in a binaryy tree have 0,, 1 or 2
child/children.
14
Binary Tree (Example)
15
Binary Tree for Expressions
16
Binary Tree Properties
1. Every binary tree with n elements, n > 0, has
exactlyy n-1 edges.
g
2. A binary tree of height h, h >= 0, has at least h
h+1
1
and at most 2h+1-1 elements in it.
minimum number of elements maximum number of elements
17
Binary Tree Properties
3. The height of a binary tree that contains n
elements,, n >= 0,, is at least (
(log
g2((n+1))-1
)) and at
most n-1.
4. 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.
18
Full Binary Tree
A full binary tree of height h has exactly 2h+!-1
nodes.
Numbering the nodes in a full binary tree
– Number the nodes 1 throughg 2h+1-1
– Number by levels from top to bottom
– Within a level, number from left to right
19
Node Number Property of Full Binary Tree
Parent of node i is node (i/2),
(i/2) unless i = 1
Node 1 is the root and has no parent
20
Node Number Property of Full Binary Tree
Left child of node i is node 2i,
2i unless 2i > n
n,
where n is the total number of nodes.
If 2i > n
n, node i has no left child
child.
21
Node Number Property of Full Binary Tree
Right child of node i is node 2i+1,
2i+1 unless 2i+1 > n,
n
where n is the total number of nodes.
If 2i+1 > n
n, node i has no right child
child.
22
Complete Binary Tree with n Nodes
Start with a full binary tree that has at least n nodes
Number the nodes as described earlier
earlier.
The binary tree defined by the nodes numbered 1
through n is the n n-node
node complete binary tree.
A full binary tree is a special case of a complete
binaryy tree
A complete binary tree is a binary tree every level of
which has the maximum possible number of nodes
except possibly the last level.
23
Example of Complete Binary Tree
Complete binary tree with 10 nodes
nodes.
Same node number properties (as in full binary
tree) also hold here
here.
24
Binary Tree Representation
Array representation
Linked representation
25
Array Representation of Binary Tree
The binary tree is represented in an array by
storing
g each element at the array
ypposition
corresponding to the number assigned to it.
26
Incomplete Binary Trees
Complete
C l bi
binary tree with
i h some missing
i i elements
l
27
Right-Skewed Binary Tree
Right-skewed
Right skewed binary tree wastes the most space
What about left-skewed binary tree?
28
Linked Representation of Binary Tree
The most popular way to present a binary tree
Each element is represented by a node that has two link
fields (leftChild and rightChild) plus an element field
Each binary tree node is represented as an
structure/object whose data type is Node
The space required byy an n node binaryy tree is
n * sizeof(Node)
29
Linked Representation of Binary Tree
Left Value Right
child child
class Node
{
private:
int key;
Node* left;
Node* right;
public:
Node() { key=-1; left=NULL; right=NULL; };
void setKey(int aKey) { key = aKey; };
void setLeft(Node* aLeft) { left = aLeft; };
void setRight(Node* aRight) { right = aRight; };
int Key() { return key; };
Node*
d * Left()
f () { return left;
l f }
};
Node* Right() { return right; };
30
};
Linked Representation of Binary Tree
31
Linked Representation of Binary Tree
class Tree
{
private:
Node* root;
public:
Tree(){root = NULL;};
~Tree(){freeNode(root);};
Node* Root() { return root; };
...//other methods
...//other methods
void inOrder(Node* n); //inOrder traversal
void preOrder(Node* n); //preOrder traversal
void postOrder(Node* n); //postOrder traversal
private:
void
id freeNode(Node*
f N d (N d * nd);
d)
};
32
Linked Representation of Binary Tree
void Tree::freeNode(Node* nd)
{
if ( nd != NULL )
{
freeNode(nd->Left());
freeNode(nd->Right());
d l t nd;
delete d
}
}
33
Common Binary Tree Operations
Determine the height
Determine the number of nodes
Make a copy
Determine if two binary trees are identical
Display the binary tree
Delete a tree
If it is an expression tree, evaluate the expression
If it is
i an expression
i ttree, obtain
bt i th
the
parenthesized form of the expression
34
Binary Tree Traversal
Many binary tree operations are done by
performing
p g a traversal of the binaryy tree
In a traversal, each element of the binary tree is
visited exactly once
During the visit of an element, all actions (make
a copy,
py, display,
p y, evaluate the operator,
p , etc.)) with
respect to this element are taken
35
Binary Tree Traversal Methods
Preorder
The root of the subtree is processed first before going
i t the
into th left
l ft then
th right
i ht subtree
bt ( t left,
(root, l ft right).
i ht)
Inorder
After the complete processing of the left subtree the root
is processed followed by the processing of the complete
right subtree (left, root, right).
Postorder
The root is processed only after the complete processing
of the left and right subtree (left, right, root).
Level order
The tree is processed by levels. So first all nodes on
level i are processed from left to right before the first
node of level i+1 is visited
36
Preorder Traversal
void
id T
Tree::preOrder(Node*
O d (N d * n))
{
if ( n!=NULL )
{
cout << n->Key() << " ";
preOrder(n >Left());
preOrder(n->Left());
preOrder(n->Right());
}
}
37
Preorder Example (visit = print)
a b d g h e i c f j
38
Preorder of Expression Tree
/ * + a b - c d + e f
Gives prefix form of expression.
39
Inorder Traversal
void
id T
Tree::inOrder(Node*
i O d (N d * n))
{
if ( n!=NULL )
{
inOrder(n->Left());
cout << n
n->Key()
>Key() << " ";
;
inOrder(n->Right());
}
}
40
Inorder Example (visit = print)
g d h b e i a f j c
41
Inorder by Projection (Squishing)
42
Inorder of Expression Tree
Gives infix
Gi i fi form
f off expression,
i which
hi h iis h
how we
normally write math expressions.
43
Postorder Traversal
void
id T
Tree::postOrder(Node*
tO d (N d * n))
{
if ( n!=NULL )
{
postOrder(n->Left());
postOrder(n >Right());
postOrder(n->Right());
cout << n->Key() << " “;
}
}
44
Postorder Example (visit = print)
g h d i e b j f c a
45
Postorder of Expression Tree
a b + c d - * e f + /
Gives postfix form of expression.
46
Level Order Traversal
Try to write the code yourself
– Visit all nodes in the ith level before going into (i+1)th level.
level
47
Level Order Example (visit = print)
Add and delete nodes from a queue
Output: a b c d e f g h i j
48
Time Complexity
The time complexity of each of the four traversal
algorithm
g is O(n)
( ) because each node is visited
exactly once.
Recurrence relation for preorder, inorder and
postorder traversals:
T(n) = 2T(n/2) + c
49
Binary Tree Construction
Suppose that the elements in a binary tree
are distinct
distinct.
Can you construct the binary tree from which
a given
i ttraversall 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.
uniquely
Some Examples
preorder = ab a a
b b
inorder = ab b a
a b
postorder
t d = abb b b
a a
level order = ab a a
b b
Binary Tree Construction
Can you construct the binary tree,
given two traversal sequences?
Depends on which two sequences
are given.
Preorder And Postorder
preorder = ab a a
postorder = ba b b
• Preorder and postorder do not uniquely define a binary tree.
• Nor
N ddo preorder
d and
d llevell order
d (same
( example).
l )
• Nor do postorder and level order (same example).
Inorder And Preorder
inorder = g d h b e i a f j c
preorder = a b d g h e i c f j
Scan the preorder left to right using the inord
er to separate
p left and right
g subtrees.
a is the root of the tree; gdhbei are in the left
subtree; fjc are in the right subtree.
gdhbei fjc
Inorder And Preorder
a
gdhbei fjc
preorder =abdgheicfj
b is the next root; gdh are in the left
subtree;
bt eii are in
i th
the right
i ht subtree.
bt
a
b fjc
j
gdh ei
Inorder And Preorder
a
b fjc
gdh ei
preorder
d =abdgheicfj
d is the next root;; g is in the left sub
tree; h is in the right subtree.
a
b fjc
d ei
g h
Inorder And Postorder
Scan postorder from right to left using inorder
to separate
p left and right
g subtrees.
inorder = g d h b e i a f j c
postorder
t d =ghdiebjfca
Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.
Inorder And Level Order
Scan level order from left to right using inorder
to separate
p left and right
g subtrees.
inorder = g d h b e i a f j c
l
level
l order
d =abcdefghij
Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.
Heap
An array of objects than can be viewed as a
complete binary tree such that:
– Each tree node corresponds to elements of the array
– The tree is complete except possibly the lowest level
level,
filled from left to right
– The max-heap
Th h property
t for
f allll nodes
d I iin th
the ttree mustt
be maintained except for the root:
Value(Parent(I)) value(I)
– Similarlyy for min-heap:
p
Value(Parent(I)) < value(I)
59
Min Heap
p With 9 Nodes
Complete binary tree with 9 nodes.
Min Heap
p With 9 Nodes
4 3
6 7 9 3
8 6
Complete binary tree with 9 nodes
Max Heap
p With 9 Nodes
8 7
6 7 2 6
5 1
Complete binary tree with 9 nodes
Heap
p Height
g
Since a heap is a complete binary tree, the height
of an n node heap is log2(n+1) -1 or O(log n).
Application of Heaps
Delete the minimum/maximum value and
return it. This operation is called
– deleteMin / deleteMax.
Insert a new data value
Applications of Heaps:
• A heap implements a priority queue, which is a queue
th t orders
that d entities
titi nott a on first-come
fi t fi t
first-serve b i
basis,
but on a priority basis: the item of highest priority is at
the head, and the item of the lowest priority is at the tail
• Another application: sorting, which will be seen later
64
A Heap
p Is Efficiently
y Represented
p as An Array
y
8 7
6 7 2 6
5 1
9 8 7 6 7 2 6 5 1
0 1 2 3 4 5 6 7 8 9 10
Moving
g Up
p And Down A Heap
p
1
9
2 3
8 7
4 5 6 7
6 7 2 6
5 1
8 9
Inserting into a max-heap
Suppose you want to insert a new value x into the
heap
Create a new node at the “end” of the heap (or
i
insert
t x att the
th end
d off the
th array))
If x is
i <=
< its
it parent,
t done
d
Otherwise we have to restore the heap:
Otherwise,
– Repeatedly swap x with its parent until either x reaches
the root or x becomes <= its parent
67
Inserting into a max-heap
key
9 8 7 6 7 2 6 5 1
0 1 2 3 4 5 6 7 8 9 10
68
Inserting
g An Element Into A Max Heap
p
8 7
6 7 2 6
5 1 7
Complete binary tree with 10 nodes.
9 8 7 6 7 2 6 5 1
0 1 2 3 4 5 6 7 8 9 10
Inserting
g An Element Into A Max Heap
p
8 7
6 7 2 6
5 1 75
New element is 5.
9 8 7 6 7 2 6 5 1 5
0 1 2 3 4 5 6 7 8 9 10
Inserting
g An Element Into A Max Heap
p
8 7
6 7 2 6
5 1 7
New element is 20.
9 8 7 6 7 2 6 5 1 20
0 1 2 3 4 5 6 7 8 9 10
Inserting
g An Element Into A Max Heap
p
8 7
6 2 6
5 1 7
New element is 20.
9 8 7 6 20 2 6 5 1 7
0 1 2 3 4 5 6 7 8 9 10
Inserting
g An Element Into A Max Heap
p
6 8 2 6
5 1 7
New element is 20.
9 20 7 6 8 2 6 5 1 7
0 1 2 3 4 5 6 7 8 9 10
Inserting
g An Element Into A Max Heap
p
20
9 7
6 8 2 6
5 1 7
New element is 20.
20 9 7 6 8 2 6 5 1 7
0 1 2 3 4 5 6 7 8 9 10
Inserting
g An Element Into A Max Heap
p
20
9 7
6 8 2 6
5 1 7
Complete binary tree with 11 nodes.
20 9 7 6 8 2 6 5 1 7
0 1 2 3 4 5 6 7 8 9 10 11
Inserting
g An Element Into A Max Heap
p
20
9 7
6 8 2 6
5 1 7
New element is 15.
20 9 7 6 8 2 6 5 1 7 15
0 1 2 3 4 5 6 7 8 9 10 11
Inserting
g An Element Into A Max Heap
p
20
9 7
6 2 6
5 1 7 8
New element is 15.
20 9 7 6 15 2 6 5 1 7 8
0 1 2 3 4 5 6 7 8 9 10 11
Inserting
g An Element Into A Max Heap
p
20
15 7
6 9 2 6
5 1 7 8
New element is 15.
20 15 7 6 9 2 6 5 1 7 8
0 1 2 3 4 5 6 7 8 9 10 11
Insert
79
Complexity
p y Of Insertion
20
15 7
6 9 2 6
5 1 7 8
Complexity is O(log n), where n is heap size.
DeleteMax in max-heaps
The maximum value in a max-heap is at the root!
To delete the max, you can’t just remove the data
value of the root, because every node must hold a
key
IInstead,
t d take
t k the
th last
l t node
d from
f the
th heap,
h move its
it
key to the root, and delete that last node
But now, the tree is no longer a heap (still complete,
but the root keyy value mayy no longer
g be < the keysy
of its children
81
Restore Heap
To bring the structure back to its “heapness”,
we restore
t the
th heap
h
Swap the new root key with the smaller child
child.
Now the potential bug is at the one level
down. If it is not already > the keys of its
children, swap it with its larger child
Keep repeating the last step until the “bug”
key becomes > its children, or the it becomes
a leaf
82
Removing
g The Max Element
DeleteMax
20 15 7 6 9 2 6 5 1 7 8
0 1 2 3 4 5 6 7 8 9 10 11
Removing
g The Max Element
20
15 7
6 9 2 6
5 1 7 8
Max element is in the root.
20 115 7 6 9 2 6 5 1 7 8
0 1 2 3 4 5 6 7 8 9 10 11
Removing
g The Max Element
15 7
6 9 2 6
5 1 7 8
After max element is removed.
1 7
15 6 9 2 6 5 1 7 8
0 1 2 3 4 5 6 7 8 9 10 11
Removing
g The Max Element
15 7
6 9 2 6
5 1 7 8
Heap with 10 nodes.
Reinsert 8 into the heap.
Removing
g The Max Element
15 7
6 9 2 6
5 1 7
Reinsert 8 into the heap.
8 115 7 6 9 2 6 5 1 7
0 1 2 3 4 5 6 7 8 9 10 11
Removing
g The Max Element
15
6 9 2 6
5 1 7
Reinsert 8 into the heap.
1 8 7
15 6 9 2 6 5 1 7
0 1 2 3 4 5 6 7 8 9 10 11
Removing
g The Max Element
15
9 7
6 8 2 6
5 1 7
Reinsert 8 into the heap.
1 9 7
15 6 8 2 6 5 1 7
0 1 2 3 4 5 6 7 8 9 10 11
Removing
g The Max Element
15
9 7
6 8 2 6
5 1 7
Max element is 15.
1 9 7
15 6 8 2 6 5 1 7
0 1 2 3 4 5 6 7 8 9 10 11
Removing
g The Max Element
9 7
6 8 2 6
5 1 7
After max element is removed.
9 7 6 8 2 6 5 1 7
0 1 2 3 4 5 6 7 8 9 10 11
Removing
g The Max Element
9 7
6 8 2 6
5 1 7
Heap with 9 nodes.
9 7 6 8 2 6 5 1 7
0 1 2 3 4 5 6 7 8 9 10 11
Removing
g The Max Element
9 7
6 8 2 6
5 1
Reinsert 7.
7 9 7 6 8 2 6 5 1
0 1 2 3 4 5 6 7 8 9 10 11
Removing
g The Max Element
6 8 2 6
5 1
Reinsert 7.
9 7 7 6 8 2 6 5 1
0 1 2 3 4 5 6 7 8 9 10 11
Removing
g The Max Element
8 7
6 7 2 6
5 1
Reinsert 7.
9 8 7 6 7 2 6 5 1
0 1 2 3 4 5 6 7 8 9 10 11
Remove Max
HEAP-EXTRACT-MAX(A)
remove A[1]
A[1] A[n] ; n is HeapSize(A), the length of the heap, not array
n n-1 ; decrease size of heap
Heapify(A,1,n) ; Remake heap to conform to heap properties
Heapify(A,I,n) ; Array A, heapify node I, heapsize is n
; Note that the left and right subtrees of I are also heaps
; Make I’s subtree be a heap.p
If 2I n and A[2I]>A[I]
; see which is largest of current node and its children
then largest 2I
else
l largest
l I
If 2I+1 n and A[2I+1]>A[largest]
then largest 2I+1
If largest I
then swap A[I] A[largest]
Heapify(A,largest,n)
96
Complexity
p y Of Remove Max Element
8 7
6 7 2 6
5 1
Complexity is O(log n).