You are on page 1of 85

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 02 children per node

Tree Binary Tree


Trees
Terminology
Root no parent
Leaf no child
Interior non-leaf
Height distance from root to leaf
Root node

Interior nodes Height

Leaf nodes
BINARY TREE
A tree is a binary tree, which is either an empty tree
or every node has either no children, a left child, a
right child or both a left and a right child
Figure shows two sample binary trees
A A

B
B C
C

D D E F G

E
H I
BINARY TREE TRAVERSALS
Traversal
- Traversal of a binary tree is where its nodes are
visited in a particular but repetitive order, rendering
a linear order of the nodes or information
represented by them.
There are three tasks in this type of traversal
1. Preorder A

2. Inorder B C
3. Postorder
D E
Fig. 1 Binary Tree
BINARY TREE TRAVERSALS
PreOrder (DLR)
D stands for printing the data
L stands for moving left
R stands for moving right
Preorder traversal of a binary tree entails the following
three steps
1. Process the root node
2. Process the left sub tree
3. Process the right sub tree
BINARY TREE TRAVERSALS
1. Algorithm PreOrder(t)
2. // t is the binary tree. Each node of t has
3. // three fields: lchild, data and rchild
4. {
5. If t<>0 then
6. {
7. Visit(t)
8. Preorder(t lchild)
9. Preorder(t rchild)
10. }
11. }
Binary Tree Traversal
Consider the binary tree shown in fig. 7.1
For preorder traversal, the root, labeled A is visited
first.
Next we traverse the left sub tree, which is rooted at
node B. Again use the pre order method for this sub
tree. Hence we next visit the root of this tree, labeled
B, and then traverse the left sub tree of B
Then, we shall traverse the right sub tree of B, pre
order traversal of the sub tree with root B visits nodes
in the order BDE.
Finally, we traverse the right sub tree of A
Thus the complete preorder traversal of the tree visits
the nodes in the order ABDEC
Binary Tree Traversal
A 1 A A

C B C
1
B C B 1

D E
D E D 2 3 E

Node visited is A ABDE ABDEC


1. Process the root node 2. Process the left sub tree 3. Process the right sub tree

Stages on Preorder Traversal


BINARY TREE TRAVERSALS
InOrder (LDR)
The inorder traversal of a binary tree proceeds as
outlined in the following 3 ordered steps.
1. Process the left sub tree
2. Process the root node
3. Process the right sub tree
BINARY TREE TRAVERSALS
1. Algorithm InOrder (t)
2. // t is the binary tree. Each node of t has
3. // three fields: lchild, data and rchild
4. {
5. If t<>0 then
6. {
7. InOrder(tlchild)
8. Visit(t)
9. InOrder(trchild)
10. }
11. }
Binary Tree Traversal
A A 1 A
1
C B C
B 2 B C

D E D E
D 1 3 E

Node visited is D B E DBEA DBEAC


1. Process the left sub tree 2. Process the root node 3. Process the right sub tree

Stages on Inorder Traversal


BINARY TREE TRAVERSALS
For Inorder traversal, we must begin with the left
sub tree of the root node A, which is rooted at
node B.
The inorder traversal of this sub tree visits the
nodes in the order D B E.
Finally we visit node A and traverse its right sub tree
Thus the complete inorder traversal of the tree
visits the nodes in the order DBEAC.
BINARY TREE TRAVERSALS
PostOrder (LRD)
PostOrder traversal of a binary tree entails these
three ordered steps
1. Process the left subtree
2. Process the right subtree
3. Process the root node
BINARY TREE TRAVERSALS
1. Algorithm PostOrder(t)
2. //t is the binary tree. Each node of t has
3. // three fields: lchild, data and rchild
4. {
5. If t<> 0 then
6. {
7. PostOrder(tlchild)
8. PostOrder(trchild)
9. Visit(t)
10. }
11. }
Binary Tree Traversal
A A A

1
C B C
1 B 3 C B

D E D E
D 1 2 E

Node visited is D E B DEBC DEBCA


1. Process the left sub tree 2. Process the right sub tree 3. Process the root node

Stages on Postorder Traversal


