Skip to main content
ICT
Lesson AB30 - Binary Search Trees
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

B. Building a Binary Tree page 4 of 15

  1. The following definition of a TreeNode class will be used in the remainder of this 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 sorted binary tree in the indicated order.

    26 79 14 99 53 9 35 21 87

    Draw the resulting binary tree.

  3. You will notice that as each node was added to the tree, it was inserted as a leaf. The insert algorithm will be recursive.

    See Transparency AB30.1, Building a Binary Tree

  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.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.