Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Creating a binary tree in general</h3> <p>It would be easier and would create cleaner code to create a class named <code>BinaryTree</code> which will be something like this):</p> <pre><code>class Node { Node left, right; Person value; public Node(Person val) { value = val; } } public class BinaryTree { Node head; public BinaryTree() { head = null; } public void insert(Person data) { Node tmp = new Node(data); if (head == null) head = tmp; else // do comparisons with head to decide where to add the new node head-&gt;left = new Node(data); // or head-&gt;right = new Node(data); } public void traverse() { // could be similar to your printInOrder method } } </code></pre> <p>and then you could do:</p> <pre><code>... main(String[] args) { BinaryTree tree = new BinaryTree(); tree.insert(new Person(...)); tree.insert(new Person(...)); tree.traverse(); } </code></pre> <h3> Make your code work with not many changes </h3> <p>This is more on how to "work" in your already created code.</p> <p>If you want to insert <code>Person</code> instances inside a binary tree, first of all you should change:</p> <pre><code> // Node Class static class Node { Node left, right; int value; public Node(int value) { this.value = value; } } </code></pre> <p>instead of taking <code>int</code> values, to take <code>Person</code> instances, like this:</p> <pre><code> // Node Class static class Node { Node left, right; Person per; public Node(Person per) { this.per = per; } } </code></pre> <p>You should also change <code>insert</code> method to take <code>Person</code> instances, instead of <code>int</code> values. And change the comparisons <code>(value &lt; node.value)</code> to compare <code>Person</code> instances, maybe by their <code>lastName</code> or their <code>id</code> instead.</p> <p>Then inside your <code>clubmember</code> <code>main</code> code you could do:</p> <pre><code>public static void main(String[] args) { BinaryTreeTest foo; Person per1 = new Person(...); BinaryTreeTest.Node nod1 = new BinaryTreeTest.Node(per1); Person per2 = new Person(...); foo.insert(nod1, per2); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload