Vous êtes sur la page 1sur 9


com/search/label/Data%20Structure %20Concepts Problems based on Binary Tree

1.A strictly binary tree contains 10 leaves(nodes dont have children). find the total number of nodes in the tree? Solution 2.A binary tree has 20 nodes. Then how many null branches have the tree?

Solution Answer=21 null branches If the tree has 3 nodes then it has 4 null branches.ie 3+1. If the tree has 5 nodes then it has 6 null branches.ie5+1. In general a binary tree with n nodes has exactly n+1 null nodes. So 20 nodes has 21 null branches.

3.In the given binary tree below, in which location the node 4 is stored in an array?

Solution:The node 4 is stored at location 6. The nodes are stored in the array in the order of root first then left child of root,right child of root, then left child of child1,right child of child1,then left child of child2 ,right child of child 2 and so on.

Here LCn means left child of node n and RCn means right child of node n. In array the index starts from 0. so the node 4 is at location 6. but the index value is 5. 4.Comparing to Incomplete Binary Tree,Complete Binary Tree and Full Binary Tree,Which tree is efficient considering space and time complexities? Solution Complete Binary Tree Full binary tree loses its nature when operationas of insertions and deletions are done. For Incomplete Binary trees extra storage is required and overhead of NULL node checking takes place. So complete binary tree is the better one since the property of complete binary tree is maintained even after operations like additions and deletions are done on it. 5.Draw a Binary Tree for the expression A*B-(C+D)*(P/Q)? Solution

Evaluating a Postfix Expression

Postfix expressions are parenthesis free notation and precedence is already defined so evaluation is done very easily. Example of Postfix expression are

1. AB+C* 2. 234*+5-

Algorithm for Postfix evaluation:

1. Read the tokens from the postfix string one at a time from left to right 2. Initialize an empty stack 3. If the token is an operand, push the operand in to the stack
4. If the token is an operator,then there will be at least 2 operands in the stack.Pop the top 2

elements from the stack and apply the operator to the poped out 2 elements.The result of this operation is push back in to the stack.
5. After all the characters are read,only one element is present in the stack that is the result

Example for Postfix Evaluation:

Example1: Postfix expression-->234*+5Solution: 1. Initially the stack is empty.
2. Read the token 2. 2 is an operand.Push 2 in to the stack. now stack->2

3. Read the token 3.3 is an operand. Push 3 in to the stack. now stack->32 4. Read the token 4.4 is an operand. Push 4 in to the stack. now stack->432
5. Read the token *.* is an operator. pop the top 2 elements 4,3 from the stack and

4*3=12.then push 12 in to the stack. now stack->12 2

6. Read the token +.+is an operator.pop the 2 elements 12,2 from the stack and

12+2=12.then push 12 in to the stack. now stack->14

7. Read the token 5.5 is an operand.Push 5 in to the stack. now stack->5 14 8. Read the token -.- is an operator.pop the 2 elements 14,5 from the stack and 14-5=9.then

push 9 in to the stack. now stack->9 9. result=9 The value for 234*+5- is 9

Exercise for Postfix Evaluation:

1. 234+56**+ 2. 623+-382/+*

Sample Program 1: Simple linear linked list constructed in c.Adding 10 elements into the list and print them Program #include "stdio.h"

#include "stdlib.h" struct Node { //each node contains a data and a pointer to the next node in the list int data; struct Node *next; }; typedef struct Node node; int main() { node *newnode,*head,*first; int i; head=NULL; first=NULL; //Inserting 10 nodes in a list for(i=1;i<=10;i++) { newnode=(node*)malloc(sizeof(node)); newnode->data=i; newnode->next=NULL; if(head==NULL) { head=newnode; first=newnode; //To point the first node in the list } head->next=newnode; head=newnode; } //Print the nodes in the list newnode=first; while(newnode) { printf("%d\t",newnode->data); newnode=newnode->next; } } Output: 1 2 3 4 5 6 7 8 9 10 Sample Program 2

Write a C program to perform the basic operation like insertion,deletion,display in a singly linked list. solution #include<stdio.h> #include<stdlib.h> void Insert_list(int); void Insert_pos(int,int); void delete_list(int); void display(void); void search(int); struct Node { int data; struct Node *next; }; typedef struct Node node; node *head=NULL,*curr=NULL; int main() { int ch,n,m; do { printf("ENter your choice\n1.Insert end of list\n2.Insert at position\n3.Delete\n4.Search\n5.Display\n6.Exit\n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the data to insert\n"); scanf("%d",&n); Insert_list(n); break; case 2: printf("Enter the data to insert\n"); scanf("%d",&n); printf("Enter the position after the data is insert\n"); scanf("%d",&m); Insert_pos(n,m); break; case 3: printf("Enter the data item to be delete\n"); scanf("%d",&n); delete_list(n); break;

case 4: printf("Enter the data item to be search\n"); scanf("%d",&n); search(n); break; case 5: display(); break; case 6: exit(0); default: printf("Enter a valid digit\n"); break; } }while(ch>=1 && ch<=6); } void Insert_list(int n) { node *newnode; //creating a new node with data n newnode=(node *)malloc(sizeof(node)); newnode->data=n; newnode->next=NULL; //check the head node. If head is NULL then the list is empty //insert this node as the head node if(head==NULL) { head=newnode; curr=head; } else { //curr is the pointer pointing the last node of the list curr->next=newnode; curr=newnode; } } void Insert_pos(int n,int pos) { node *nnode,*newnode; int i; if(head==NULL) { printf("The list is empty\n"); return; } //assigning a temporary pointer nnode to point the head node

nnode=head; //finding the position in the list for(i=0;i<pos-1;i++) { nnode=nnode->next; if(nnode==NULL) //reached end of list { printf("There are less than %d nodes in the list\n",pos); return; } } //found the position to insert //create a newnode with given data n newnode=(node *)malloc(sizeof(node)); newnode->data=n; newnode->next=NULL; //inserting after the position newnode->next=nnode->next; nnode->next=newnode; printf("Node with value %d inserted after %d position\n",n,pos); return; } void delete_list(int n) { //Search the given item in the list if it is present delete or print a message node *nnode,*temp; if(head==NULL) { printf("The List is Empty\n"); return; } //If the data is present in the first node if(head->data==n) { temp=head; head=head->next; free(temp); return; } nnode=head; while(nnode->next->next!=NULL) { if(nnode->next->data==n) { temp=nnode->next; nnode->next=temp->next;

free(temp); printf("node with value %d is deleted\n",n); return; } else { nnode=nnode->next; } } //Deleting the last node if(nnode->next->data==n) { temp=nnode->next; nnode->next=NULL; free(temp); printf("The node with value %d is deleted\n",n); return; } //Reached end of list and the item is not present printf("The node with value %d is not found\n",n); return; } void search(int n) { node *nnode; nnode=head; while(nnode) { if(nnode->data==n) { printf("Node with data %d found\n",n); return; } nnode=nnode->next; } printf("Node with data %d is not found\n",n); } void display() { node *nnode; if(head==NULL) { printf("Empty list\n"); return; } nnode=head;

while(nnode) { printf("%d->",nnode->data); nnode=nnode->next; } } Exercise Programs in Linked list

1. Modify sample program 1 to store items in the reverse order. ie while printing the list the

output should be 10 9 8 7 6 5 4 3 2 1.