Binary Search Tree is a kind of Binary Tree. Let’s start with Tree. Tree is a data structure model which looks like a reversed tree, the root placed on top of the tree and the leaf placed on the bottom of the tree. The model will be looks like pyramid shape. Binary Tree is a tree which every leaves / nodes just have maximum two child leaves, that’s why we call it binary (base 2). Binary Search Tree… this kind of binary tree that have the position of leaves “sorted”. All of elements on the left of a leaf must be smaller than the leaf and all of elements on the right of a leaf must be bigger than the leaf. What about same value? that’s up to you to place it where.

Let’s look this example from wikipedia, “F” is the root of the tree. “B” is a left child of “F”. “G” is a right child of “F”. “B” is a parent of “A”. “B” also a parent of “D”.

Creating Binary Search Tree can’t use the Node which is used for Linked List, Stack, and Queue. Honestly, the Node can be used but the context will be different because the pointer to other Node we will named child. I also give additional pointer, “parent” for pointing the parent of the Node. Let’s see the implementation on Java Programming Language.

public class BinaryTreeNode { public BinaryTreeNode parent; public BinaryTreeNode leftChild; public BinaryTreeNode rightChild; private String info; public BinaryTreeNode(String info){ this.parent = null; this.leftChild = null; this.rightChild = null; this.info = info; } public String getInfo(){ return this.info; } public void setInfo(String info){ this.info = info; } }

Then after we have the Nodes we can construct the Binary Search Tree. Which the most important method is inserting Nodes. If the tree root is null (the tree is new) we just make the node as root. But if the tree is not empty, we must track or find the right place of that node. The concepts is to find an empty place that meet the rules of binary search tree.

- If the value of the new node less than current node
- If the left child of current node is not empty, move current node to left child
- else, the left child of current node is new node

- If the value of the new node equal or more than current node
- If the right child of current node is not empty, move the current node to right child
- else, the right child of current node is new node

public class BinarySearchTree { private BinaryTreeNode root; public BinarySearchTree(){ this.root = null; } public void insertNode(BinaryTreeNode node){ if (this.root == null){ this.root = node; } else{ trackPosition(node, this.root); } } private void trackPosition(BinaryTreeNode node, BinaryTreeNode start){ String sInfo = start.getInfo(); if (sInfo.compareTo(node.getInfo()) > 0){ if (start.leftChild == null){ start.leftChild = node; node.parent = start; } else{ trackPosition(node, start.leftChild); } } else{ if (start.rightChild == null){ start.rightChild = node; node.parent = start; } else{ trackPosition(node, start.rightChild); } } } }

how do i create the main method of this tree????

Pingback: Binary Search Tree | Object Oriented Programming

it’s look nice, i’ll try it and report to you.

thx 😀