Skip to main content
Logo image

Problem Solving with Algorithms and Data Structures using Kotlin The Interactive Edition

Section 7.4 Implementation

Keeping in mind the definitions from the previous section, we will show how to implement a BinaryTree class with the following methods.
  • getRootValue() returns the object stored in the current node.
  • setRootValue(value) stores the object in parameter value 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. We will do so with an approach based on nodes and references.
You have attempted of activities on this page.