Sie sind auf Seite 1von 14

How to declare a structure of a linked list?

struct node { int value; struct node *next; }; typedef struct node *mynode;

Question: What is almost complete binary tree?. Answer: An almost complete binary tree is a tree in which each node that has a right child also has a left child. Having a left child does not require a node to have a right child. Stated alternately, an almost complete binary tree is a tree where for a right child, there is always a left child, but for a left child there may not be a right child. The number of nodes in a binary tree can be found using this formula: n = 2^h Where n is the amount of nodes in the tree, and h is the height of the tree.

How would you check if a binary tree is balanced?

A tree is considered balanced when the difference between the min depth and max depth does not exceed 1. Recursive algorithms always work well on trees. int mindepth( Node * root ) {
if( !root ) { return 0; } return 1 + min( min_depth( root->left ), min_depth( root->right )); } int max_depth( Node * root ) { if( !root ) { return 0; } return 1 + max( max_depth( root->left ), max_depth( root->right )); } bool is_balanced( Node * root ) { return ( max_depth( root ) - min_depth( root ) ) <= 1 }

Write a C program to delete a tree (i.e, free up its nodes) Solutions: clear(struct node* pNode) { if (pNode != NULL) { clear(pNode->left); clear(pNode->right); delete pNode; } }

Write a C program to determine the number of elements (or size) in a tree. Solution: int tree_size(struct node* node) { if (node==NULL) { return(0); } else { return(tree_size(node->left) + tree_size(node->right) + 1); } }

Write a C program to find the depth or height of a tree. tree_height(mynode *p) { if(p==NULL)return(0); if(p->left){h1=tree_height(p->left);} if(p=>right){h2=tree_height(p->right);} return(max(h1,h2)+1); }

The degree of the leaf is zero. The degree of a tree is the max of its element degrees. A binary tree of height n, h > 0, has at least h and at most (2^h -1) elements in it. The height of a binary tree that contains n, n>0, elements is at most n and atleast log(n+1) to the base 2. Log(n+1) to the base 2 = h n = (2^h - 1)

How to create a copy of a linked list? Write a C program to create a copy of a linked list. copy_linked_lists(struct node *q, struct node **s) { if(q!=NULL) { *s=malloc(sizeof(struct node)); (*s)->data=q->data; (*s)->link=NULL; copy_linked_list(q->link, &((*s)->link)); }}

How to compare two linked lists? Write a C program to compare two linked lists.

int compare_linked_lists(struct node *q, struct node *r) { static int flag; if((q==NULL ) && (r==NULL)) { flag=1; } else { if(q==NULL || r==NULL) { flag=0; } if(q->data!=r->data) { flag=0; } else { compare_linked_lists(q->link,r->link); } } return(flag); }

If you are using C language to implement the heterogeneous linked list, what pointer type will you use? The heterogeneous linked list contains different data types in its nodes and we need a link, pointer, to connect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void pointer is capable of storing pointer to any type as it is a generic pointer type.

What is the minimum number of queues needed to implement the priority queue Two. One queue is used for the actual storing of data, and the other one is used for storing the priorities.

Which data structure is used to perform recursion? The answer is Stack. Stack has the LIFO (Last In First Out) property; it remembers it's caller. Therefore, it knows to whom it should return when the function has to return. On the other hand, recursion makes use of the system stack for storing the return addresses of the function calls. Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written explicit, stack is to be used.

What are some of the applications for the tree data structure? 1- Manipulation of the arithmetic expressions. 2- Symbol table construction. 3- Syntax analysis.

How do you reverse a linked list without using any C pointers? One way is to reverse the data in the nodes without changing the pointers themselves. One can also create a new linked list which is the reverse of the original linked list. A simple C program can do that for you. Please note that you would still use the "next" pointer fields to traverse through the linked list (So in effect, you are using the pointers, but you are not changing them when reversing the linked list).

Das könnte Ihnen auch gefallen