COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Binary Search Trees BinarySearchTree Program
TreeNode.java
BinarySearchTree.java
BinarySearchTreeProjectClient.java




Binary Search Tree Program


Description

This program builds a generic binary search tree to store Comparable objects.

Associated Concepts:

Binary Search Tree

Algorithm:

TreeNode class

Four private data members:

A generic Comparable object.

Three self-referential references: parent, leftChild, and rightChild.

Constructor.

Getters and setters.

insert() method to insert a value into the tree.

BinarySearchTree class

One private data member: the root of tree.

Constructor.

Getter and setter.

insertNode() method.

Call root.insert() method.

inorderTraverse() method.

preorderTraverse() method.

postorderTraverse() method.

search() method to search a value in the tree.

Return true if found; false otherwise.

Client program

Create a BinarySearchTree object to store integers in the tree nodes.

Insert a number integers into the tree.

Traverse the tree with three different algorithms: inorder, preorder, and postorder.

Search a particular value in the tree.

If found, print its parent, left child, and right child accordingly.

Source Code