Skip to main content
Logo image

Problem Solving with Algorithms and Data Structures using Java: The Interactive Edition

Section 6.4 Implementation

Keeping in mind the definitions from the previous section, we can use the following functions to create and manipulate a binary tree:
  • BinaryTree() creates a new instance of a binary tree.
  • getRootValue() returns the object stored in the current node.
  • setRootValue(value) stores the object in parameter val in the current node.
  • getLeftChild() returns the binary tree corresponding to the left child of the current node.
  • getRightChild() returns the binary tree corresponding to the right child of the current node.
  • insertLeft(value) creates a new binary tree and installs it as the left child of the current node.
  • insertRight(value) creates a new binary tree and installs it as the right child of the current node.
The key decision in implementing a tree is choosing a good internal storage technique. Java allows us two very interesting possibilities, and we will examine both before choosing one. We call them array of arrays and nodes and references.
You have attempted of activities on this page.