Write down how will you create a binary Tree

Showing Answers 1 - 4 of 4 Answers

John

  • Mar 15th, 2005
 

This article introduces the basic concepts of binary trees, and then works through a series of practice problems with solution code in C/C++ and Java. Binary trees have an elegant recursive pointer structure, so they are a good way to learn recursive pointer algorithms. http://cslibrary.stanford.edu/110/BinaryTrees.html

  Was this answer useful?  Yes

RAAJKUMAR.S

  • May 18th, 2017
 

This is the code for binary tree in java

Code
  1. public class bTree {

  2. bnode root=null;

  3.  

  4. void add(int val){

  5.     bnode newnode= new bnode(val);

  6.     bnode curr = this.root;

  7.     bnode parent = null;

  8.  

  9.     if(this.root == null){

  10.         this.root = newnode;

  11.         System.out.println("Inserted at root    "+newnode.value);

  12.     }

  13.     else{

  14.             while( curr != null){

  15.             System.out.println("Current Left and Right  "+curr.left+"   "+curr.right);

  16.  

  17.             if(curr.left == null){

  18.                 curr =  curr.left;

  19.                 curr = newnode;

  20.                 System.out.println("Inserted Left       " +newnode.value);

  21.                 return;

  22.             }

  23.             if(curr.right ==  null){

  24.                 curr =  curr.right;

  25.                 curr = newnode;

  26.                 System.out.println("Inserted Right      " +newnode.value);

  27.                 return;

  28.             }

  29.         }

  30.     }

  31. }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions