Write a C program to find the depth or height of a tree.
Here is some C code to get the height of the three
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 […]
Write a C program to determine the number of elements (or size) in a tree.
Try this C program
int tree_size(struct node* node)
{
if (node==NULL)
{
return(0);
}
else
{
return(tree_size(node->left) + tree_size(node->right) + 1);
}
}
Write C code to determine if two trees are identical
Here is a C program using recursion
int identical(struct node* a, struct node* b)
{
if (a==NULL && b==NULL){return(true);}
else if (a!=NULL && b!=NULL)
{
return(a->data == b->data &&
identical(a->left, b->left) &&
[…]
Write a C program to find the mininum value in a binary search tree.
Here is some sample C code. The idea is to keep on moving till you hit the left most node in the tree
int minValue(struct node* node)
{
struct node* current = node;
while (current->left != NULL)
{
current = current->left;
}
return(current->data);
}
On similar lines, to find […]
Write a C program to compute the maximum depth in a tree?
Here is some C code…
int maxDepth(struct node* node)
{
if (node==NULL)
{
return(0);
}
else
{
int leftDepth = maxDepth(node->left);
int rightDepth = maxDepth(node->right);
if (leftDepth > rightDepth) return(leftDepth+1);
else return(rightDepth+1);
}
}
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 will only print the mirror of the tree
void tree_mirror(struct node* node)
{
struct node […]
Write C code to return a pointer to the nth node of an inorder traversal of a BST.
Here is a C program. Study it carefully, it has some Gotchas!
typedef struct node
{
int value;
struct node *left;
struct node *right;
}mynode;
mynode *root;
static ctr;
void nthnode(mynode *root, int n, mynode **nthnode /* POINTER TO A POINTER! */);
int main()
{
mynode *temp;
root = NULL;
// Construct the tree
add(19);
[…]
Write a C program to create a copy of a tree
Here is a C program which does that…
mynode *copy(mynode *root)
{
mynode *temp;
if(root==NULL)return(NULL);
temp = (mynode *) malloc(sizeof(mynode));
temp->value = root->value;
temp->left = copy(root->left);
temp->right = copy(root->right);
return(temp);
}
Write C code to check if a given binary tree is a binary search tree or not?
Here is a C program which checks if a given 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) data)
[…]
Write C code to search for a value in a binary search tree (BST).
Here are a few C programs….
mynode *search(int value, mynode *root)
{
while(root!=NULL && value!=root->value)
{
root = (value < root->value)?root->left:root->right;
}
return(root);
}
Here is another way to do the same
mynode *recursive_search(int item, mynode *root)
{
if(root==NULL || item == root->value){return(root);}
if(iteminfo)return{recursive_search(item, root->left);}
return{recursive_search(item, […]

