Friday, 6 November 2015
Total No. of Questions—8] [Total No. of Printed Pages—4
Se at
No. [4657]-573
S.E. (Computer Engineering) (I Semester) EXAMINATION, 2014
DIGITAL ELECTRONICS AND LOGIC DESIGN
(2012 PATTERN)
Time : Two Hours Maximum Marks : 50
N.B. :— (i) Attempt Q. No. 1 or Q. No. 2, Q. No. 3 or Q. No. 4,
Q. No. 5 or Q. No. 6 and Q. No. 7 or Q. No. 8.
(ii) Figures to the right indicate full marks.
(iii) Assume suitable data, if necessary.
1. (a) Do the following conversions : [6]
(i) (101110.0101)2 ( )10
(ii) (432A)16 ( )2
(iii) (428.10)10 ( )2
P.T.O.
[4657]-573 2
(b) Reduce the following using K-map techniques : [4]
f(A, B, C, D) = (0, 2, 3, 8, 9, 12, 13, 15).
(c) What is logic family ? Give the classification of logic family. [2]
Or
2. (a) Minimize the following expression using Quine-McClusky : [6]
f(A, B, C, D) = m (0, 2, 3, 6, 7, 8, 10, 12, 13).
(b) Explain with neat diagram two input CMOS NAND gate. [6]
3. (a) Explain Look Ahead Carry generator in detail. [6]
(b) Explain with neat diagram working of serial- n serial-out 4-bit
shift register. Draw necessary timing diagram. [6]
Or
4. (a) Explain rules for BCD addition with suitable example and design
one digit BCD adder using IC 7483. [6]
(b) Design given sequence generator using J-K FF. Sequence is
1 3 5 6 7 1 [6]
[4657]-573 3 P.T.O.
5. (a) Draw the ASM chart for the following state machine. A 2-bit up
counter is to be designed with output QAQB, and enable signal ‘X’.
If X = 0, then counter changes the state as 00 —01 — 10— 11
— 00. If ‘X’ = 1, then counter should remain in current state.
Design the circuit using JK-FF and suitable MUX. [7]
(b) Write VHDL code for 4-bit adder using structural modeling
style. [6]
Or
6. (a) Write a VHDL code for 8 : 1 MUX using Behavioural
modeling. [7]
(b) Draw an ASM chart, state diagram and state table for synchronous
circuit having the following description.
The circuit has control input C, clock and outputs x, y, and z.
(i) If C = 1, on every clock rising edge the code on output x,
y and z changes from 000 — 010 —100 — 110 — 000 and
repeats.
[4657]-573 4
(ii) If C = 0, the circuit holds the present state. [6]
7. (a) Draw and explain Basic architecture of FPGA in detail. [6]
(b) Implement the following functions using PLA : [7]
f1(A, B, C) = m(0, 3, 4, 7)
f2(A, B, C) = m(1, 2, 5, 7).
Or
8. (a) A combinational circuit is defined by the functions :
f1(A, B, C) = m(3, 5, 7)
f2(A, B, C) = m(4, 5, 7).
Implement the circuit with PLA having 3 input and 3 product
term with 2 output. [7]
(b) Implement 4 : 1 MUX using PAL. [6]
Wednesday, 7 October 2015
shortest path
#include<stdio.h>
#include<conio.h>
#define MAX 1000
void dijkstra(int n,int v,int cost[10][10],int dist[10]);
int main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of Nodes: ");
scanf("%d",&n);
printf("\n Enter the Weight Matrix:\n");
printf("\nEnter 1000 to denote Infinity\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cost[i][j]);
}
}
printf("\n Enter the Source Node:");
scanf("%d",&v);
dijkstra(n,v-1,cost,dist);
printf("\n Shortest Path from Node : %d",v);
printf("\n#################################\n\n");
for(i=0;i<n;i++)
{
printf("Distance to Node: %d is %d\n",i+1,dist[i]);
}
return 0;
}
void dijkstra(int n,int v,int cost[10][10],int dist[10])
{
int i,u,count,w,flag[10],min;
for(i=0;i<n;i++)
{
flag[i]=0;
dist[i]=cost[v][i];
}
count=1;
while(count<n)
{
min=MAX;
for(w=0;w<n;w++)
{
if(dist[w]<min && !flag[w])
{
min=dist[w];
u=w;
}
}
flag[u]=1;
count++;
for(w=0;w<n;w++)
{
if((dist[u]+cost[u][w]<dist[w])&&!flag[w])
{
dist[w]=dist[u]+cost[u][w];
}
}
}
}
#include<conio.h>
#define MAX 1000
void dijkstra(int n,int v,int cost[10][10],int dist[10]);
int main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of Nodes: ");
scanf("%d",&n);
printf("\n Enter the Weight Matrix:\n");
printf("\nEnter 1000 to denote Infinity\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cost[i][j]);
}
}
printf("\n Enter the Source Node:");
scanf("%d",&v);
dijkstra(n,v-1,cost,dist);
printf("\n Shortest Path from Node : %d",v);
printf("\n#################################\n\n");
for(i=0;i<n;i++)
{
printf("Distance to Node: %d is %d\n",i+1,dist[i]);
}
return 0;
}
void dijkstra(int n,int v,int cost[10][10],int dist[10])
{
int i,u,count,w,flag[10],min;
for(i=0;i<n;i++)
{
flag[i]=0;
dist[i]=cost[v][i];
}
count=1;
while(count<n)
{
min=MAX;
for(w=0;w<n;w++)
{
if(dist[w]<min && !flag[w])
{
min=dist[w];
u=w;
}
}
flag[u]=1;
count++;
for(w=0;w<n;w++)
{
if((dist[u]+cost[u][w]<dist[w])&&!flag[w])
{
dist[w]=dist[u]+cost[u][w];
}
}
}
}
news paper using kruskal alog
#include<iostream.h>
#include<stdio.h>
int parent[10]; //initial value 0 due to global initialization
class kruskal
{
int a,b,u,v,i,j,n,noofedges;
int visited[10],min,mincost,cost[10][10];
public:
kruskal()
{
noofedges=1;
mincost=0;
}
void read();
void kruskals(int cost[][10],int n);
};
void kruskal::read()
{
cout<<"Enter the no. of vertices";
cin>>n;
cout<<"Enter the adjacency matrices";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=999;
}
kruskals(cost,n);
}
void kruskal::kruskals(int cost[][10],int n)
{
cout<<"The minimum cost edges are \n";
while(noofedges<n)
{
min=999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(u!=v)
{
noofedges++;
cout<<"\n Edge ( "<<a<<"->"<<b<<")"<<min;
mincost+=min;
parent[v]=u;
}
cost[a][b]=cost[b][a]=999;
}
cout<<"\n \n Minimum cost \t"<<mincost<<" ";
}
main()
#include<stdio.h>
int parent[10]; //initial value 0 due to global initialization
class kruskal
{
int a,b,u,v,i,j,n,noofedges;
int visited[10],min,mincost,cost[10][10];
public:
kruskal()
{
noofedges=1;
mincost=0;
}
void read();
void kruskals(int cost[][10],int n);
};
void kruskal::read()
{
cout<<"Enter the no. of vertices";
cin>>n;
cout<<"Enter the adjacency matrices";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=999;
}
kruskals(cost,n);
}
void kruskal::kruskals(int cost[][10],int n)
{
cout<<"The minimum cost edges are \n";
while(noofedges<n)
{
min=999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(u!=v)
{
noofedges++;
cout<<"\n Edge ( "<<a<<"->"<<b<<")"<<min;
mincost+=min;
parent[v]=u;
}
cost[a][b]=cost[b][a]=999;
}
cout<<"\n \n Minimum cost \t"<<mincost<<" ";
}
main()
java program for queue
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Graphics;
public class queue extends Applet implements ActionListener {
TextField t1, t2;
Button b1, b2, b3, b4;
TextArea T;
static int n;
int i, front=0, rear=0, item, count=0;
String s1, s2;
int queue[];
public void init() {
t1 = new TextField(10);
add(t1);
t1.setText("");
Button b4 = new Button("queue Limit");
add(b4);
b4.addActionListener(this);
t2 = new TextField(10);
add(t2);
t2.setText("");
Button b1 = new Button("Push");
add(b1);
b1.addActionListener(this);
Button b2 = new Button("Pop");
add(b2);
b2.addActionListener(this);
Button b3 = new Button("Display");
add(b3);
b3.addActionListener(this);
T = new TextArea(20, 30);
add(T);
}
public void actionPerformed(ActionEvent e){
Button source = (Button)e.getSource();
if(source.getLabel() == "queue Limit"){
try {
s1 = t1.getText();
n = Integer.parseInt(s1);
queue = new int[n];
}
catch(Exception e1) {
System.out.println(e1.getMessage());
}
}
if(source.getLabel() == "Push"){
try {
if(count < n) {
s2 = t2.getText();
item = Integer.parseInt(s2);
queue[rear]=item;
t2.setText("");
rear++;
count++;
}
else{
T.setText("");
T.setText("queue IS FULL\n");
t2.setText("");
}
}
catch(Exception e1) {
System.out.println(e1.getMessage());
}
}
if (source.getLabel() == "Pop") {
if(count!=0) {
T.setText("");
T.setText("The item deleted is: "+queue[front]+"\n");
front++;
count--;
}
else{
T.setText("");
T.setText("queue IS EMPTY\n");
}
if(rear==n)
rear=0;
}
if (source.getLabel() == "Display") {
int m=0;
T.setText("");
if(count==0) {
T.setText("");
T.setText("queue IS EMPTY\n");
}
else {
for(i=front;m<count;i++,m++)
T.append(" "+queue[i]);
}
}
}
}
import java.applet.*;
import java.awt.event.*;
import java.awt.Graphics;
public class queue extends Applet implements ActionListener {
TextField t1, t2;
Button b1, b2, b3, b4;
TextArea T;
static int n;
int i, front=0, rear=0, item, count=0;
String s1, s2;
int queue[];
public void init() {
t1 = new TextField(10);
add(t1);
t1.setText("");
Button b4 = new Button("queue Limit");
add(b4);
b4.addActionListener(this);
t2 = new TextField(10);
add(t2);
t2.setText("");
Button b1 = new Button("Push");
add(b1);
b1.addActionListener(this);
Button b2 = new Button("Pop");
add(b2);
b2.addActionListener(this);
Button b3 = new Button("Display");
add(b3);
b3.addActionListener(this);
T = new TextArea(20, 30);
add(T);
}
public void actionPerformed(ActionEvent e){
Button source = (Button)e.getSource();
if(source.getLabel() == "queue Limit"){
try {
s1 = t1.getText();
n = Integer.parseInt(s1);
queue = new int[n];
}
catch(Exception e1) {
System.out.println(e1.getMessage());
}
}
if(source.getLabel() == "Push"){
try {
if(count < n) {
s2 = t2.getText();
item = Integer.parseInt(s2);
queue[rear]=item;
t2.setText("");
rear++;
count++;
}
else{
T.setText("");
T.setText("queue IS FULL\n");
t2.setText("");
}
}
catch(Exception e1) {
System.out.println(e1.getMessage());
}
}
if (source.getLabel() == "Pop") {
if(count!=0) {
T.setText("");
T.setText("The item deleted is: "+queue[front]+"\n");
front++;
count--;
}
else{
T.setText("");
T.setText("queue IS EMPTY\n");
}
if(rear==n)
rear=0;
}
if (source.getLabel() == "Display") {
int m=0;
T.setText("");
if(count==0) {
T.setText("");
T.setText("queue IS EMPTY\n");
}
else {
for(i=front;m<count;i++,m++)
T.append(" "+queue[i]);
}
}
}
}
dictionary
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct dict
{
char key[20];
char mean[20];
};
void main()
{
int n,i,f=0;
char k[20];
clrscr();
cout<<"\t\t\t\tDictionary Dpcoe\n\n\n\n" ;
//insert operation
struct dict d[30];
cout<<"how many key u want to enter\n";
cin>>n;
//insret
for(i=1;i<=n;i++)
{
cout<<"enter key:\t";
cin>>d[i].key;
cout<<"enter meaning\t" ;
cin>>d[i].mean;
}
//diplay
cout<<"key \t mean \n" ;
//display
for(i=1;i<=n;i++)
{
cout<<d[i].key<<"\t";
cout<<d[i].mean<<"\n";
}
cout<<"enetr key to serch\n" ;
cin>>k;
// logic for search oprration
for(i=1;i<=n;i++)
{
if(strcmp(d[i].key,k)==0)
{
cout<<"meaninig is \t";
cout<<d[i].mean;
f=1;
}
}
if(f==0)
{ cout<<"update key\n" ;
}
//update
cout<<"enetr key to be updated\n" ;
cin>>k;
for(i=1;i<=n;i++)
{
if(strcmp(d[i].key,k)==0)
{
cout<<"meaninig to be updated \t";
cin>>d[i].mean;
}
}
//diply updated:
cout<<"updated dictionary is\n" ;
for(i=1;i<=n;i++)
{
cout<<d[i].key<<"\t";
cout<<d[i].mean<<"\n";
}
getch();
}
#include<conio.h>
#include<string.h>
struct dict
{
char key[20];
char mean[20];
};
void main()
{
int n,i,f=0;
char k[20];
clrscr();
cout<<"\t\t\t\tDictionary Dpcoe\n\n\n\n" ;
//insert operation
struct dict d[30];
cout<<"how many key u want to enter\n";
cin>>n;
//insret
for(i=1;i<=n;i++)
{
cout<<"enter key:\t";
cin>>d[i].key;
cout<<"enter meaning\t" ;
cin>>d[i].mean;
}
//diplay
cout<<"key \t mean \n" ;
//display
for(i=1;i<=n;i++)
{
cout<<d[i].key<<"\t";
cout<<d[i].mean<<"\n";
}
cout<<"enetr key to serch\n" ;
cin>>k;
// logic for search oprration
for(i=1;i<=n;i++)
{
if(strcmp(d[i].key,k)==0)
{
cout<<"meaninig is \t";
cout<<d[i].mean;
f=1;
}
}
if(f==0)
{ cout<<"update key\n" ;
}
//update
cout<<"enetr key to be updated\n" ;
cin>>k;
for(i=1;i<=n;i++)
{
if(strcmp(d[i].key,k)==0)
{
cout<<"meaninig to be updated \t";
cin>>d[i].mean;
}
}
//diply updated:
cout<<"updated dictionary is\n" ;
for(i=1;i<=n;i++)
{
cout<<d[i].key<<"\t";
cout<<d[i].mean<<"\n";
}
getch();
}
bubble sort
# Program Name: Bubble Sort using python
#array[] contains the element that we want to sort
array = [2,5,4,1,7]
#BUBBLE SORT LOGIC
swapped = True
while (swapped):
swapped = False
for i in range(0,len(array)-1):
if (array[i] > array[i+1]):
temp = array[i]
array[i] = array[i+1]
array[i+1] = temp
swapped = True
# PRINT THE SORTED ARRAY
print (array)
#array[] contains the element that we want to sort
array = [2,5,4,1,7]
#BUBBLE SORT LOGIC
swapped = True
while (swapped):
swapped = False
for i in range(0,len(array)-1):
if (array[i] > array[i+1]):
temp = array[i]
array[i] = array[i+1]
array[i+1] = temp
swapped = True
# PRINT THE SORTED ARRAY
print (array)
python program
def bubbleSort(alist):
for passnum in range(len(alist)-1,0,-1):
for i in range(passnum):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
alist = [54,26,93,17,77,31,44,55,20]
bubbleSort(alist)
print(alist)
<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)
<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def mergeSort(alist):
print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=0
j=0
k=0
while i<len(lefthalf) and j<len(righthalf):
if lefthalf[i]<righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i<len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1
while j<len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)
<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def quickSort(alist):
quickSortHelper(alist,0,len(alist)-1)
def quickSortHelper(alist,first,last):
if first<last:
splitpoint = partition(alist,first,last)
quickSortHelper(alist,first,splitpoint-1)
quickSortHelper(alist,splitpoint+1,last)
def partition(alist,first,last):
pivotvalue = alist[first]
for passnum in range(len(alist)-1,0,-1):
for i in range(passnum):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
alist = [54,26,93,17,77,31,44,55,20]
bubbleSort(alist)
print(alist)
<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)
<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def mergeSort(alist):
print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=0
j=0
k=0
while i<len(lefthalf) and j<len(righthalf):
if lefthalf[i]<righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i<len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1
while j<len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",alist)
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)
<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def quickSort(alist):
quickSortHelper(alist,0,len(alist)-1)
def quickSortHelper(alist,first,last):
if first<last:
splitpoint = partition(alist,first,last)
quickSortHelper(alist,first,splitpoint-1)
quickSortHelper(alist,splitpoint+1,last)
def partition(alist,first,last):
pivotvalue = alist[first]
Thursday, 17 September 2015
C++ Program to Implement AVL Trees
This C++ Program demonstrates operations on AVL Trees.
Here is source code of the C++ Program to demonstrate AVL Trees. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/** C++ program to Implement AVL Tree*/#include<iostream>#include<cstdio>#include<sstream>#include<algorithm>#define pow2(n) (1 << (n))using namespace std;
/** Node Declaration*/struct avl_node{int data;
struct avl_node *left;
struct avl_node *right;
}*root;
/** Class Declaration*/class avlTree{public:
int height(avl_node *);
int diff(avl_node *);
avl_node *rr_rotation(avl_node *);
avl_node *ll_rotation(avl_node *);
avl_node *lr_rotation(avl_node *);
avl_node *rl_rotation(avl_node *);
avl_node* balance(avl_node *);
avl_node* insert(avl_node *, int );
void display(avl_node *, int);
void inorder(avl_node *);
void preorder(avl_node *);
void postorder(avl_node *);
avlTree()
{root = NULL;
}};
/** Main Contains Menu*/int main()
{int choice, item;
avlTree avl;while (1)
{cout<<"\n---------------------"<<endl;
cout<<"AVL Tree Implementation"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the tree"<<endl;
cout<<"2.Display Balanced AVL Tree"<<endl;
cout<<"3.InOrder traversal"<<endl;
cout<<"4.PreOrder traversal"<<endl;
cout<<"5.PostOrder traversal"<<endl;
cout<<"6.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
root = avl.insert(root, item);
break;
case 2:
if (root == NULL)
{cout<<"Tree is Empty"<<endl;
continue;
}cout<<"Balanced AVL Tree:"<<endl;
avl.display(root, 1);
break;
case 3:
cout<<"Inorder Traversal:"<<endl;
avl.inorder(root);
cout<<endl;
break;
case 4:
cout<<"Preorder Traversal:"<<endl;
avl.preorder(root);
cout<<endl;
break;
case 5:
cout<<"Postorder Traversal:"<<endl;
avl.postorder(root);
cout<<endl;
break;
case 6:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}}return 0;
}/** Height of AVL Tree*/int avlTree::height(avl_node *temp)
{int h = 0;
if (temp != NULL)
{int l_height = height (temp->left);
int r_height = height (temp->right);
int max_height = max (l_height, r_height);
h = max_height + 1;
}return h;
}/** Height Difference*/int avlTree::diff(avl_node *temp)
{int l_height = height (temp->left);
int r_height = height (temp->right);
int b_factor= l_height - r_height;
return b_factor;
}/** Right- Right Rotation*/avl_node *avlTree::rr_rotation(avl_node *parent)
{avl_node *temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}/** Left- Left Rotation*/avl_node *avlTree::ll_rotation(avl_node *parent)
{avl_node *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}/** Left - Right Rotation*/avl_node *avlTree::lr_rotation(avl_node *parent)
{avl_node *temp;
temp = parent->left;
parent->left = rr_rotation (temp);
return ll_rotation (parent);
}/** Right- Left Rotation*/avl_node *avlTree::rl_rotation(avl_node *parent)
{avl_node *temp;
temp = parent->right;
parent->right = ll_rotation (temp);
return rr_rotation (parent);
}/** Balancing AVL Tree*/avl_node *avlTree::balance(avl_node *temp)
{int bal_factor = diff (temp);
if (bal_factor > 1)
{if (diff (temp->left) > 0)
temp = ll_rotation (temp);
elsetemp = lr_rotation (temp);
}else if (bal_factor < -1)
{if (diff (temp->right) > 0)
temp = rl_rotation (temp);
elsetemp = rr_rotation (temp);
}return temp;
}/** Insert Element into the tree*/avl_node *avlTree::insert(avl_node *root, int value)
{if (root == NULL)
{root = new avl_node;
root->data = value;
root->left = NULL;
root->right = NULL;
return root;
}else if (value < root->data)
{root->left = insert(root->left, value);
root = balance (root);
}else if (value >= root->data)
{root->right = insert(root->right, value);
root = balance (root);
}return root;
}/** Display AVL Tree*/void avlTree::display(avl_node *ptr, int level)
{int i;
if (ptr!=NULL)
{display(ptr->right, level + 1);
printf("\n");
if (ptr == root)
cout<<"Root -> ";
for (i = 0; i < level && ptr != root; i++)
cout<<" ";
cout<<ptr->data;
display(ptr->left, level + 1);
}}/** Inorder Traversal of AVL Tree*/void avlTree::inorder(avl_node *tree)
{if (tree == NULL)
return;
inorder (tree->left);
cout<<tree->data<<" ";
inorder (tree->right);
}/** Preorder Traversal of AVL Tree*/void avlTree::preorder(avl_node *tree)
{if (tree == NULL)
return;
cout<<tree->data<<" ";
preorder (tree->left);
preorder (tree->right);
}/** Postorder Traversal of AVL Tree*/void avlTree::postorder(avl_node *tree)
{if (tree == NULL)
return;
postorder ( tree ->left );
postorder ( tree ->right );
cout<<tree->data<<" ";
}
$ g++ avl_tree.cpp $ a.out --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 2 Tree is Empty --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 1 Enter value to be inserted: 8 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 2 Balanced AVL Tree: Root -> 8 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 1 Enter value to be inserted: 5 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 2 Balanced AVL Tree: Root -> 8 5 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 1 Enter value to be inserted: 4 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 2 Balanced AVL Tree: 8 Root -> 5 4 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 1 Enter value to be inserted: 11 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 2 Balanced AVL Tree: 11 8 Root -> 5 4 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 1 Enter value to be inserted: 15 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 2 Balanced AVL Tree: 15 11 8 Root -> 5 4 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 1 Enter value to be inserted: 3 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 2 Balanced AVL Tree: 15 11 8 Root -> 5 4 3 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 1 Enter value to be inserted: 6 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 2 Balanced AVL Tree: 15 11 8 6 Root -> 5 4 3 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 1 Enter value to be inserted: 2 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 2 Balanced AVL Tree: 15 11 8 6 Root -> 5 4 3 2 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 4 Preorder Traversal: 5 3 2 4 11 8 6 15 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 5 Postorder Traversal: 2 4 3 6 8 15 11 5 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 3 Inorder Traversal: 2 3 4 5 6 8 11 15 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 2 Balanced AVL Tree: 15 11 8 6 Root -> 5 4 3 2 --------------------- AVL Tree Implementation --------------------- 1.Insert Element into the tree 2.Display Balanced AVL Tree 3.InOrder traversal 4.PreOrder traversal 5.PostOrder traversal 6.Exit Enter your Choice: 6 ------------------ (program exited with code: 1) Press return to continue
Subscribe to:
Comments (Atom)