Vertical Sum of given Binary Tree

Vertical order sum of binary tree nodes

Here given code implementation process.

/*
  C Program 
+ Find Vertical Sum in a given Binary Tree
*/
#include<stdio.h>

#include<stdlib.h>
 //structure of Binary Tree node
struct Node {
  int data;
  struct Node *left, *right;
};

struct List {
  int order, data;
  struct List *next;

};
//Create a binary tree nodes and node fields (data,pointer) 
//And returning the reference of newly nodes

struct Node *insert(int data) {
  //create dynamic memory to new binary tree node
  struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
  if (new_node != NULL) {
    //set data and pointer values
    new_node->data = data;
    new_node->left = NULL; //Initially node left-pointer is NULL
    new_node->right = NULL; //Initially node right-pointer is NULL
  } else {
    printf("Memory Overflow\n");
    exit(0); //Terminate program execution
  }
  //return reference
  return new_node;

}
//Add new element into List
//When vertical nodes are already exist in same then 
//updating its value to add new data
void addList(struct List **result, int order, int data) {

  struct List *temp = *result;
  int status = 1;
  //Check vertical node alrady exist or not
  while (temp != NULL) {
    if (temp->order == order) {
      status = 0;
      //when vertical order exist
      temp->data += data; //add vertical node data
      break;
    }
    temp = temp->next;
  }
  if (status == 0) {
    return;
  }

  struct List *new_node = NULL, *per = NULL;

  new_node = (struct List *) malloc(sizeof(struct List));

  new_node->next = NULL;
  new_node->data = data;
  new_node->order = order;
  if ( *result != NULL) {
    temp = *result;
    if (temp->order > order) {
      //smallest element
      new_node->next = *result;
      *result = new_node;
    } else {

      while (temp != NULL && temp->order < order) {
        per = temp;
        temp = temp->next;
      }
      new_node->next = per->next;
      per->next = new_node;

    }

  } else {
    //When first element of list
    *result = new_node;
  }
}
void removeList(struct List **bucket) {
  struct List *temp = NULL;
  while ( *bucket) {
    temp = *bucket;
    *bucket = temp->next;
    free(temp);
    temp = NULL;
  }
}

void get_vertical(struct Node *node, struct List **bucket, int order) {

  if (node != NULL) {

    get_vertical(node->left, bucket, order - 1);

    get_vertical(node->right, bucket, order + 1);
    addList(bucket, order, node->data);
  }
}
void vertical_sum(struct List *bucket) {
  struct List *temp = bucket;

  while (temp != NULL) {
    printf("%4d", temp->data);
    temp = temp->next;
  }

}
int main() {

  struct Node *root = NULL;
  struct List *bucket = NULL;

  /* Make A Binary Tree
  -----------------------
           4
         /  \
        3    10
       /    /  \
      2    9    11
          / \
         5   12
            /  \
           8    7 
  */
  //Insertion of binary tree nodes
  root = insert(4);
  root->left = insert(3);
  root->left->left = insert(2);
  root->right = insert(10);
  root->right->right = insert(11);
  root->right->left = insert(9);
  root->right->left->left = insert(5);

  root->right->left->right = insert(12);
  root->right->left->right->right = insert(7);
  root->right->left->right->left = insert(8);
  get_vertical(root, & bucket, 0);
  vertical_sum(bucket);
  removeList( & bucket);

  return 0;
}

Output

2   8  21  22  18
/*
  C++ Program 
  Find Vertical Sum in a given Binary Tree
*/
#include<iostream>

