TREE
1- Linear Data Structures
Examples: Arrays, Link List, Stack, Queue
2- Non-Linear Data Structures
Examples: Tree, Graphs, Polynomials
Types of Tree
a) Natural Tree
b) Programming Tree
Types of Programming Tree
c) Binary Tree
d) Binary Search Tree
e) AVL/Balance Tree
f) 2/3 Tree
g) 2/3/4 Tree
h) B Tree
i) B+ Tree
Binary TREE
Binary Tree
Binary tree is a tree in which one parent tree has maximum 2 nodes and
minimum 0 node.
Two Types of Binary Tree
a) Strictly Binary Tree
Strictly Binary Tree is such a binary tree in which one parent node can have maximum 2 nodes.
b) Complete Binary Tree
Strictly Binary Tree is such a binary tree in which all the leaf nodes should be at the same level
2
and at each level there can be maximum d+1 or 2level nodes Height
of Tree:
The total number of levels from root node to leaf node is called height of tree.
Leaf Nodes:
Those nodes which do not have further child nodes are called leaf nodes or terminated nodes.
Traversal of Tree
Whenever you visit the each node of tree then this process is called
traversal or tree traversal.
Types of Tree Traversal
1- Pre-Ordered Traversal
2- In-Ordered Traversal
3- Post-Ordered Traversal
Pre-Ordered Traversal:
a) visit the root node
b) traverse the lefts of sub-tree
c) traverse the rights of sub-tree
Traversal of Tree
In-Ordered Traversal:
a) traverse the lefts of sub-tree
b) visit the root node
c) traverse the rights of sub-tree
Post-Ordered Traversal:
a) traverse the lefts of sub-tree
b) traverse the rights of sub-tree
c) visit the root node
Linear Search
Linear Search Algorithm
0 1 2 3 4 5 6 7 8 9
20 15 3 1 45 50 60 75 55 100
for(i=0; i< 10; i++)
if(List[i]== x)
return i;
Complexity:
Best Case= 1
Worst Case= n
Average Case=n/2 where n
represents the total number of records
Binary Search
Binary Search Algorithm
0 1 2 3 4 5 6 7 8 9
1 3 5 15 20 45 50 55 60 75
LB Mid UB
LB=0
UB=9
Mid=(LB+UB)/2
if(List[Mid]==x) return Mid;
if(x>Left[Mid]) LB= Mid+1; else UB=Mid-1;
Best Case: 1 Worst Case: log2n Average Case= log2n/2
Note: always remember that log2n < n
Binary Search Algorithm
int binary_search(int LB, int UB, int List[], int x)
{
int mid;
while(LB<=UB)
{
Mid=(LB+UB)/2;
if(List[Mid]==x)
return Mid;
if(x>Left[Mid]) LB=
Mid+1; else UB=Mid-1;
}
return -1;
}