Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7710989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:15:46+00:00 2026-06-01T01:15:46+00:00

I have this code for BinaryTree creation and traversal class Node { Integer data;

  • 0

I have this code for BinaryTree creation and traversal


class Node
{
    Integer data;
    Node left;
    Node right;
    Node()
    {
        data = null;
        left = null;
        right = null;
    }
}
class BinaryTree
{
    Node head;
    Scanner input = new Scanner(System.in);
    BinaryTree()
    {
        head = null;
    }
    public void createNode(Node temp, Integer value) 
    {
        Node newnode= new Node();
        value = getData();
        newnode.data = value;
        temp = newnode;
        if(head==null)
        {
            head = temp;
        }
        System.out.println("If left child exits for ("+value+") enter y else n");
        if(input.next().charAt(0)=='y')
        {
            createNode(temp.left, value);
        }
        System.out.println("If right child exits for ("+value+") enter y else n");
        if(input.next().charAt(0)=='y')
        {
            createNode(temp.right, value);
        }       
    }
    public Integer getData()
    {
        out.println("Enter the value to insert:");
        return (Integer)input.nextInt();
    }

    public void print()
    {
        inorder(head);
    }
    public void inorder(Node node)
    {
        if(node!=null)
        {
            inorder(node.left);
            System.out.println(node.data);
            inorder(node.right);
        }
        else
            return;
    }
}

class BinaryTreeWorker
{
    static BinaryTree treeObj = null;
    static Scanner input = new Scanner(System.in);
    public static void displaymenu()
    {
        int choice;
        do{
            out.print("\n Basic operations on a tree:");
            out.print("\n 1. Create tree  \n 2. Insert \n 3. Search value \n 4. print list\n Else. Exit \n Choice:");
            choice = input.nextInt();

            switch(choice)
            {
                case 1:
                    treeObj = createBTree();
                    break;
                case 2:
                    treeObj.createNode(null, null);
                    break;
                case 3:
                    //searchnode();
                    break;
                case 4:
                    treeObj.print();
                    break;
                default:
                    return;
            }       
        }while(true);
    }
    public static BinaryTree createBTree()
    {
        return new BinaryTree();
    }
    public static void main(String[] args)
    {
        displaymenu();
    }
}

It compiles and runs. But I think there is something wrong with the inorder traversal.

I created the below tree,

    2
1       3

But it prints only 2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:15:48+00:00Added an answer on June 1, 2026 at 1:15 am

    I have tried solving the problem your way and I have pasted the solution below.. Though I haven’t tested it thoroughly so it might fail in some edge condition.. But I have tested it for one case. Kindly let me know if it fails in some scenario. I would appreciate others help in making this answer better. I agree that this solution is not the most ideal way to code a Binary Tree but it wont hurt this way if some one is just practicing..

    import java.util.Scanner;
    
    
    class Node
    {
        Integer data;
        Node left;
        Node right;
        Node()
        {
            data = null;
            left = null;
            right = null;
        }
    }
    class BinaryTree
    {
        Node head;
        Scanner input = new Scanner(System.in);
        BinaryTree()
        {
            head = null;
        }
    
        public void createNode(Node temp,Node newnode) 
        {
    
            if(head==null)
            {
                System.out.println("No value exist in tree, the value just entered is set to Root");
                head = newnode;
                return;
            }
            if(temp==null)
                temp = head;
    
            System.out.println("where you want to insert this value, l for left of ("+temp.data+") ,r for right of ("+temp.data+")");
            char inputValue=input.next().charAt(0); 
            if(inputValue=='l'){
                if(temp.left==null)
                {
                    temp.left=newnode;
                    System.out.println("value got successfully added to left of ("+temp.data+")");
                    return;
                }else  {
                    System.out.println("value left to ("+temp.data+") is occupied 1by ("+temp.left.data+")");
                    createNode(temp.left,newnode);
                }
            }
            else if(inputValue=='r')
            {
                if(temp.right==null)
                {
                    temp.right=newnode;
                    System.out.println("value got successfully added to right of ("+temp.data+")");
                    return;
    
                }else  {
                    System.out.println("value right to ("+temp.data+") is occupied by ("+temp.right.data+")");
                    createNode(temp.right,newnode);
                }
    
            }else{
                System.out.println("incorrect input plz try again , correctly");
                return;
            }
    
        }
        public Node generateTree(){
            int [] a = new int[10];
            int index = 0; 
            while(index<a.length){
                a[index]=getData();
                index++;
            }
            if(a.length==0 ){
                return null;
            }
            Node newnode= new Node();
            /*newnode.left=null;
            newnode.right=null;*/
            return generateTreeWithArray(newnode,a,0);
    
        }
        public Node generateTreeWithArray(Node head,int [] a,int index){
    
            if(index >= a.length)
                return null;
            System.out.println("at index "+index+" value is "+a[index]);
            if(head==null)
                head= new Node();
            head.data = a[index];
            head.left=generateTreeWithArray(head.left,a,index*2+1);
            head.right=generateTreeWithArray(head.right,a,index*2+2);
            return head;
        }
    
        public Integer getData()
        {
            System.out.println("Enter the value to insert:");
            return (Integer)input.nextInt();
        }
    
        public void print()
        {
            inorder(head);
        }
        public void inorder(Node node)
        {
            if(node!=null)
            {
                inorder(node.left);
                System.out.println(node.data);
                inorder(node.right);
            }
            else
                return;
        }
    }
    
    public class BinaryTreeWorker
    {
        static BinaryTree treeObj = null;
        static Scanner input = new Scanner(System.in);
        public static void displaymenu()
        {
            int choice;
            do{
                System.out.print("\n Basic operations on a tree:");
                System.out.print("\n 1. Create tree  \n 2. Insert \n 3. Search value \n 4. print list\n 5. generate a tree \n Else. Exit \n Choice:");
                choice = input.nextInt();
    
                switch(choice)
                {
                    case 1:
                        treeObj = createBTree();
                        break;
                    case 2:
                        Node newnode= new Node();
                        newnode.data = getData();
                        newnode.left=null;
                        newnode.right=null;
                        treeObj.createNode(treeObj.head,newnode);
                        break;
                    case 3:
                        //searchnode();
                        break;
                    case 4:
                        System.out.println("inorder traversal of list gives follows");
                        treeObj.print();
                        break;
                    case 5:
                        Node tempHead = treeObj.generateTree();
                        System.out.println("inorder traversal of list with head = ("+tempHead.data+")gives follows");
                        treeObj.inorder(tempHead);
                        break;
                    default:
                        return;
                }       
            }while(true);
        }
        public static Integer getData()
        {
            System.out.println("Enter the value to insert:");
            return (Integer)input.nextInt();
        }
        public static BinaryTree createBTree()
        {
            return new BinaryTree();
        }
        public static void main(String[] args)
        {
            displaymenu();
        }
    }
    

    [Update] : Updated the code to generate a binary tree using an array. This will involve less user interaction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which export data from GridView to csv. It works with
I have this code but it doesn't work! public class Trial { public static
i have this code: <!DOCTYPE HTML> <html> <head> <script src=http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/dojo.js type=text/javascript djConfig=parseOnLoad: true></script> <script
I have this code This is the script in my head <script src=jquery.js></script> <script
I have this code which gets info from an other class but I have
I have this code for doing an ajax request to a webservice: var MyCode
I have this code: $(div[id^='intCell']).mouseover(function() { $(this).css({ border:,1px solid #ff097c}); }).mouseout(function() { $(this).css({border:,1px solid
I have this code to show a map using the Virtual Earth API: <script
I have this code here, which is intended to allow any type of arguments:
I have this code in my forms.py : from django import forms from formfieldset.forms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.