using namespace std;
class Node {
public:
  int data;
  Node *left, *right;
  Node(int value) {
    this->data = value;
    this->left = this->right = NULL;
  }
};
class MyList {
  public:
  int order, data;
  MyList *next;
  MyList(int level, int value) {
    this->order = level;
    this->data = value;
    this->next = NULL;
  }
};
class BinaryTree {
  public:
  Node *root;
  MyList *bucket;
  BinaryTree() {
    this->root = NULL;
    this->bucket = NULL;
  }
  void addList(int level, int value) {
    MyList *temp = this->bucket;
    int status = 1;
    while (temp != NULL) {
      if (temp->order == level) {
        status = 0;
        temp->data += value;
        break;;
      }
      temp = temp->next;
    }
    if (status == 0) {
      return;
    }
    MyList *new_node = NULL, *per = NULL;
    new_node = new MyList(level, value);
    if (this->bucket != NULL) {
      temp = this->bucket;
      if (temp->order > level) {
        new_node->next = this->bucket;
        this->bucket = new_node;
      } else {
        while (temp != NULL && temp->order < level) {
          per = temp;
          temp = temp->next;
        }
        new_node->next = per->next;
        per->next = new_node;
      }
    } else {
      this->bucket = new_node;
    }
  }
  void removeList() {
    MyList *temp = NULL;
    while (this->bucket != NULL) {
      temp = this->bucket;
      this->bucket = temp->next;
      temp = NULL;
    }
  }
  void get_vertical(Node *head, int level) {
    if (head != NULL) {
      this->get_vertical(head->left, level - 1);
      this->get_vertical(head->right, level + 1);
      this->addList(level, head->data);
    }
  }
  void vertical_sum() {
    MyList *temp = this->bucket;
    while (temp != NULL) {
      cout << "  " << temp->data;
      temp = temp->next;
    }
  }
};

int main() {
  BinaryTree obj ;
  /* Make A Binary Tree
  -----------------------
           4
         /  \
        3    10
       /    /  \
      2    9    11
          / \
         5   12
            /  \
           8    7 
  */
  obj.root = new Node(4);
  obj.root->left = new Node(3);
  obj.root->left->left = new Node(2);
  obj.root->right = new Node(10);
  obj.root->right->right = new Node(11);
  obj.root->right->left = new Node(9);
  obj.root->right->left->left = new Node(5);
  obj.root->right->left->right = new Node(12);
  obj.root->right->left->right->right = new Node(7);
  obj.root->right->left->right->left = new Node(8);
  obj.get_vertical(obj.root, 0);
  obj.vertical_sum();
  obj.removeList();
}

Output

2   8  21  22  18
/*
Java Program 
Find Vertical Sum in a given Binary Tree
*/

//Class of Binary Tree node
class Node {

  public int data;
  public Node left, right;
  //Make a tree node
  public Node(int value) {
    //Assign field values
    data = value;
    left = right = null;
  }
}

class MyList {
  public int order, data;
  public MyList next;
  public MyList(int level, int value) {
    order = level;
    data = value;
    next = null;
  }
}
public class BinaryTree {

  public Node root;
  public MyList bucket;

  public BinaryTree() {
    //Set initial initial values
    root = null;
    bucket = null;
  }
  //Add new element into List
  //When vertical nodes are already exist in same then 
  //updating its value to add new data
  public void addList(int level, int value) {

    MyList temp = this.bucket;
    int status = 1;
    //Check vertical node alrady exist or not
    while (temp != null) {
      if (temp.order == level) {
        status = 0;
        //when vertical order exist
        temp.data += value; //add vertical node data
        break;
      }
      temp = temp.next;
    }
    if (status == 0) {
      return;
    }

    MyList new_node = null, per = null;

    new_node = new MyList(level, value);


    if (this.bucket != null) {
      temp = this.bucket;
      if (temp.order > level) {
        //smallest element
        new_node.next = this.bucket;
        this.bucket = new_node;
      } else {

        while (temp != null && temp.order < level) {
          per = temp;
          temp = temp.next;
        }
        new_node.next = per.next;
        per.next = new_node;

      }

    } else {
      //When first element of list
      this.bucket = new_node;
    }
  }
  public void removeList() {
    MyList temp = null;
    while (this.bucket != null) {
      temp = bucket;
      this.bucket = temp.next;
      temp = null;
    }
  }

  public void get_vertical(Node head, int order) {

    if (head != null) {

      get_vertical(head.left, order - 1);

      get_vertical(head.right, order + 1);
      addList(order, head.data);
    }
  }
  public void vertical_sum() {
    MyList temp = this.bucket;

    while (temp != null) {
      System.out.print("  " + temp.data);
      temp = temp.next;
    }

  }