BINARY TREE TRAVERSALS
For PostOrder traversal we must traverse both the left
and right subtrees of each node before visiting the node
itself
Hence we first traverse the left subtree of the root A
The post order traversal of the subtree rooted at B
gives the order A.
The post order traversal of the subtree rooted at B
gives the order DEB
Then visit the right subtree of the root A
The root of a binary tree is always the last node visited
by a post order traversal
Thus the complete post order traversal of the tree visits
the nodes in the order D E B C A
BINARY TREE TRAVERSALS
15

5 16

3 12 20

10 13 18 23

7
BINARY TREE TRAVERSALS
PreOrder Traversal (Root, Left, Right)
15,5,3,12,10,6,7,13,16,20,18,23
Post Order Traversal (Left, Right, Root)
3,7,6,10,13,12,5,18,23,20,16,15
In Order Traversal (Left, Root, Right)
3,5,6,7,10,12,13,15,16,18,20,23
BINARY TREE TRAVERSALS
F

A D

E K H G

Inorder : E A C K F H D G
Preorder : F A E K C D H G
PostOrder : E C K A H G D F
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 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10

25
Binary search Not a binary
trees search tree
BINARY SEARCH TREE
A binary tree T is called a binary search tree, if
each node N of T has the following property.
1. The value at N is greater than every value in
the left sub tree of N
2. The value at N is less than every value in the
right sub tree of N
BINARY SEARCH TREE

Example:
15

14 18

10

7 12
Algorithm to find a node in BST
To find a node with key K in the binary search
tree rooted at Ri. The following steps are taken
1. if the tree is empty the search terminates
unsuccessfully
2. if K= Ki the search terminates successfully.
3. if K < Ki the left sub tree of Ri is searched.
i.e. Ri = left(Ri)
4. if K > Ki the right sub tree of Ri is searched.
i.e. Ri = right(Ri)
Algorithm to find a node in BST
1. Algorithm search (K,r)
2. // K is a keynode, r is a node pointer
3. {
4. If (r=nil) then
5. Return (false) // no node in the tree
6. Else if (K=r.key) then
7. Return (true) // node found in the tree
8. Else if (K<r.key) then
9. Search(K,r.left) //k may be in left subtree, call the
algorithm with left pointer as root
10. Else if (K>r.key) then
11. Search(K,r.right) //k may be in right sub tree, call the
algorithm with right pointer as root
Algorithm to insert a node in a BST
To insert a new node with key K in the binary search
tree rooted at Ri. the following steps are taken.
1. If the tree is empty, the node with key K becomes
the root
2. If K= Ki, the insertion terminates unsuccessfully, the
key is already in the tree
3. If K < Ki, the left subtree of Ri is searched until the
appropriate position for the new node is found
4. If K > Ki , the right subtree of Ri, is searched until the
appropriate position for the new node is found.
5. The code for insert is given below.
Algorithm to insert a node in a BST
1. Algorithm insert (K, r)
2. // K is a key node to be inserted
3. // r is a node pointer
4.{
5. if (r=nil) then
6. {
7. new(r)
8. r.key=K
9. r. leftchild= nil
10. r. rightchild=nil
11.}
12.else if(K<r.key) then
13. insert(K, r, leftchild)
14. else if (K>r.key) then
15. insert(K, r, rightchild)
16. else if(K=r.key) then
17. print(node already present)
18.}
Deleting nodes from BST
To delete a node N with key K from a binary
search tree T, there are three cases
1. when N has no children.
2. when N has exactly one child.
3. when N has two children.
Deleting nodes from BST
1. When N has no children.
The process to delete a node N when it has no
children is simply delete the node N from T and
replace the location of P(N) with nil.
40 40

33 60 60

Consider the binary search tree T. After deleting the node


33 the tree looks like this
Deleting nodes from BST
2. When N has exactly one child.
Delete N from T by replacing P(N) with the
location of only child of N. consider the binary
search tree T
40

30 60

15
Deleting nodes from BST
After deleting the node 30 the tree T look like
this
40

15 60
Deleting nodes from BST
3. When N has two children
Find the smallest element in the descendent
of right child of N; S(N)
Delete node N from T by replacing location of
S(N) in P(N).
Deleting nodes from BST
40 50

30 60 30 60

50 70 70
Algorithm to delete a node from BST
1. Algorithm delete (K,r)
2. // K is a node of key to be deleted
3. {
4. If (r <> nil) then
5. If (K<r.key) then
6. Delete (K,r.leftchild)
7. Else if (K,r.rightchild) then
8. Delete (K,r.rightchild)
9. Else if(r.rightchild=nil) and (r.leftchild=nil) then
10. r=nil
11. Else if(r.leftchild=nil) then
12. r=r.rightchild
13. Else if(r.rightchild=nil) then
14. r=r.leftchild
15. Else // both children are present
16. R.key=deletemin(r.rightchild)
17. }
Representing Binary tree in memory
Let T be a binary tree. There are two ways of
representing T in memory
1. Linked representation
2. Sequential representation.
Linked representation
Suppose the node N(T) correspond to a location K
such that
1) INFO(K) contains the data at the node N
2) LEFT(K) contains the location of the left child of Node
N
3) RIGHT(K) contains the location of the right child of
Node N
Representing Binary tree in memory

