Professional Documents
Culture Documents
The graph is one non-linear data structure. That is consists of some nodes and their connected edges.
The edges may be director or undirected. This graph can be represented as G(V, E). The following graph
can be represented as G({A, B, C, D, E}, {(A, B), (B, D), (D, E), (B, C), (C, A)})
The graph has two types of traversal algorithms. These are called the Breadth First Search and Depth
First Search.
Algorithm
bfs(vertices, start)
Input: The list of vertices, and the start vertex.
Output: Traverse all of the nodes, if the graph is connected.
Begin
define an empty queue que
at first mark all nodes status as unvisited
add the start vertex into the que
while que is not empty, do
delete item from que and set to u
display the vertex u
for all vertices 1 adjacent with u, do
if vertices[i] is unvisited, then
mark vertices[i] as temporarily visited
add v into the queue
mark
done
mark u as completely visited
done
End
Depth First Search (DFS)
The Depth First Search (DFS) is a graph traversal algorithm. In this algorithm one starting vertex is given,
and when an adjacent vertex is found, it moves to that adjacent vertex first and try to traverse in the same
manner.
Algorithm
dfs(vertices, start)
Input: The list of all vertices, and the start node.
Output: Traverse all nodes in the graph.
Begin
initially make the state to unvisited for all nodes
push start into the stack
while stack is not empty, do
pop element from stack and set to u
display the node u
if u is not visited, then
mark u as visited
for all nodes i connected to u, do
if ith vertex is unvisited, then
push ith vertex into the stack
mark ith vertex as visited
done
done
End
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes
are connected via edges (links) we always start from the root (head) node. That is, we cannot randomly
access a node in a tree. There are three ways which we use to traverse a tree −
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the values it
contains.
In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We
should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order.
We start from A, and following in-order traversal, we move to its left subtree B. B is also traversed in-
order. The process goes on until all the nodes are visited. The output of inorder traversal of this tree will
be −
D→B→E→A→F→C→G
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.
We start from A, and following pre-order traversal, we first visit A itself and then move to its left
subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The output
of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree,
then the right subtree and finally the root node.
We start from A, and following Post-order traversal, we first visit the left subtree B. B is also traversed
post-order. The process goes on until all the nodes are visited. The output of post-order traversal of this
tree will be −
D→E→B→F→G→C→A
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties −
The value of the key of the left sub-tree is less than the value of its parent (root) node's key.
The value of the key of the right sub-tree is greater than or equal to the value of its parent (root)
node's key.
Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree and can be
defined as −
left_subtree (keys) < node (key) ≤ right_subtree (keys)
Representation
BST is a collection of nodes arranged in a way where they maintain BST properties. Each node has a
key and an associated value. While searching, the desired key is compared to the keys in BST and if
found, the associated value is retrieved.
Following is a pictorial representation of BST −
We observe that the root node key (27) has all less-valued keys on the left sub-tree and the higher
valued keys on the right sub-tree.
Basic Operations
Following are the basic operations of a tree −
Search − Searches an element in a tree.
Insert − Inserts an element in a tree.
Pre-order Traversal − Traverses a tree in a pre-order manner.
In-order Traversal − Traverses a tree in an in-order manner.
Post-order Traversal − Traverses a tree in a post-order manner.
Node
Define a node having some data, references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
Search Operation
Whenever an element is to be searched, start searching from the root node. Then if the data is less than
the key value, search for the element in the left subtree. Otherwise, search for the element in the right
subtree. Follow the same algorithm for each node.
Algorithm
struct node* search(int data){
struct node *current = root;
printf("Visiting elements: ");
while(current->data != data){
if(current != NULL) {
printf("%d ",current->data);
//not found
if(current == NULL){
return NULL;
}
}
}
return current;
}
Insert Operation
Whenever an element is to be inserted, first locate its proper location. Start searching from the root
node, then if the data is less than the key value, search for the empty location in the left subtree and
insert the data. Otherwise, search for the empty location in the right subtree and insert the data.
Algorithm
void insert(int data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
while(1) {
parent = current;
if(current == NULL) {
parent->leftChild = tempNode;
return;
}
} //go to right of the tree
else {
current = current->rightChild;
B-Tree has all of the properties of one m-way tree. It has some other properties.
Every node in B-Tree will hold maximum m children
Every node except root and leaves, can hold at least m/2 children
The root nodes must have at least two children.
All leaf nodes must have at same level
To insert an element, the idea is very similar to the BST, but we have to follow some
rules.
Each node has m children, and m-1 elements. If we insert an element into one node,
there are two situations. If the node has elements less than m-1, then the new element
will be inserted directly into the node. If it has m-1 elements, then by taking all elements,
and the element which will be inserted, then take the median of them, and the median
value is send to the root of that node by performing the same criteria, then create two
separate lists from left half and right half of the node
Suppose we want to insert 79 into the tree. At first it will be checked with root, this is
greater than 56. Then it will come to the right most sub-tree. Now it is less than 81, so
move to the left sub-tree. After that it will be inserted into the root. Now there are three
elements [66, 78, 79]. The median value is 78, so 78 will go up, and the root node
becomes [78, 81], and the elements of the node will be split into two nodes. One will
hold 66, and another will hold 79.
B-Tree after inserting 79.
Algorithm
BTreeInsert(root, key)−
Input − The root of the tree, and key to insert We will assume, that the key is not
present into the list
x := Read root
if x is full, then
y := new node
z := new node
Locate the middle object oi, stored in x, move the objects to
the left of oi in to node y
Move the object to the right of oi into node z.
If x is an index node, then move the child pointers accordingly
x->child[1] := address of y
x->child[2] := address of z
end if
Here we will see what are the B-Trees. The B-Trees are specialized m-way search tree.
This can be widely used for disc access. A B-tree of order m, can have maximum m-1
keys and m children. This can store large number of elements in a single node. So the
height is relatively small. This is one great advantage of B-Trees.
Example of B-Tree
This supports basic operations like searching, insertion, deletion. In each node, the item
will be sorted. The element at position i has child before and after it. So children sored
before will hold smaller values, and children present at right will hold bigger values.
The B+ Trees are extended version of B-Trees. This tree supports better insertion,
deletion and searching over B-Tree.
B-trees, the keys and the record values are stored in the internal as well as leaf nodes.
In B+ tree records, can be stored at the leaf node, internal nodes will store the key
values only. The leaf nodes of the B+ trees are also linked like linked list
Example of B+ Tree −
This supports basic operations like searching, insertion, deletion. In each node, the item
will be sorted. The element at position i has child before and after it. So children sored
before will hold smaller values, and children present at right will hold bigger values.