Sunday, February 20, 2011

WAP to FindOut the Vrtical Sum of nodes values in BInary Tree

#include
#include

/* A binary tree node has data, pointer to left child
and a pointer to right child */

struct node
{
int data;
struct node* left;
struct node* right;
};

int verticalsum[]={0,0,0,0,0,0,0};
//verticalsum[-1]={0};
//verticalsum[-2]={0};

int size=sizeof(verticalsum)/sizeof(int);

void vertcal_sum(struct node *root, int level)
{
if(root!=NULL)
{
verticalsum[level]+=root->data;
vertcal_sum(root->left,level-1);
vertcal_sum(root->right,level+1);
}
}

/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}

int maxDepth(struct node* node)
{
if (node==NULL)
return -1;
else
{
/* compute the depth of each subtree */
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);

/* use the larger one */
if (lDepth > rDepth)
return(lDepth+1);
else return(rDepth+1);
}
}

/* Driver program to test above functions*/
int main()
{ int i=0;
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
//root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);

int depth=maxDepth(root);
printf("Level Order traversal of binary tree is \n");
vertcal_sum(root,depth);


for(i=0;i printf(" %d ", verticalsum[i]);

getchar();
return 0;
}

TC O(n)
Sc O(n) to show the output using array its not auxiliary space
Run Here https://ideone.com/PZeo3


Another Solution is Using HashMap , Its Efficient The Privious One as we are not wasting space . An Anon Commented on This Post, so i am posting it here .

import java.util.HashMap;
public class TreeApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
tree t=new tree();
t.insert('C');
t.insert('E');
t.insert('A');
t.insert('B');
t.insert('D');
t.insert('F');
t.display();
t.printVertically(t.getRoot(), 0);
t.displayHash();
}
}
class tree{
private treeNode root;
private HashMap<Integer,Integer> h;
public treeNode getRoot(){
return root;
}
void insert(char ch){
treeNode parent;
treeNode current=root;
treeNode newNode= new treeNode();
newNode.data=ch;
while(true){
if(root==null){
root=newNode;
return;
}
else{
parent=current;
if(ch<current.data){
current=current.leftChild;
if(current==null){
parent.leftChild=newNode;
break;
}
}
else{
current=current.rightChild;
if(current==null){
parent.rightChild=newNode;
break;
}
}
}
}
}
void display(){
treeNode curr=root;
h=new HashMap<Integer,Integer>();
inorderTraversal(curr);
}
private void inorderTraversal(treeNode curr) {
// TODO Auto-generated method stub
if(curr!=null){
inorderTraversal(curr.leftChild);
System.out.println("data->"+curr.data);
inorderTraversal(curr.rightChild);
}
}
public void printVertically(treeNode root,int level){
if(root!=null){
printVertically(root.leftChild,level-1);
int lvalue;
if(h.get(level)==null){
lvalue=0;
}
else{
lvalue=h.get(level);
}
h.put(level, lvalue+root.data);
printVertically(root.rightChild,level+1);
}
}
public void displayHash(){
for(Integer i:h.keySet())
System.out.println("Hash key->"+i+" Value"+h.get(i));
}
class treeNode{
char data;
treeNode leftChild;
treeNode rightChild;
}
}
TC O(N)
SC O(N)

4 comments :

Unknown said...

Could you please give some examples of the Above problem... ?/

Unknown said...

@aMitra..Dear you can try different test & try to run this program using that the u will get it..it might possible u are able to find out some bug in this..i will b happy to review then it

Happy Coding
Shashank

Arizona said...

why do we need to calculate the max depth of the tree...shouldnt it be the maximum width because in the end that will be the number of vertical levels!

Unknown said...

@Arizona ..I think it won't be true for every case ..correct me if i am wrong ?