|
|
||
| You are here: | ||
|
The abstract data types (ADT's) we have seen so far (lists, stacks and queues) have been linear , that is each item is preceded or followed by exactly one item (unless, of course, it is the first or last data item).
Object is the super class of all classes created in Java. You don't have any choice over that. We have a left link and a right link. If you understood the way linear Nodes worked in a simple linked list this should be a breeze.
.
Objects to enter data Buttons for program options display area
The usual sort of thing
if something was typed in, add it to the tree. This is a simplistic approach since we have left some nodes in memory by doing this. We pass on the Button ID to another method.
Tree traversal is a topic we cover on the next page .
Visit left subtree
Visit left subtree
output data
Hint : s1.compareTo (s2) evaluates to a 0 if both Strings are equal.
|
on this page: [ logical structure | Binary Tree Node | Binary Tree example | Exercises ] Trees are non-linear hierarchical data structures where every component may have super-components above and sub-components below. A tree can also be described as a finite set of one or more linked nodes such that there is a special node with no parent called the root . Each subtree of the node is a child . Nodes at the same level are siblings . In either case a tree is probably a familiar concept already:
Looking at the sub tree we can see that it has all of the features of the main tree (a root, nodes and leaves) so that a tree is a recursive structure. A binary tree is a collection of nodes, which has, at most, two children. It is relatively simple to implement such a structure using a node with a data field and two pointer fields:
As we did previously, we establish a class to represent the data structure and another to manipulate it: /** The class which demonstrates the binary tree is not particularly sophisticated. This class enables the user to add items to the binary tree such that items can be retrieved in alphabetical order. For example the root node might be Harry. If the next node added is greater than Harry it is added to the right branch (say it is Mary), otherwise to the left (Adam):
import java.applet.*; Button addButton = new Button("Add item"); /** addButton.addActionListener(this); root = null; // compare the data elements What happens if you try to enter the same data item more than once? Trace the insertNode algorithm to find out where that happens, prevent it and output a suitable error message . Modify the example to store a simple data object instead of a String.
Related: [ Java home | Previous: recursion | Next: tree traversal ] |
In this course, we deal only with Binary Trees - a special kind of tree in which each node can have, at most, two children usually referred to as left-child and right-child). |
|
|
|||
|
Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: May 31, 2009 |