Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The idea behind a binary tree is that it is sorted. Any values larger than the current value are in the right node and every value smaller is in the left node. That means you shouldn't do the tree-building in your main program. Rather, every node should have an "insert" method which determines whether the new node should go to the left or the right of the current node. When you have a new node, you create that node, and then you call <code>root.insert(newNode)</code>.</p> <p>The insert-method would then work like this (because this is obviously a student assignment and you are supposed to learn from it, you only get pseudo-code from me, no complete solution):</p> <pre><code>When value is smaller than own value: When there already is a left-node: call left-node.insert(new-node) When there isn't a left-node yet: the left-node is now the new-node When value is larger than own value: When there already is a right-node: call right-node.insert(new-node) When there isn't a right-node yet: the right-node is now the new-node When value is equal to own value: Duplicate value. Either ignore the value or throw an exception. </code></pre> <p>Finding if an object already is in the tree works the same way:</p> <pre><code>When requested value is equal to the value of this node return this node When the requested value is smaller When a left node exists call left.find(value) When no left node exists value doesn't exist in this tree When the requested value is larger When a right node exists call right.find(value) When no right node exists value doesn't exist in this tree </code></pre> <p>In the case you don't actually want to learn how binary trees work and just use them, just use <a href="http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html" rel="noreferrer">TreeSet</a> which is an already working binary-tree implementation.</p>
    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. This table or related slice is empty.
    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