I Given a binary tree of integers write a routine which will accept a pointer to
ID: 3702302 • Letter: I
Question
I Given a binary tree of integers write a routine which will accept a pointer to the tree, traverse the entire tree and return a pointer to the smallest element (the element whose info field contains the smallest integer) of the tree as well as the number of nodes in the tree that have both right and left sons. ?. Given a singularly linked list write a routine which will read the information field of each node (which is an integer) and create two separate singularly linked lists. One list will be made up of all the nodes whose information field contains an even integer and the second list will be made up of all the nodes whose information field contains an odd integer. II Convert the following expressions to both Prefix and Postfix / Infix and create the binary trees which represent them A. P (Q+R)+SIT W-X*Y+Z B. (A+B)*C+D E)/F/G/H+I Write a routine which will accept a character string as input and determine if it is in the form of X C Y where X is a string consisting of the letters 'A'and 'B' and Y is the reverse of X. (That is if X = 'ABBAB. then Y-BABBA. At each point in the routine you may read only the next character of the string. The 'C' is the delimiter for X' and 'Y') IV V. Given a doubly linked list write a routine which will travel through the list adding 10 to every node whose information field is an even number and then travel backwards thru the list subtracting 4 from every node whose information field contains an odd number.Explanation / Answer
1)
node* smallest(node *head){ //function to find smallest element in the binary tree
node *temp = head; //temporary variable to make manipulations
if(head == NULL) //if there are no nodes
return NULL;
while(temp->left != NULL) //looping untill left part of tree is null
temp = temp->left;
return temp;
}
int count(node *root){ //function to count no of nodes in a binary tree
int c = 1; //intializing count to one by considering the root node
if (root == NULL) //if root is NULL as there are no nodes returning 0
return 0;
else{ //if not null
c += count(root->left); //calling count function for left subtree
c += count(root->right); //calling count function for right subtree
return c; //returning count
}
}
2)
void seperate(node *head){ //function to create two seperate lists
node* even = NULL,odd = NULL,temp = head,ptemp = NULL; //variables to head point head of even list and odd list
while(head != NULL){ //looping till the end of the list
temp = head; //storing head value in temp
head = head->next; //moving head pointer to next position
temp->next = NULL; //removing link to next nodes
if(head->data % 2 == 0){ //if the data is even number
ptemp = even;
}
else{ //if the data is odd
ptemp = odd;
}
if(ptemp == NULL) //if list is empty
ptemp = temp //assigning head of list to temp
else{ //if list has elements
while(ptemp - > next != NULL) //looping till the end
ptemp = ptemp->next;
ptemp->next = temp; //adding at end of the loop
}
}
}
3)
(a).
P * (Q+R) + S/T * W - X * Y + Z
infix to postix:
p q r + * s t / w * + x y * - z +
(b)
(a+b)*(c+d*e)/f/g/h+i
infix to postfix:
a b + c d e * + * f / g / h / i +
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.