B C

D E G

H
LINKED REPRESENTATION OF T IN
MEMORY
INFO LEFT RIGHT

ROOT 1 C 8 0
4 2 E 6 0
3 10

AVAIL 4 A 7 1
5 5 3
6 H 0 0
7 B 9 2
8 G 0 0
9 D 0 0
10 0

Here ROOT will contain the location of the Root R of T.


Sequential Representation
2. Sequential representation
Sequential representation used a single
linear array A as follows
1. The root of T is stored in A[1]
2. if a node N occupies A[K], then its left child
is stored in A[2*K] and its right child is stored
in A[2*K+1].
If A[1] = NULL indicates that the tree is empty.
sequential of representation of in
memory
Array A

1 A
B
2
C
3
D
4
5 E

6 G

7 -

8 -
-
9
H
10
PROBLEM
Show the resulting binary search tree if the
elements are added into in the following order
G, T, E, M, H, P, A, F
Solution
a) item=G b) item = T c) item=E
G G G

T E T
Example
d) item=M e)item=H f)item=p

G G G

E T E T E T

M M M

H H P
Example

g)item=A h)item=F
G G

E T E T

A M A F M

H P H P
Height Balanced Trees or AVL Trees
An empty binary tree is an AVL tree
If non empty, the binary tree T is an AVL tree if (i)
TL and TR, the left and right subtrees of T are also
AVL trees and (ii) |h(TL)-h(TR)|<=1 where h(TL)
and h(TR) are the heights of the left subtree and
right subtree of T respectively
For a node u, bf(u)=(h(uL)-h(uR)) where h(uL) and
h(uR) are the heights of the left and right subtrees
of the node u respectively, is known as the
balance factor (bf) of the node u
In an AVL tree therefore, every node u has a
balance factor bf(u) which may be either 0 or +1
or -1
Height Balanced Trees or AVL Trees
A binary search tree T which is an AVL tree is
referred to as an AVL search tree
Figure illustrates examples of AVL trees and
AVL search trees
The balance factor of each of the nodes is
indicated by the side of the node within
parentheses
Height Balanced Trees or AVL Trees
T g (0) h
(+2)
(+1) (-1)
(a) An empty AVL tree v a (-1) j
(0) p x (0)
b (0)
(b) an AVL tree (c) a non AVL tree
d (-1) (-2)
u
(0) (+1)
b g
(0) t x (+2)
(0)
f
v (-1)
(d) an AVL search tree
w
(0) (e) a non AVL search tree
Height Balanced Trees or AVL Trees
AVL trees and AVL search trees just like binary
trees or binary search trees may be
represented using a linked representation
adopting the same node structure
To facilitate efficient rendering of insert and
delete procedures, a field termed BF may be
included in the node structure to record the
balance factor of the specific node.
Height Balanced Trees or AVL Trees
Algorithm for construction of AVL tree
An AVL tree is constructed in same procedure
as an ordinary binary tree, except that after
the addition of each new node, a check must
be made to ensure that the AVL balance
conditions satisfied. If satisfied, no further
action need be taken. If the new node causes
an imbalance in the tree, some rearrangement
of the trees node must be done
Height Balanced Trees or AVL Trees
Algorithm for construction of AVL tree
1. Insert the new node using binary tree algorithm
2. Beginning with the new node, calculate the
difference in heights of the left and right
subtrees of each node
3. Continue these checks until either the root node
is encountered and all nodes have differences
not greater than 1
4. If an imbalance is found, perform rotation of the
nodes to correct the imbalance
Height Balanced Trees or AVL Trees
Construct the AVL tree for the following
10, 20, 30, 25, 27
- We begin by inserting the integer 10 into the
root. Since this node has no children, the
difference in height of the two subtree is 0,
and this node satisfies the AVL conditions
10
0
- Now we add another node 20
10
-1
20 0
Height Balanced Trees or AVL Trees
- Beginning at the new node 20, we calculate
differences in subtree heights. The node 20
has a difference of 0, and its parent 10 has a
difference of -1. This tree is also an AVL tree.
- Now we insert a third node 30
10 -2

