• No results found

ALGORITHM FOR BREADTH FIRST SEARCH AND TRAVERSAL:

ADD ON PROGRAMS :

ALGORITHM FOR BREADTH FIRST SEARCH AND TRAVERSAL:

In Breadth first search we start at vertex v and mark it as having been reached (visited) the vertex v is at this time said to be unexplored. A vertex is said to have been explored by an algorithm when the algorithm has visited all vertices adjacent from it. All unvisited vertices adjacent from v are visited next. These are new unexplored vertices. Vertex v has now been explored. The newly visited vertices have not been explored and or put on to the end of a list of unexplored list of vertices. The first vertex on this list is the next to be explored.

Exploration continues until no unexplored vertex is left. The list of unexplored vertices operates as a queue and can be represented using any of the standard queue representations.

Algorithm BFS(v)

//A breadth first search of G is carried out beginning at vertex v. For //any node I, visited[I=1 if I has already been visited. The graph G //and array visited are global; visited[] is initialized to zero.

{

u:=v; //q is a queue of unexplored vertices visited[v]:=1;

repeat {

for all vertices w adjacent from u do

P a g e | 60 Department of Computer Science & Engineering GEC {

if (visited[w]=0) then {

add w to q; //w is unexplored visited[w]:=1;

} }

if q is empty then return; //no unexplored vertex delete u from q; //get first unexplored vertex }until(false);

}

Algorithm BFT(G, n) //Breadth first traversal of G {

for I:=1 to n do //mark all vertices unvisited visited[I]:=0;

for I:=1 to n do if (visited[I]=0) then BFS(i);

}

P a g e | 61 Department of Computer Science & Engineering GEC 4. Write a C program to perform various operations i.e., insertions and deletions

on AVL trees

AVL Trees: Also called as: Height Balanced Binary Search Trees. Search, Insertion, and Deletion can be implemented in worst case O (log n) time.

Definition: An AVL tree is a binary search tree in which

1. The heights of the right subtree and left subtree of the root differ by at most 1 2. The left subtree and the right subtree are themselves AVL trees

3. A node is said to be

left-high if the left subtree has

greater height / right-high if the right subtree has

greater height

equal if the heights of the LST and RST are the same -

Examples: Several examples of AVL trees are shown in Figure1.

P a g e | 62 Department of Computer Science & Engineering GEC Figure 2: An AVL tree with height h

Maximum Height of an AVL Tree: What is the maximum height of an AVL tree having exactly n nodes? To answer this question, we will pose the following question:

What is the minimum number of nodes (sparsest possible AVL tree) an AVL tree of height h can have?

Let Fh be an AVL tree of height h, having the minimum number of nodes. Fh can be visualized as in Figure 2.

Let Fl and Fr be AVL trees which are the left subtree and right subtree, respectively, of Fh. Then Fl or Fr must have height h-2.

Suppose Fl has height h-1 so that Fr has height h-2. Note that Fr has to be an AVL tree having the minimum number of nodes among all AVL trees with height of h-1. Similarly, Fr will have the minimum number of nodes among all AVL trees of height h--2. Thus we have

| Fh| = | Fh - 1| + | Fh - 2| + 1

Where | Fr| denotes the number of nodes in Fr. Such trees are called Fibonacci trees. See Figure 3.

P a g e | 63 Department of Computer Science & Engineering GEC Figure 3: Fibonacci trees

Note that | F0| = 1 and | F1| = 2.

Adding 1 to both sides, we get

| Fh| + 1 = (| Fh - 1| + 1) + (| Fh - 2| + 1)

Thus the numbers | Fh| + 1 are Fibonacci numbers. Using the approximate formula for Fibonacci numbers, we get

| F

h

| + 1

h 1.44log| Fn|

The sparsest possible AVL tree with n nodes has height h 1.44log n

The worst case height of an AVL tree with n nodes is 1.44log n

Algorithm for Insertions and Deletions into an AVL Trees:

While inserting a new node or deleting an existing node, the resulting tree may violate the (stringent) AVL property. To reinstate the AVL property, we use rotations. See Figure 4.

P a g e | 64 Department of Computer Science & Engineering GEC Figure 4: Rotations in a binary search tree

Rotation in a BST:

Left rotation and right rotation can be realized by a three-way rotation of pointers.

Left Rotation:

Temp = p right ;

p right = temp left ; temp left = p ;

p = temp ;

Left rotation and right rotation preserve

BST property

Inorder ordering of keys

Problem Scenarios in AVL Tree Insertions

left sub tree of node has degree higher by >= 2

left child of node is left high (A)

left child or node is right high (B)

right sub tree has degree higher by >= 2

right child of node is left high (C)

right child or node is right high (D)

The AVL tree property may be violated at any node, not necessarily the root. Fixing the AVL property involves doing a series of single or double rotations.

Double rotation involves a left rotation followed or preceded by a right rotation.

In an AVL tree of height h, no more than [h/2] rotations are required to fix the AVL property.

P a g e | 65 Department of Computer Science & Engineering GEC Insertion: Problem Scenario 1: (Scenario D)

Scenario A is symmetrical to the above. See Figure 5.

Figure 5: Insertion in AVL trees: Scenario D

P a g e | 66 Department of Computer Science & Engineering GEC Insertion: Problem Scenario 2: (Scenario C)

Scenario B is symmetrical to this. See Figure 6.

Figure 6: Insertion in AVL trees: Scenario C

P a g e | 67 Department of Computer Science & Engineering GEC Deletion: Problem Scenario 1:

Depending on the original height of T2, the height of the tree will be either unchanged (height of T2 = h) or gets reduced (if height of T2 = h - 1). See Figure 7.

Figure 7: Deletion in AVL trees: Scenario 1

There is a scenario symmetric to this.

P a g e | 68 Department of Computer Science & Engineering GEC Deletion: Problem Scenario 2:

See Figure 8. As usual, there is a symmetric scenario.

Figure 8: Deletion in AVL trees: Scenario 2

Related documents