Vous êtes sur la page 1sur 4

Write a C program to find the depth or height of a tree.

#define max(x,y) ((x)>(y)?(x):(y)) struct Bintree{ int element; struct Bintree *left; struct Bintree *right; }; typedef struct Bintree* Tree; int height(Tree T) { if(!T) return -1; else return (1 + max(height(T->left), height(T->right))) }
Write a C program to determine the number of elements (or size) in a tree.

struct Bintree{ int element; struct Bintree *left; struct Bintree *right; }; typedef struct Bintree* Tree; int CountElements( Tree T ) { if(!T) return 0; else return (1 + CountElements(T->left) + CountElements(T->right)); }
Write a C program to delete a tree (i.e, free up its nodes).

DeleteTree(T1) 1. If(T1->left!=Null) 2. DeleteTree(T1->left); 4. If(T1->Right!=Null) 5. DeleteTree(T1->right); 8. 9. T1->parent->next=Null; Free(T1);

Write C code to determine if two trees are identical.

struct Bintree{ int element; struct Bintree *left; struct Bintree *right; }; typedef struct Bintree* Tree; int CheckIdentical( Tree T1, Tree T2 ) { if(!T1 && !T2) // If both tree are NULL then return true return 1; else if((!T1 && T2) || (T1 && !T2)) //If either of one is NULL, return false return 0; else return ((T1->element == T2->element) && CheckIdentical(T1->left, T2-i>left) && CheckIdentical(T1->right, T2->right)); // if element of both tree are same and left and right tree is also

same then both trees are same }


Write a C program to find the mininum value in a binary search tree.

int findMinElement( Tree T) // Recursively finding min element { if( !T ) return NOT_FOUND; else if( !T->left ) return T->element; else return findMin( T->left ); } Note : For non-recursive version just have a while loop and keep going left until you find null.
Write a C program to create a mirror copy of a tree (left nodes become right and right nodes become left).

This C code will create a new mirror copy tree. mynode *copy(mynode *root) { mynode *temp; if(root==NULL)return(NULL); temp = (mynode *) malloc(sizeof(mynode)); temp->value = root->value; temp->left = copy(root->right); temp->right = copy(root->left); return(temp); } This code will not make the copy. Change the existing tree only. void tree_mirror(struct node* node) { struct node *temp; if (node==NULL) { return; } else { tree_mirror(node->left); tree_mirror(node->right); // Swap the pointers in this node temp = node->left; node->left = node->right; node->right = temp; } }
Write C code to return a pointer to the nth node of an inorder traversal of a BST. Write C code to check if a given binary tree is a binary search tree or not.

int isThisABST(struct node* mynode) { if (mynode==NULL) return(true); if (node->left!=NULL && maxValue(mynode->left) > mynode->data) return(false); if (node->right!=NULL && minValue(mynode->right) <= mynode->data) return(false); if (!isThisABST(node->left) || !isThisABST(node->right)) return(false);

return(true); }
Write C code to implement level order traversal of a tree. Using Queue. Similar to BFS. Write C code to count the number of leaves in a tree.

void count_leaf(mynode *root) { if(root!=NULL) { count_leaf(root->left); if(root->left == NULL && root->right==NULL) { // This is a leaf! count++; } count_leaf(root->right); } } Note : Preorder and postorder do not uniquely define a binary tree. Nor do preorder and level order (same example). Nor do postorder and level order.
Construct a tree given its inorder and preorder traversal strings. Similarly construct a tree given its inorder and post order traversal strings.

Find the closest ancestor of two nodes in a tree. How do you convert a tree into an array?

The conversion is based on these rules If i > 1, i/2 is the parent If 2*i > n, then there is no left child, else 2*i is the left child. If (2*i + 1) > n, then there is no right child, else (2*i + 1) is the right child. Converting a tree to an array is very easy Suppose we have a tree like this A B D E C F G

The array representation would be a[1] a[2] a[3] a[4] a[5] a[6] a[7] A B C D E F G That is, for every node at position i in the array, its left child will be stored at position (2*i) and right child at (2*i + 1). The root starts at position 1.
How many different binary trees can be constructed using n nodes?

2^n n
A full N-ary tree has M non-leaf nodes, how many leaf nodes does it have?

M * (N - 1) + 1 ->Try to find out this expression. Easy.


What is a threaded binary tree?

Since traversing the three is the most frequent operation, a method must be devised to improve the speed. This is where Threaded tree comes into picture. If the right link of a node in a tree is NULL, it can be replaced by the address of its inorder successor. An extra field called the rthread is used. If rthread is equal to 1, then it means that the right link of the node points to the inorder success. If its equal to 0, then the right link represents an ordinary link connecting the right subtree.

Vous aimerez peut-être aussi