  public static void main(String[] args) {
    //Make object of Binary Tree
    BinaryTree obj = new BinaryTree();

    /* Make A Binary Tree
    -----------------------
             4
           /  \
          3    10
         /    /  \
        2    9    11
            / \
           5   12
              /  \
             8    7 
    */
    //Binary tree nodes
    obj.root = new Node(4);
    obj.root.left = new Node(3);
    obj.root.left.left = new Node(2);
    obj.root.right = new Node(10);
    obj.root.right.right = new Node(11);
    obj.root.right.left = new Node(9);
    obj.root.right.left.left = new Node(5);

    obj.root.right.left.right = new Node(12);
    obj.root.right.left.right.right = new Node(7);
    obj.root.right.left.right.left = new Node(8);
    obj.get_vertical(obj.root, 0);
    obj.vertical_sum();
    obj.removeList();
  }
}

Output

2   8  21  22  18
/*
C# Program 
Find Vertical Sum in a given Binary Tree
*/
using System;
//Class of Binary Tree node
public class Node {

  public int data;
  public Node left, right;
  //Make a tree node
  public Node(int value) {
    //Assign field values
    data = value;
    left = right = null;
  }
}

public class MyList {
  public int order, data;
  public MyList next;
  public MyList(int level, int value) {
    order = level;
    data = value;
    next = null;
  }
}
public class BinaryTree {

  public Node root;
  public MyList bucket;

  public BinaryTree() {
    //Set initial initial values
    root = null;
    bucket = null;
  }
  //Add new element into List
  //When vertical nodes are already exist in same then 
  //updating its value to add new data
  public void addList(int level, int value) {

    MyList temp = this.bucket;
    int status = 1;
    //Check vertical node alrady exist or not
    while (temp != null) {
      if (temp.order == level) {
        status = 0;
        //when vertical order exist
        temp.data += value; //add vertical node data
        break;
      }
      temp = temp.next;
    }
    if (status == 0) {
      return;
    }

    MyList new_node = null, per = null;

    new_node = new MyList(level, value);


    if (this.bucket != null) {
      temp = this.bucket;
      if (temp.order > level) {
        //smallest element
        new_node.next = this.bucket;
        this.bucket = new_node;
      } else {

        while (temp != null && temp.order < level) {
          per = temp;
          temp = temp.next;
        }
        new_node.next = per.next;
        per.next = new_node;

      }

    } else {
      //When first element of list
      this.bucket = new_node;
    }
  }
  public void removeList() {
    MyList temp = null;
    while (this.bucket != null) {
      temp = bucket;
      this.bucket = temp.next;
      temp = null;
    }
  }

  public void get_vertical(Node head, int order) {

    if (head != null) {

      get_vertical(head.left, order - 1);

      get_vertical(head.right, order + 1);
      addList(order, head.data);
    }
  }
  public void vertical_sum() {
    MyList temp = this.bucket;

    while (temp != null) {
      Console.Write("  " + temp.data);
      temp = temp.next;
    }

  }


  public static void Main(String[] args) {
    //Make object of Binary Tree
    BinaryTree obj = new BinaryTree();

    /*Make A Binary Tree
    -----------------------
             4
           /  \
          3    10
         /    /  \
        2    9    11
            / \
           5   12
              /  \
             8    7 
    */
    //Binary tree nodes
    obj.root = new Node(4);
    obj.root.left = new Node(3);
    obj.root.left.left = new Node(2);
    obj.root.right = new Node(10);
    obj.root.right.right = new Node(11);
    obj.root.right.left = new Node(9);
    obj.root.right.left.left = new Node(5);

    obj.root.right.left.right = new Node(12);
    obj.root.right.left.right.right = new Node(7);
    obj.root.right.left.right.left = new Node(8);
    obj.get_vertical(obj.root, 0);
    obj.vertical_sum();
    obj.removeList();
  }
}

Output

2   8  21  22  18
#Python3 Program 
#Find Vertical Sum in a given Binary Tree

class Node :
  
  def __init__(self, value) :
    self.data = value
    self.left = self.right = None
  

class MyList :

  def __init__(self, level, value) :
    self.order = level
    self.data = value
    self.next = None
  

