Trees
Trees
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
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
7
Subtrees
8
Tree Terminology
The element at the top of the
hierarchy is the root.
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
15
Binary Tree for Expressions
16
Binary Tree Properties
1. Every binary tree with n elements, n > 0, has
exactlyy n-1 edges.
g
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.
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
20
Node Number Property of Full Binary Tree
21
Node Number Property of Full Binary Tree
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
23
Example of Complete Binary Tree
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
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
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
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
– Visit all nodes in the ith level before going into (i+1)th level.
level
47
Level Order Example (visit = print)
48
Time Complexity
The time complexity of each of the four traversal
algorithm
g is O(n)
( ) because each node is visited
exactly once.
49
Binary Tree Construction
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
preorder = ab a a
postorder = ba b b
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
Value(Parent(I)) value(I)
4 3
6 7 9 3
8 6
8 7
6 7 2 6
5 1
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
If x is
i <=
< its
it parent,
t done
d
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
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
8 7
6 2 6
5 1 7
6 8 2 6
5 1 7
20
9 7
6 8 2 6
5 1 7
20
9 7
6 8 2 6
5 1 7
20
9 7
6 8 2 6
5 1 7
20
9 7
6 2 6
5 1 7 8
20
15 7
6 9 2 6
5 1 7 8
79
Complexity
p y Of Insertion
20
15 7
6 9 2 6
5 1 7 8
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
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
15 7
6 9 2 6
5 1 7 8
15 7
6 9 2 6
5 1 7 8
15 7
6 9 2 6
5 1 7
15
6 9 2 6
5 1 7
15
9 7
6 8 2 6
5 1 7
15
9 7
6 8 2 6
5 1 7
9 7
6 8 2 6
5 1 7
9 7
6 8 2 6
5 1 7
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
8 7
6 7 2 6
5 1