#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
#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
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
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();
* @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;
}
}
}
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);
}
}
}
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));
}
// 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;
}
}
char data;
treeNode leftChild;
treeNode rightChild;
}
}