20
-1

30
0
Height Balanced Trees or AVL Trees
- Beginning at the new node 30, we calculate
difference of 0. Working back towards the
root, the node 20 has a difference of -1,which
is satisfied AVL conditions, but the root node
10 has a difference of -2 which violates the
AVL conditions. So we must rearrange the
nodes to restore the balance in the tree
- Now we perform an operation known as
rotation when the tree goes out of balance
- There are two types of rotation used in AVL
trees: Single and Double Rotation
Height Balanced Trees or AVL Trees
- The rules for deciding the type of rotation to use
are as follows
1. When you have found the first node that is out
of balance, consider that node, and the two
nodes in the two layers immediately below this
node
2. If these three nodes lie in a straight line, a single
rotation is needed to restore the balance
3. If these three nodes lie in a dog-leg pattern
(i.e., There is bend in the path), you need a
double rotation to restore the balance.
Height Balanced Trees or AVL Trees
- In this example, the first node where an
imbalance was detected was the root node
10.
- The two layers immediately below this node
are the nodes 20 and 30. These three lie in the
straight line, so we need a single rotation to
restore the balance
- A single rotation involves shifting the middle
node up to replace the top node and top node
down to become the left child of the middle
node. After performing the rotation we obtain
Height Balanced Trees or AVL Trees
20
0

0 10 30 0
- We continue by adding two more nodes 25
and 27. After adding 25, tree satisfies AVL
conditions. 20
-1

0 10 30
1

0 25
Height Balanced Trees or AVL Trees
- After adding 27, the tree looks like this.
-2
20

0 10 30
2

-1
25

27
0
Height Balanced Trees or AVL Trees
- Tracing a path back from the node 27, we find
height differences of 0 at 27, -1 at 25 and 2 at
30. Thus the imbalance is detected at node
30.
- So consider the node 30 and the two nodes
immediately below is (25 and 27).
- These three nodes form a dog-leg pattern,
since there is a bend in the path at node 25.
So we require a double rotation, to correct the
balance.
Height Balanced Trees or AVL Trees
- A double rotation consists of two single
rotations. These two rotations are in opposite
directions. The first rotation occurs on the two
layers below the node where the imbalance
was detected
- We rotate the node 27 up to replace 25, and
25 down to become the left child of 27. The
tree now looks like this.
Height Balanced Trees or AVL Trees
20 -2

10
0 30 2

1 27

0 25
Height Balanced Trees or AVL Trees
- The second rotation involves the three nodes
25, 27 and 30. Node 27 rotates up to replace
30 and node 30 rotates down to become the
right child of 27. Now the tree satisfies AVL
structure, the tree looks like this
Height Balanced Trees or AVL Trees
20 -1

10
0 27 0

25 30
0 0
Height Balanced Trees or AVL Trees
- Construct the AVL tree for the following values
20,15,19,25,23,24
Step 1 : add item 20
20 0

Step 2 : add item 15


20 1

0 15
Height Balanced Trees or AVL Trees
Step 3 : add item 19
20
2
15

-1
19 0
Height Balanced Trees or AVL Trees
Step 4: Imbalance at node 20. Double rotation to be
performed)
2 0
20 19
1
19
15 20
0 0
0 15

First single rotation (left) Second single rotation (right)


Height Balanced Trees or AVL Trees
Step 5: add item 25
19
-1
15 20
0 -1
25
0
Height Balanced Trees or AVL Trees
Step 6: add item 23. Imbalance at node 20. Double
rotation to be performed on node 20,25 and node 23
19
-2

15 20
0 -2

25 +1

0
23
Height Balanced Trees or AVL Trees
Step 7:
-2 19 -1
19

