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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:56:28+00:00 2026-05-26T21:56:28+00:00

The problem: apply a binary search tree to a person object isntead of a

  • 0

The problem: apply a binary search tree to a “person” object isntead of a int value. The person object is composed of a name and a weight. The value of the person object that the tree will organize and search on is the “name” (string) of a person.

Here is a (leaf) tree Node:

 public class Node {
   private Node leftChild;
   private Node rightChild;
   private Node parent;
   private Person person;
   private int height;

   public Node(Node p, Node l, Node r, Person paul, int h) {
     leftChild = l;
     rightChild = r;
     person = paul;
     height = h;
     parent = p;
   }

   public Node(Person paul, Node p) {
     this(p, null, null, paul, 0);
   }

   public Node (Person paul) {
     this(null, null, null, paul, 0);
   }

   public boolean isBalanced() {
     if(leftChild == null && rightChild == null)
       return true;
     else if (leftChild == null)
       return rightChild.getHeight() == 0;
     else if (rightChild == null)
       return leftChild.getHeight() == 0;

      return Math.abs(leftChild.getHeight()- rightChild.getHeight()) < 2;
   }

   public void setParent(Person parent) {
     parent = new Person( parent.getName(), parent.getWeight());
   }

   public Node getParent() {
     return parent;
   }

   public void setLeftChild(Node leftChild) {
     this.leftChild = leftChild;
   }

   public Node getLeftChild() {
     return leftChild;
   }

   public void setRightChild(Node rightChild) {
     this.rightChild = rightChild;
   }

   public Node getRightChild() {
     return rightChild;
   }

    public void setPerson(Person person) {
      Person = new Person(person.getName(), person.getWeight());
    }

    public Person getPerson() {
      return new Person(person.getName(), person.getWeight());
    }

 public void setHeight() {
  if(leftChild == null && rightChild == null)
   height = 0;
  else if (leftChild == null)
   height = rightChild.getHeight() + 1;
  else if (rightChild == null)
   height = leftChild.getHeight() + 1;
  else
   height= leftChild.getHeight() >= rightChild.getHeight()?
     leftChild.getHeight() + 1: rightChild.getHeight() +1;
 }

 public int getHeight() {
  setHeight();
  return height;
 }
}

Here is the BinarySearchTree:

public class BinarySearchTree {
 private Node root;

 public BinarySearchTree(Node root) {
  this.root = root;
 }
 public BinarySearchTree() {
  this.root = (Node)null;
 }

 void setRoot(Node r) {
  root = r;
 }

 Node getRoot() {
  return root;
 }

 public Node findParent(Person person, Node node) {
  if (node.getPerson() == person)//Error in code here
   return (Node)null; // root itself
  else if(node.getLeftChild() != null && node.getLeftChild().getPerson() == person)
   return node;
  else if(node.getRightChild() != null && node.getRightChild().getPerson() == person)
   return node;
  else if (node.getPerson().getName().compareTo(" a ")  > 0 ){
   return findParent(person, node.getLeftChild());}
  else
   return findParent(person, node.getRightChild());
 }

 public Node insertNode(Person person) {
  return insertNode(person, null, null);
 }

 public Node insertNode(Person person, Node node, Node parent) {
  if(node == (Node)null)
   node = new Node(parent, null, null, person, 0);
  else if (person.getName().compareTo(node.getPerson().getName()) < 0)
   node.setLeftChild(insertNode(person,node.getLeftChild(), node));
  else if (person.getName().compareTo(node.getPerson().getName()) > 0)
   node.setRightChild(insertNode(person, node.getRightChild(), node));
  node.setHeight();
  return node;
 }

 /* in-order traversal for showing inside of tree */
 public void traverseInOrder(Node node) {
  if(node.getLeftChild() != (Node)null)
   traverseInOrder(node.getLeftChild());
  System.out.print("Value: " + '\n' + node.getPerson().toString() + ", Height: " +
    node.getHeight() + ", Parent: ");

  Node n = findParent(node.getPerson(), getRoot());
  if(n == (Node)null)
   System.out.println("root");
  else
   System.out.println(n.getPerson().toString() + "");

  if(node.getRightChild()!= (Node)null)
   traverseInOrder(node.getRightChild());
 }
}




     public Node(Person paul, Node p) {
      this(p, null, null, paul, 0);

     }
     public Node (Person paul) {
      this(null, null, null, paul, 0);
     }

     public boolean isBalanced() {
      if(leftChild == null && rightChild == null)
       return true;
      else if (leftChild == null)
       return rightChild.getHeight() == 0;
      else if (rightChild == null)
       return leftChild.getHeight() == 0;

      return Math.abs(leftChild.getHeight()- rightChild.getHeight()) < 2;
     }

     public void setParent(Person parent) {
      parent = new Person( parent.getName(), parent.getWeight());

     //or is the parent supposed to be a null pointer ????
     }

     public Node getParent() {
      return parent;
     }

     public void setLeftChild(Node leftChild) {
          this.leftChild = leftChild;
         }

         public Node getLeftChild() {
          return leftChild;
         }

         public void setRightChild(Node rightChild) {
          this.rightChild = rightChild;
         }

         public Node getRightChild() {
          return rightChild;
         }

     public void setPerson(Person Person) {
     Person = new Person(person.getName(), person.getWeight());
     }

     public Person getPerson() {
         return new Person(person.getName(), person.getWeight());

     }

     public void setHeight() {
      if(leftChild == null && rightChild == null)
       height = 0;
      else if (leftChild == null)
       height = rightChild.getHeight() + 1;
      else if (rightChild == null)
       height = leftChild.getHeight() + 1;
      else
       height= leftChild.getHeight() >= rightChild.getHeight()?
         leftChild.getHeight() + 1: rightChild.getHeight() +1;
     }

     public int getHeight() {
      setHeight();
      return height;
     }

}
  • 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-26T21:56:29+00:00Added an answer on May 26, 2026 at 9:56 pm

    The problem is that

    if (node.getPerson() == person)
    

    tests if these objects have the same address in memory, not if they have the same “logical” value. You should replace with:

    if (node.getPerson().getName().equals(person.getName()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: Ajax suggest-search on [ n ] ingredients in recipes. That is: match recipes
I have a very strange problem. Under some elusive circumstances I fail to apply
I make DDL changes using SQL Developer's GUI. Problem is, I need to apply
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Problem I have timestamped data, which I need to search based on the timestamp
A binary tree T is semi-balanced if for every node m in T: R(m)/2
I need to create an application for the iPhone that will connect to a
I have a file that should be added as binary but currently is not.
Im trying to apply the state pattern on a multi threaded application.The problem is

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.