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 6050737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:43:19+00:00 2026-05-23T07:43:19+00:00

How would you implement a binary tree in the code below? I need it

  • 0

How would you implement a binary tree in the code below? I need it to print out the below in numerical order by id.

import java.util.Scanner;

class clubmember {

    public static void main(String[] args) {

        int id;
        String fname, lname;
        Scanner input = new Scanner(System.in);

        System.out.println("ID>");
        id = input.nextInt();

        System.out.println("Fname >");
        fname = input.next();

        System.out.println("lname >");
        lname = input.next();


        Person object1 = new Person(id, fname, lname);

        System.out.println(object1);


    }
}




public class Person {
    private final int id;
    private final String firstName;
    private final String lastName;


    public Person(int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public String toString() {
        return String.valueOf(id) + ": " + firstName + " " + lastName;
    }
}

I was actually asked to use this tree as an example…Can anyone help??

public class BinaryTreeTest {

  public static void main(String[] args) {
    new BinaryTreeTest().run();
  }

  // Node Class
  static class Node {
    Node left;

    Node right;

    int value;

    public Node(int value) {
      this.value = value;
    }
  }

  public void run() {
    // build simple tree 

    Node root = new Node(5);
    System.out.println("Binary Tree Example");
    System.out.println("Building tree with root value of ID " + root.value);
    insert(root, 1);
    insert(root, 8);
    insert(root, 6);
    insert(root, 3);
    insert(root, 9);
    System.out.println("Traversing tree in order");
    printInOrder(root);
    System.out.println("Traversing tree front-to-back from location 7");
    printFrontToBack(root, 7);
  }

  public void insert(Node node, int value) {
    if (value < node.value) {
      if (node.left != null) {
        insert(node.left, value);
      } else {
        System.out.println("  Inserted " + value + " to left of "
            + node.value);
        node.left = new Node(value);
      }
    } else if (value > node.value) {
      if (node.right != null) {
        insert(node.right, value);
      } else {
        System.out.println("  Inserted " + value + " to right of "
            + node.value);
        node.right = new Node(value);
      }
    }
  }

  public void printInOrder(Node node) {
    if (node != null) {
      printInOrder(node.left);
      System.out.println("  Traversed " + node.value);
      printInOrder(node.right);
    }
  }

  /**
   * uses in-order traversal when the origin is less than the node's value
   * 
   * uses reverse-order traversal when the origin is greater than the node's
   * order
   */
  public void printFrontToBack(Node node, int idNumber) {
    if (node == null)
      return;
    if (node.value > idNumber) {
      // print in order
      printFrontToBack(node.left, idNumber);
      System.out.println("  Traversed " + node.value);
      printFrontToBack(node.right, idNumber);
    } else if (node.value < idNumber) {
      // print reverse order
      printFrontToBack(node.right, idNumber);
      System.out.println("  Traversed " + node.value);
      printFrontToBack(node.left, idNumber);
    } else {
      // order doesn't matter
      printFrontToBack(node.left, idNumber);
      printFrontToBack(node.right, idNumber);
    }
  }

}
  • 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-05-23T07:43:20+00:00Added an answer on May 23, 2026 at 7:43 am

    If you want to keep your objects ordered by an id, you can use a TreeMap.

    public static void main(String[] args) {
        Map<Integer, Person> persons = new TreeMap<Integer, Person>();
        persons.put(433, new Person(433, "John", "Smith");
        persons.put(211, new Person(211, "Peter", "Johnson");
        persons.put(556, new Person(556, "Laura", "Walters");
    
        // This loop will print out ordering by id
        for (Integer key: persons.keySet())
            System.out.println(persons.get(key));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I implement a binary search using just an array?
How would you implement a Plugin-system for your Java application? Is it possible to
How would you implement a capacity-limited, generic MruList in C# or Java? I want
What is the dispatcher pattern and how would I implement it in code? I
I would like to implement a binary search algorithm in Perl. My 'array' is
I'm looking for some tips to implement binary --version that would provide good information
I wish to implement a binary tree using references instead of using pointers (which
Going through some excercises to hone my binary tree skills, I decided to implement
I want to write the algorithm of Balanced Binary Search Tree with backtracking would
What is the best way to implement a tree structure (generic - not binary)

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.