April 2014 | C Programming and Computer Geeks

Search MY Blog

Monday, 21 April 2014

How to find the depth of a binary tree


Depth of a binary tree:
Depth of a binary tree is the maximum length of all paths. Nodes from the root to a leaf form a path.
For example, the depth of the binary tree in the below figure is 4, with the longest path through nodes 1, 2, 5, and 7.
Binary Tree Depth
A binary Tree with depth 4

Solution: Depth of a binary tree is the length of the longest path.
If a binary tree has only one node, its depth is 1. If the root node of a binary tree has only a left sub-tree, its depth is the depth of the left sub-tree plus 1(root node). Similarly, its depth is the depth of the right sub-tree plus 1(root node) if the root node has only a right sub-tree. What if the root node has both left sub-tree and right sub-tree? It is the greater/max value of the depth of the left and right sub-trees plus 1(root node).

 For example, the root node of the binary tree in the above figure has both left and right sub-trees. The depth of the left sub-tree rooted at node 2 is 3, and the depth of the right sub-tree rooted at node 3 is 2, so the depth of the whole binary tree is 4; 1 plus the greater value of 3 and 2.

 It is easy to implement this solution recursively, with little modification on the post-order traversal
algorithm, as shown below:

int TreeDepth(Binarytree Node* pRoot)
{
   int nLeft, nRight;
   if(root == NULL)
        return 0;
    nLeft = TreeDepth(pRoot->pLeft);
    nRight = TreeDepth(pRoot->pRight);
    return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);

}

Tuesday, 15 April 2014

Pointer to array and Array of Pointers



Pointers and Arrays
Pointers and Arrays
What Is the difference between the following three declarations?

int *ptr1[5];
int  (*ptr2)[5];
int* (ptr3[5]);



Answer:
int *ptr1[5];
   Here in int *ptr1[5], ptr1 is an array of 5 integer pointers (An array of int pointers).

int  (*ptr2)[5];
   And in int (*ptr2)[5], ptr2 is a pointer to an array of 5 integers (A pointer to an array of integers).

int* (ptr3[5]);
   This is same as ptr1 (An array of int pointers).


What will be the Output of the below program?

void main(void)
{
     int arr1[4] = {1, 3, 5, 7};
     int arr2[4] = {2, 4, 6, 8};

     int (*ptr[2])[4] = {arr1, &arr2};
    
     printf("%d, %d", (*ptr[1])[2], *(**(ptr) + 3));

}

Try to explain (*ptr[1])[2] And *(**(ptr) + 3) expressions and outputs by step by step..
Learning Points: Pointers to array/Array of pointers and resolving pointer expressions.

Program Explanation:
int (*ptr[2])[4] = {arr1, &arr2};
Here ptr is a array of 2 pointers,In which each pointing to an array of 4 integers.
from the above C statement this ptr is assigned as {arr1,&arr2}.
Here arr1 is a base address to array arr1 also called pointer to the first element of array.
And &arr2 is an address of array(arr2) also called entire array address).
ptr array elements(ptr[0],ptr[1]) expects the address of type int(*)[4]. But ptr[0] is assigned arr1,which is of type int*.
So this is the flaw in code. This may not lead to any compilation error, but it gives warning.
Even though you have given base address(arr1) to ptr[0], it will considers it as pointer to an array of 4 int (array address or entire array address).

printf("%d, %d", (*ptr[1])[2], *(**(ptr) + 3));
From the above C Statement

( *ptr[1] )[2]:
( *ptr[1] )[2] ==> (* ( ptr[1] ) ) [2]  ==> (* ( &arr2 ) ) [2] ==> ( arr2 ) [2] ==> arr2[2] ==> which equals the value 6

*(**(ptr) + 3):
**(ptr) ==> *(*ptr) ==> *(&arr1) ==>arr1(base address) 
Now

*(**(ptr) + 3) ==> *( arr1 + 3) ==> Nothing but arr1[3] ==> which equal to the value 7

Output:
6, 7

Please Correct me if anything is wrong. Please share your inputs and suggestions in comments.
Find more on C Pointer Concepts Here


Search This Blog