class BinaryTree :
  
  def __init__(self) :
    self.root = None
    self.bucket = None
  
  def addList(self, level, value) :
    temp = self.bucket
    status = 1
    while (temp != None) :
      if (temp.order == level) :
        status = 0
        temp.data += value
        break
      
      temp = temp.next
    
    if (status == 0) :
      return
    
    new_node = None
    per = None
    new_node = MyList(level, value)
    if (self.bucket != None) :
      temp = self.bucket
      if (temp.order > level) :
        new_node.next = self.bucket
        self.bucket = new_node
      else :
        while (temp != None and temp.order < level) :
          per = temp
          temp = temp.next
        
        new_node.next = per.next
        per.next = new_node
      
    else :
      self.bucket = new_node
    
  
  def removeList(self) :
    temp = None
    while (self.bucket != None) :
      temp = self.bucket
      self.bucket = temp.next
      temp = None
    
  
  def get_vertical(self, head, level) :
    if (head != None) :
      self.get_vertical(head.left, level - 1)
      self.get_vertical(head.right, level + 1)
      self.addList(level, head.data)
    
  
  def vertical_sum(self) :
    temp = self.bucket
    while (temp != None) :
      print(temp.data,end=" ")
      temp = temp.next
    
  
def main() :
  obj = BinaryTree()

  # Make A Binary Tree
  #   
  #             4
  #           /  \
  #          3    10
  #         /    /  \
  #        2    9    11
  #            / \
  #           5   12
  #              /  \
  #             8    7
  #    
  obj.root = Node(4)
  obj.root.left = Node(3)
  obj.root.left.left = Node(2)
  obj.root.right = Node(10)
  obj.root.right.right = Node(11)
  obj.root.right.left = Node(9)
  obj.root.right.left.left = Node(5)
  obj.root.right.left.right = Node(12)
  obj.root.right.left.right.right = Node(7)
  obj.root.right.left.right.left = Node(8)
  obj.get_vertical(obj.root, 0)
  obj.vertical_sum()
  obj.removeList()


if __name__ == "__main__":
  main()

Output

2   8  21  22  18
#Ruby Program 
#Find Vertical Sum in a given Binary Tree
class Node 
  attr_reader :data, :left, :right
  attr_accessor :data, :left, :right
  def initialize(value) 
    @data = value
    @left = @right = nil
  end
end

class MyList 
  attr_reader :order, :data, :next
  attr_accessor :order, :data, :next
  def initialize(level, value) 
    @order = level
    @data = value
    @next = nil
  end
end

class BinaryTree 
  attr_reader :root, :bucket
  attr_accessor :root, :bucket
  def initialize() 
    @root = nil
    @bucket = nil
  end
  def addList(level, value) 
    temp = self.bucket
    status = 1
    while (temp != nil) 
      if (temp.order == level) 
        status = 0
        temp.data += value
        break
      end
      temp = temp.next
    end
    if (status == 0) 
      return
    end
    new_node = nil
    per = nil
    new_node = MyList.new(level, value)
    if (self.bucket != nil) 
      temp = self.bucket
      if (temp.order > level) 
        new_node.next = self.bucket
        self.bucket = new_node
      else 
        while (temp != nil and temp.order < level) 
          per = temp
          temp = temp.next
        end
        new_node.next = per.next
        per.next = new_node
      end
    else 
      self.bucket = new_node
    end
  end
  def removeList() 
    temp = nil
    while (self.bucket != nil) 
      temp = @bucket
      self.bucket = temp.next
      temp = nil
    end
  end
  def get_vertical(head, level) 
    if (head != nil) 
      self.get_vertical(head.left, level - 1)
      self.get_vertical(head.right, level + 1)
      self.addList(level, head.data)
    end
  end
  def vertical_sum() 
    temp = self.bucket
    while (temp != nil) 
      print("  ", temp.data)
      temp = temp.next
    end
  end

end
def main() 
  obj = BinaryTree.new()
  # Make A Binary Tree
  #   
  #             4
  #           /  \
  #          3    10
  #         /    /  \
  #        2    9    11
  #            / \
  #           5   12
  #              /  \
  #             8    7
  #  
  obj.root = Node.new(4)
  obj.root.left = Node.new(3)
  obj.root.left.left = Node.new(2)
  obj.root.right = Node.new(10)
  obj.root.right.right = Node.new(11)
  obj.root.right.left = Node.new(9)
  obj.root.right.left.left = Node.new(5)
  obj.root.right.left.right = Node.new(12)
  obj.root.right.left.right.right = Node.new(7)
  obj.root.right.left.right.left = Node.new(8)
  obj.get_vertical(obj.root, 0)
  obj.vertical_sum()
  obj.removeList()
