Data Structures | Binary Search Tree
In this article we will talk about Binary Search Tree.
What is the binary search tree?
Node based data structure with following properties:
- The left subtree of node contains values that are lesser than node’s value
- The right subtree of node contains values that are bigger than nodes’ value
- Left and right subtrees are also binary search trees.
Which operations we can peform on binary search trees?
- Insert value
- Get minimum value
- Get maximum value
- Search value
- Get height of tree
- Delete value
Are they contains duplicates?
No
What about asymptotic complexity of operations?
Depends on “balance” property. In balanced trees - logarithmic complexity, in unbalanced trees - linear complexity.
What about space complexity?
Linear complexity
What about insert operation?
1 | public class BinarySearchTree { |
If tree is empty then define root with value val, otherwise insert recursively into tree, until we find suitable place.insertRecursively
function search for suitable place, if there is some insert new node and return newely created node, otherwise search suitable place in children’s subtrees and return given current node.
What about get maximum, minimum value operation?
1 | public int getMaximum() throws Exception { |
As we know each node’s right children is bigger than current node, so the most biggest node is rightmost.
The same logic for minumum operation:
1 | public int getMinimum() throws Exception { |
What about search operation?
1 | public boolean search(int val) { |
What about height operation?
1 | public int getHeight() { |
What about delete operation?
1 | public void deleteValue(int val) { |
If node do not present in tree, so nothings happens.
If deleting node has one child then return that child(change refference of node’s parent child to node’s child)
If deleting node has two childrens then find the smallest value in right subtree, setup that value to current node’s value and recursively delete smallest node.