0 -2 0 15 23
0
15 20

23 -1 0 20 0 25

0 25

First single rotation (right) Second single rotation (left)


Height Balanced Trees or AVL Trees
Step 8: add item 24. Imbalance at node19. Single
rotation to be performed on node 19, 23 and 25
Height Balanced Trees or AVL Trees

19 -2 23 0

0 15 23
-1 0 19 25 -1

15 20 24
20 25
0 +1 0 0 0

0 24
GRAPHS
Definition: A graph, G consists of two sets V and
E, Vis a finite non empty set of vertices, these
pair are called edges. V(G)and E(G) will represent
the set of vertices and edges of graph G. we can
write G= (V, E) to represent a graph.
The following figure shows the two graph G1 and
G2
The graph G1 is undirected and G2 is directed
graph.
GRAPH G1 GRAPH G2
1 2 1 2

3
4 3
V(G1)= {1,2,3}
E(G1)= {(1,2),(1,3),(2,1), (2,3),(3,1),(3,2)}

V(G2)={1,2,3,4}
E(G2)={(1,2),(2,2),(2,3),(3,4),(4,1),(4,2),(4,3)}
Directed graph
A graph is directed when its edges have an
orientation and this provide a one-way
connection an indicated by an arrow. The
graph G2 is directed graph.
Weighted graph: When each edge of a graph
is associated with a real no, called its weight,
the graph is said to be weighted. The weighted
graph may directed or undirected.
Degree of vertex: the degree of a vertex in an
undirected graph is the number of edges
incident on it
The degree of a vertex in a directed graph is its
in-degree plus its out-degree
Example: the graph G1 has a degree 3 and the
graph G2 has the degree 5
In-degree and out-degree of graph
For directed graph the in-degree and out-
degree of a vertex are the number of incoming
and outgoing edges respectively.
Example: the following figure has vertex 2 has
in-degree 3, out-degree 2.
1 2

4 3
Complete graph: A simple graph in which
there exists an edge between every pair of
vertices is called a complete graph. Complete
graph of four vertices is shown in the figure. A
complete graph is sometimes also referred to
as a universal graph.
Example
Degree of a graph: The maximum degree of
any vertex in a graph is called degree of a
graph.
Example: the degree of graph is 2 for the
following figure

1 2

3
1) Representation of graph:
1.1 Adjacency matrix: Let G = (V,E) be a graph
with n= |V| m= |E| and V = {v1,v2,,vn}
G can be represented by an n*n matrix.
A=(aij) called the adjacency matrix for G.
A is defined by
aij={1 vi vj E
0, otherwise for 1<=i,j<=h
if G is a weighted graph
aij={W(vi vj) if vi vj E
C otherwise
C is a constant
1.2:Adjacency list: an alternative to the
adjacency matrix representation is an array
indexed by vector number containing linked
list is called adjacency lists.
For each vertex vi, the ith array contains a list
with information on all edges of G that leave
vi
An undirected graph a)
1 2

5 6

3 4
Adjacency matrix of (a)
0 1 1 0 0 0
1 0 0 1 1 0
1 0 0 1 0 0
0 1 10 1 0
0 1 0 1 0 1
0 0 0 0 1 0
Adjacency list structure of (a)
1 2 3 nil

2 1 4 5 nil

3
1 4 nil
4
2 3 5 nil
5
2 4 6 nil
6
5 nil
Directed acyclic graphs: A directed acyclic
graph, or Dag is a directed graph with no
cycles. The following figure is dag

1 2

4 3
Topological sort: It is a process of assigning a
linear ordering to the vertices of a dag so that if
there is an edge from vertex i to vertex j, then I
appears b4 j in the linear ordering.
Algorithm start with the node N, with zero
indegree ie., without any predecessors.
The algorithm will repeat the following steps until
the graph G is empty.
1. Finding a node N with zero in degree
2. Deleting N and its edge from the graph G.
Algorithm for topological sort:
1. Find the indegree of each node N ofG
2. Put in a queue all the nodes with zero indegree.
3. Remove the front node N of the queue.
4.Repeat the following foe each w adjacent to N
a) Reduce the indegree of w
b) if indegree of w is zero then add w to the rear of
thequeue.
5.goto step 4 if queue is not empty.
6. end.

You might also like