end
main()

Output

2   8  21  22  18
/*
  Node JS Program
  Find Vertical Sum in a given Binary Tree
*/
class Node {

  constructor(value) {
    this.data = value;
    this.left = this.right = null;
  }
}
class MyList {

  constructor(level, value) {
    this.order = level;
    this.data = value;
    this.next = null;
  }
}
class BinaryTree {

  constructor() {
    this.root = null;
    this.bucket = null;
  }
  addList(level, value) {
    var temp = this.bucket;
    var status = 1;
    while (temp != null) {
      if (temp.order == level) {
        status = 0;
        temp.data += value;
        break;;
      }
      temp = temp.next;
    }
    if (status == 0) {
      return;
    }
    var new_node = null;
    var per = null;
    new_node = new MyList(level, value);
    if (this.bucket != null) {
      temp = this.bucket;
      if (temp.order > level) {
        new_node.next = this.bucket;
        this.bucket = new_node;
      } else {
        while (temp != null && temp.order < level) {
          per = temp;
          temp = temp.next;
        }
        new_node.next = per.next;
        per.next = new_node;
      }
    } else {
      this.bucket = new_node;
    }
  }
  removeList() {
    var temp = null;
    while (this.bucket != null) {
      temp = this.bucket;
      this.bucket = temp.next;
      temp = null;
    }
  }
  get_vertical(head, level) {
    if (head != null) {
      this.get_vertical(head.left, level - 1);
      this.get_vertical(head.right, level + 1);
      this.addList(level, head.data);
    }
  }
  vertical_sum() {
    var temp = this.bucket;
    while (temp != null) {
      process.stdout.write("  " + temp.data);
      temp = temp.next;
    }
  }
}

function main() {
  var obj = new BinaryTree();
  /* Make A Binary Tree
    -----------------------
           4
         /  \
        3    10
       /    /  \
      2    9    11
          / \
         5   12
            /  \
           8    7 
    */
  obj.root = new Node(4);
  obj.root.left = new Node(3);
  obj.root.left.left = new Node(2);
  obj.root.right = new Node(10);
  obj.root.right.right = new Node(11);
  obj.root.right.left = new Node(9);
  obj.root.right.left.left = new Node(5);
  obj.root.right.left.right = new Node(12);
  obj.root.right.left.right.right = new Node(7);
  obj.root.right.left.right.left = new Node(8);
  obj.get_vertical(obj.root, 0);
  obj.vertical_sum();
  obj.removeList();
}
main();

Output

2   8  21  22  18
<?php
/*
  Php Program
  Find Vertical Sum in a given Binary Tree
*/
class Node {
  public $data;
  public $left;
  public $right;

  function __construct($value) {
    $this->data = $value;
    $this->left = $this->right = null;
  }
}
class MyList {
  public $order;
  public $data;
  public $next;

  function __construct($level, $value) {
    $this->order = $level;
    $this->data = $value;
    $this->next = null;
  }
}
class BinaryTree {
  public $root;
  public $bucket;

  function __construct() {
    $this->root = null;
    $this->bucket = null;
  }
  public  function addList($level, $value) {
    $temp = $this->bucket;
    $status = 1;
    while ($temp != null) {
      if ($temp->order == $level) {
        $status = 0;
        $temp->data += $value;
        break;;
      }
      $temp = $temp->next;
    }
    if ($status == 0) {
      return;
    }
    $new_node = null;
    $per = null;
    $new_node = new MyList($level, $value);
    if ($this->bucket != null) {
      $temp = $this->bucket;
      if ($temp->order > $level) {
        $new_node->next = $this->bucket;
        $this->bucket = $new_node;
      } else {
        while ($temp != null && $temp->order < $level) {
          $per = $temp;
          $temp = $temp->next;
        }
        $new_node->next = $per->next;
        $per->next = $new_node;
      }
    } else {
      $this->bucket = $new_node;
    }
  }
  public  function removeList() {
    $temp = null;
    while ($this->bucket != null) {
      $temp = $this->bucket;
      $this->bucket = $temp->next;
      $temp = null;
    }
  }
  public  function get_vertical($head, $level) {
    if ($head != null) {
      $this->get_vertical($head->left, $level - 1);
      $this->get_vertical($head->right, $level + 1);
      $this->addList($level, $head->data);
    }
  }
  public  function vertical_sum() {
    $temp = $this->bucket;
    while ($temp != null) {
      echo("  ". $temp->data);
      $temp = $temp->next;
    }
  }

}

