Skip to main content
Lesson 34 - Binary Trees
Lesson MenuPreviousNext
  
Building a Binary Tree page 4 of 7

  1. The following definitions will apply in this next section on building a binary tree.

    public class TreeNode
    {
      private Object value;
      private TreeNode left;
      private TreeNode right;
    
      public TreeNode(Object initValue, TreeNode initLeft,
            TreeNode initRight)
      {
      value = initValue;
      left = initLeft;
      right = initRight;
      }
     
      public Object getValue()
      {
      return value;
      }
    
      public TreeNode getLeft()
      {
      return left;
      }
    
      public TreeNode getRight()
      {
      return right;
      }
    
      public void setValue(Object theNewValue)
      {
      value = theNewValue;
      }
    
      public void setLeft(TreeNode theNewLeft)
      {
      left = theNewLeft;
      }
    
      public void setRight(TreeNode theNewRight)
      {
      right = theNewRight;
      }
    }
  2. Suppose the following integers were inserted into a binary tree in this order:

    26	 79   14   99	53	 9	 35   21   87

    Draw the resulting binary tree:


    Sorry, but your browser
    doesn't support Java.

    Animated Binary Tree Applet*


  3. You will notice that new information was placed in the binary tree as a leaf. The insert algorithm will be a recursive solution.

  4. Given this parameter list for the insert method, develop the pseudocode below it.

    void insert (TreeNode node, Object data)
    // Will insert data into an ordered binary tree. 
    // The solution is recursive.
    
    
    
    
    
    
    

*Based on Animated Data Structure Binary Tree Applet created by John Kloss <jkloss@hops.cs.jhu.edu>


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.