Professional Documents
Culture Documents
STRUCTURES
Classification of Data
Structures
Classification of Data
Structures
Non-Primitive Data Structure
TREES
Trees - Introduction
A tree is an abstract data type that stores
elements hierarchically.
With the exception of the top element, each
node.
Sibling Child of same node are called siblings
Leaf or Terminal Node which does not have any child node is called leaf (Terminal)
node.
Sub tree Sub tree represents descendants of a node.
Levels Level of a node represents the generation of a node. If root node is at level 0,
then its next child node is at level 1, its grandchild is at level 2 and so on.
keys Key represents a value of a node based on which a search operation is to be
Example of a Tree
No. of nodes = 9
Height = 4
Highest level = 3
Root node = 8
Leaves = 1,4,7,13
Interior nodes = 3,10,6,14
Ancestors of 6 = 3,8
Descendants of 10 = 14,13
Sibling of 1 = 6
Degree of a node:
Degree of a Tree:
The degree of a tree is the maximum degree of nodes in a given tree
Path:
It is the sequence of consecutive edges from source node to destination
node.
Height of a node:
The height of a node is the max path length from that node to a leaf node.
Height of a tree:
The height of a tree is the height of the root
Depth of a tree:
Depth of a tree is the max level of any leaf in the tree
Level 1
Level 2
Level 3
Level 4
Characteristics of trees
Non-linear data structure
Combines advantages of an ordered array
Searching as fast as in ordered array
Insertion and deletion as fast as in linked list
BINARY TREES
Binary Tree
A binary tree T is defined as a finite set of elements, called nodes,
such that
a)
b)
of R
Binary Tree
Binary Tree
A binary tree is a tree in which no node can
Level 1
TL
TR
Level 2
C
E
Level 3
Binary Trees
Consider a binary tree T, here A is the
Expression Trees
(a+b*c)+((d*e+f)*g)
Left
Info
Right
Binary Tree
The root node of this binary tree is A.
The left sub tree of the root node, which we denoted by LA, is
the set LA = {B,D,E,G} and the right sub tree of the root node,
RA is the set RA={C,F,H}
The root node of LA is node B, the root node of RA is C and so
on
CLASSIFICATION OF
BINARY TREE
1.
If every non-leaf node in a binary tree has nonempty left and right subtrees,
the tree is termed a strictly binary tree.
3.
called
descendants
of nd that
are
We can add special node to leaf node & node that have only one child.
The special nodes added to the tree are called External Node & original
node of tree are called Internal Node
Internal Node
Binary Tree
External Node
Extended Binary Tree
Note: Binary Tree of Height h has maximum (2 h+1 - 1) nodes
REPRESENTATION OF
BINARY TREE
Representation
of
Tree
1. Sequential Representation using array
Tree is store in single array
Number are assigned to each node from leftmost to rightmost node.
Root node always assign no. 1
Left Child is placed at position [2 * K] (k is position of root)
Right Child is placed at position [2 * K + 1]
Size of array is Depends on Depth of tree i.e. 2 d+1
1
A
2
B
3
C
4
D
5
E
A 1
3
C
2
B
4
5
E
D
8
9
H
6
F
7
G
6
F
7 8
G H
9
I
Root (A) =1
L Child = [2*k]= 2 * 1 =2 (B)
R child = [2 * K + 1] = 2 * 1 + 1 = 3 (C)
Root(B)=2
L Child = [2*k]= 2 * 2 =4 (D)
R child = [2 * K + 1] = 2 * 2 + 1 = 5 (E)
Note: If root is placed at 0th position then
L Child = [2 * k + 1 ]
R child = [2 * K + 2 ]
Representation
of
Tree
2. Linked Representation using array
3
B
5
D
E.g.
2 A 3
6
F
7
G
Structure of Node
Struct mode
{
Int Left;
Int Info;
Int Right;
};
Node Tree[10];
Tree [ 1] . Info = A
Tree [ 1 ] . Left = 2
Tree [ 1 ] . Right = 3
Representation of Tree
3. Linked Representation using Pointer
Right
Left Info
E.g.
Structure of Node
Struct node
{
node *Left;
Int Info;
node *Right;
};
node *root;
1
A
2
3
B
5
D
7
F
Note:
If n number of node in B.T. then total number of null link will be n+1
If tree is empty then root will be NULL
BINARY SEARCH
TREE
Example:
k
All<kA
Allkk
20
30
10
12
25
35
10
45
30
45
30
2
25
45
10
25
30
25
Binary
search trees
Not a binary
search tree
children.
A binary search tree is a binary tree in which the nodes are assigned
BST Operations
Four basic BST operations
1
Traversal
2
Search
Insertion
4
Deletion
TRAVERSING BINARY
TREE
Tree traversal
<root><left><right>
In-order (LNR)
<left><root><right>
Post-order (LRN)
<left><right><root>
N Node
L Left subtree
R Right subtree
Pre-order
Traversal
Pre-order Traversal
Root
Left
23 18 12 20 44 35 52
Right
In-order traversal
The
in-order
traversal
of
In-order Traversal
Left
Root
Right
12 18 20 23 35 44 52
Post-order traversal
The
in-order
traversal
of
order
Visit the root node
given tree is
HIDEBFGCA
Post-order Traversal
Left
Right
Root
12 20 18 35 52 44 23
reconstruction.exe
evaluation.exe
node
If the target value is equal, the search is successful.
If target value is less, search the left subtree.
If target value is greater, search the right subtree.
If the subtree is empty, the search is unsuccessful.
20
10
50
-5
-1
20
10
50
70
80
40
30
42
75
85
90
20
10
50
11
BST (Delete)
In deletion, we dont ask for a position. We ask for the
Deleting a leaf
11
case1.exe
-5
-1
20
10
50
-5
-1
case3.exe
20
10
50
2
3
-5
-1
case2.exe
20
10
50
THREADED BINARY
TREE
Threaded
Binary
Tree
A binary tree with n nodes has 2n pointers(one for the left node and another for the right
node) out of which n+1 (because the pointers in the leaves are always null)are always
NULL, So about half the space allocated for pointers is wasted
Utilize this wasted space to contain some useful information
A left NULL pointer can be used to store the address of inorder predecessor of the node
A right NULL pointer can be used to store the address of inorder successor of the node.
These pointers are called threads and a binary tree which implements these pointers
Example
A
B
N C N
N D N
N F N
N G N
A
B
N C N
E
N D
N F
N G
A
B
C N
N D N
F N
G N
C N
N D
Complexity in BST
Operation
Average
Worst Case
Best Case
Search
O(log n)
O(n)
O(1)
Insertion
O(log n)
O(n)
O(1)
Deletion
O(log n)
O(n)
O(1)
ADELSON-VELSKI LANDIS
(AVL) TREE
AVL Tree
Adelson-Velski Landis (AVL) Tree
AVL tree is a self-balancing Binary Search Tree
(BST) where the difference between heights of left
and right subtrees cannot be more than one for all
nodes.
AVL-TREE
NOT AN AVL-TREE
Right Rotate
Left-Rotate
Left-Right Rotate
Right-Left Rotate
Applications of BST
Used in many search applications where data is constantly
entering/leaving, such as the map and set objects in many
languages' libraries.
Storing a set of names, and being able to lookup based on a
prefix of the name. (Used in internet routers.)
Storing a path in a graph, and being able to reverse any
subsection of the path in O(log n) time. (Useful in travelling
salesman problems).
Finding square root of given number
Allows you to do range searches efficiently.
Preliminary Definitions
A set is a collection of objects.
Set A is a subset of set B if all elements of A are in B.
Union of two sets A and B is a set C which consists of
all elements in A and B
Two sets are mutually disjoint if they do not have a
common element.
No
Is A, D partition of S?
No
A Set As A Tree
S = {2, 4, 5, 9, 11, 13, 30}
Some possible tree representations:
13
2
11
30
9
2
5
11
4
11
13
4
13
2
9
30
30
11
30
13
5
11
30
2
Start at the node that represents element i and climb up
13
4
9
5
11
22
30
10
2
1
20
16
14
12
parent.
Use an array table[] such that table[i] is a pointer to
Example
13
4
9
5
11
30
2
1
table[]
0
10
15
Union Operation
union(i,j)
i and j are the roots of two different trees, i != j.
the other.
parent[j] = i
13
4
9
5
11
22
30
2
1
10
20
16
14
union(7,13)
Which tree should become a subtree of the other?
12
Height Rule
Make tree with smaller height a subtree of the other
tree.
Break ties arbitrarily.
13
4
9
5
11
30
22
2
1
10
union(7,13)
20
16
14
12
Weight
Rule
Make tree with fewer number of elements a subtree of the other tree.
Break ties arbitrarily.
7
13
4
9
22
5
11
10
30
2
1
union(7,13)
20
16
14
12
Implementation
Root of each tree must record either its height or the
Height Of A Tree
Suppose we start with single element trees and
floor (log2p) + 1.
2
1
22
g 11
10
30
20
16
a b c
find(1)
Do additional work to make future finds easier.
14
12
Path Compaction
Make all nodes on find path point to tree root.
find(1)
7
13
4
9
2
1
22
g 11
10
30
20
a b c
16
14
12
Path Splitting
Nodes on find path point to former grandparent.
find(1)
7
13
4
9
2
1
22
g 11
10
30
20
a b c
16
14
12
Path Halving
Parent pointer in every other node on find path is changed to former grandparent.
find(1)
7
13
4
9
2
1
22
g 11
10
30
20
a b c
16
14
12
GRAPHS
Graphs
A graph is a way of representing relationships that
Terminology
V = Set of vertices (or nodes)
|V| = # of vertices or cardinality of V (in usual terminology |V| = n)
E = Set of edges, where an edge is defined by two vertices
|E| = # of edges or cardinality of E
A Graph, G is a pair
G = (V, E)
Labeled Graphs:
We may give edges and vertices labels.
Graphing applications often require the
labeling of vertices Edges might also be
numerically labeled. For instance if the
vertices represent cities, the edges might
be labeled to represent distances.
Graph Terminology
Directed (or digraph) & Undirected Graphs
A directed graph is one in which every edge (u,v) has a direction, so that
(u,v) is different from (v,u).
In an undirected graph, there is no distinction between (u,v) and (v,u).
There are two possible situations that can arise in a directed graph between
vertices u and v.
D
A
B
D
Graph Terminology
Two vertices joined by an edge are called the end
Graph Terminology
a
D
g
C
f
Graph Terminology
a
D
g
C
Vertices A and B
are endpoints of edge a
Graph Terminology
a
D
g
C
Vertex A is the
origin of edge a
Graph Terminology
a
D
g
C
Vertex B is the
destination of edge a
Graph Terminology
a
D
g
C
Vertices A and B are
adjacent as they are
endpoints of edge a
Graph Terminology
An edge is said to be incident on a vertex if the vertex is one
Graph Terminology
a
X
g
W
f
Graph Terminology
a
X
g
W
f
Graph Terminology
a
X
g
W
f
Graph Terminology
The degree of a vertex v, denoted deg(v), is the
Graph Terminology
a
X
g
W
f
Graph Terminology
a
X
g
W
f
Graph Terminology
a
X
g
W
f
Graph Terminology
a
X
g
W
f
Graph Terminology
a
X
g
W
f
Graph Terminology
a
d
e
X
g
W
f
Graph Terminology
Path:
Sequence of alternating vertices and edges
begins with a vertex
ends with a vertex
each edge is preceded and followed by its endpoints
Simple Path:
A path where all its edges and vertices are distinct
Graph Terminology
a
P1
X
g
W
f
h
i
Graph Terminology
a
X
g
W
f
Graph Terminology
Cycle:
Circular sequence of alternating vertices and
edges.
Each edge is preceded and followed by its
endpoints.
Simple Cycle:
A cycle such that all its vertices and edges are
unique.
Graph Terminology
a
W
f
Simple cycle
{U, a, V, b, X, g, Y, f, W, c}
Graph Terminology
a
W
f
Y
Non-Simple Cycle
{U, c, W, e, X, g, Y, f, W, d, V, a}
Graph Representations
Adjacency Matrix
Adjacency Lists
Adjacency Matrix
Let G=(V-vertices,E-edges) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is
symmetric;
The adjacency matrix for a digraph need not be symmetric
Adjacency Matrix
1
4
5
1
0
1
1
0
1
1
1
1
0
1
G1
1
1
1
1
2
0 1 0
1
0
1
0 0 0
G2
symmetric
undirected: n2/2
directed: n2
2
3
0
1
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
1
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
1
0
0
G4
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
Adjacency Matrix
Array-based implementation
n 1
adj _ mat[i][ j ]
j 0
ind (vi ) A[ j , i ]
j 0
n 1
Adjacency Lists
Representation
node i.
The nodes in the list L[i] are in no particular order
Adj[1] = {2,3}
Adj[2] = {3}
Adj[3] = {}
Adj[4] = {3}
0
1
2
3
1
0
0
0
2
2
1
1
1
0
2
G3
3
3
3
2
0
G1
0
1
2
1
2
0
1
2
3
4
5
6
7
1
0
0
1
5
4
5
6
2
3
3
2
6
7
G4
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Adjacency List
Linked-list implementation
Reachability
Reachability: Given two vertices 'u' and 'v' of a directed graph G,
we say that 'u' reaches 'v' if G has a directed path from 'u' to 'v'.
That is 'v' is reachable from 'u'.
A directed graph is said to be strongly connected if for any two
Graph Traversal
Problem: Search for a certain node or traverse all
step at a time
4.
5.
current node
Find an unvisited neighbor of the current node, visit it,
and make it the new current node;
If the current node has no unvisited neighbors,
backtrack to the its parent, and make that parent the
new current node;
Repeat steps 3 and 4 until no more nodes can be
visited.
If there are still unvisited nodes, repeat from step 1.
Example
Start vertex: 0
Traverse order: 0, 1, 3, 7, 4, 5, 2, 6
0
2
1
3
4
7
Unexplored Vertex
Visited Vertex
Unexplored Edge
Discovery Edge
Back Edge
Start At Vertex A
D
C
Discovery Edge
D
C
Visited Vertex B
Discovery Edge
D
C
Visited Vertex C
D
C
Back Edge
D
C
Discovery Edge
D
C
Visited Vertex D
D
C
Back Edge
D
C
Discovery Edge
D
C
E
Visited Vertex E
Discovery Edge
D
C
Example
Start vertex: 0
Traverse order: 0, 1, 2, 3, 4, 5, 6, 7
0
2
1
3
4
7
Start Vertex A
Create a sequence L0
insert(A) into L0
Start Vertex A
L0
Start Vertex A
L0
for each v in L0 do
get incident edges of v
Start Vertex A
L0
Start Vertex A
L0
L1
Start Vertex A
L0
L1
Start Vertex A
L0
L1
if edge is unexplored
we get vertex opposite v
say w,
if w is unexplored
Start Vertex A
L0
L1
if w is unexplored
set edge as discovery
Start Vertex A
L0
L1
Start Vertex A
L0
L1
Start Vertex A
L0
L1
Start Vertex A
L0
L1
as L0 is now empty
we continue with list L1
Start Vertex A
L0
L1
as L0 is now empty
we continue with list L1
L2
Start Vertex A
L0
L1
L2
Start Vertex A
L0
L1
L2
Start Vertex A
L0
L1
L2
Start Vertex A
L0
L1
L2
Start Vertex A
L0
L1
L2
Start Vertex A
L0
L1
L2
Connected Component
Find all connected component in a graph G
Select one unvisited vertex v, and start DFS (or BFS) on v
Select one another unvisited vertex v, and start DFS (or BFS)
on v
Example
Start on 0
0, 1, 3, 4, 7 is visited
component
same connected
3
Choose 2
2, 5, 6 is visited
start
destination
A DFS on A
G
D
B
A
B DFS on B
A
Call DFS on G
C
B
A
DFS on C
D
B
A
Call DFS on D
Return to call on B
G
destination
rear
front
BFS Process
rear
front
A
Initial call to BFS on A
Add A to queue
rear
rear
front
D C
Dequeue A
Add B
Dequeue B
Add C, D
start
front
G
Dequeue D
Add G
rear
front
D
Dequeue C
Nothing to add
Graph Applications
Some of the applications of graphs are :
Networks (computer, cities ....)
Maps (any geographic databases )
Graphics : Geometrical Objects
Neighborhood graphs
Flow Problem
Workflow