function main() {
  $obj = new BinaryTree();
  /* Make A Binary Tree
  -----------------------
           4
         /  \
        3    10
       /    /  \
      2    9    11
          / \
         5   12
            /  \
           8    7 
  */
  $obj->root = new Node(4);
  $obj->root->left = new Node(3);
  $obj->root->left->left = new Node(2);
  $obj->root->right = new Node(10);
  $obj->root->right->right = new Node(11);
  $obj->root->right->left = new Node(9);
  $obj->root->right->left->left = new Node(5);
  $obj->root->right->left->right = new Node(12);
  $obj->root->right->left->right->right = new Node(7);
  $obj->root->right->left->right->left = new Node(8);
  $obj->get_vertical($obj->root, 0);
  $obj->vertical_sum();
  $obj->removeList();
}

Output

2   8  21  22  18
/*
  Swift 4 Program
  Find Vertical Sum in a given Binary Tree
*/
class Node {
  var data: Int;
  var left: Node? ;
  var right: Node? ;
  init(_ value: Int) {
    self.data = value;
    self.left = nil;
    self.right = nil;
  }
}
class MyList {
  var order: Int;
  var data: Int;
  var next: MyList? ;
  init(_ level: Int, _ value: Int) {
    self.order = level;
    self.data = value;
    self.next = nil;
  }
}
class BinaryTree {
  var root: Node? ;
  var bucket: MyList? ;
  init() {
    self.root = nil;
    self.bucket = nil;
  }
  func addList(_ level: Int, _ value: Int) {
    var temp: MyList? = self.bucket;
    var status: Int = 1;
    while (temp != nil) {
      if (temp!.order == level) {
        status = 0;
        temp!.data += value;
        break;
      }
      temp = temp!.next;
    }
    if (status == 0) {
      return;
    }
    var new_node: MyList? = nil;
    var per: MyList? = nil;
    new_node = MyList(level, value);
    if (self.bucket != nil) {
      temp = self.bucket;
      if (temp!.order > level) {
        new_node!.next = self.bucket;
        self.bucket = new_node;
      } else {
        while (temp != nil && temp!.order < level) {
          per = temp;
          temp = temp!.next;
        }
        new_node!.next = per!.next;
        per!.next = new_node;
      }
    } else {
      self.bucket = new_node;
    }
  }
  func removeList() {
    var temp: MyList? = nil;
    while (self.bucket != nil) {
      temp = self.bucket;
      self.bucket = temp!.next;
      temp = nil;
    }
  }
  func get_vertical(_ head: Node? , _ level : Int) {
    if (head != nil) {
      self.get_vertical(head!.left, level - 1);
      self.get_vertical(head!.right, level + 1);
      self.addList(level, head!.data);
    }
  }
  func vertical_sum() {
    var temp: MyList? = self.bucket;
    while (temp != nil) {
      print(temp!.data, terminator : " ");
      temp = temp!.next;
    }
  }
}
func main() {
  let obj: BinaryTree = BinaryTree();
  /* Make A Binary Tree
  -----------------------
           4
         /  \
        3    10
       /    /  \
      2    9    11
          / \
         5   12
            /  \
           8    7 
  */
  obj.root = Node(4);
  obj.root!.left = Node(3);
  obj.root!.left!.left = Node(2);
  obj.root!.right = Node(10);
  obj.root!.right!.right = Node(11);
  obj.root!.right!.left = Node(9);
  obj.root!.right!.left!.left = Node(5);
  obj.root!.right!.left!.right = Node(12);
  obj.root!.right!.left!.right!.right = Node(7);
  obj.root!.right!.left!.right!.left = Node(8);
  obj.get_vertical(obj.root, 0);
  obj.vertical_sum();
  obj.removeList();
}
main();

Output

2   8  21  22  18


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved