Binary Search Tree
Binary Search Tree
Binary Search
Tree is one the data structures that features fast searching, sorting, easy
insertion and deletion. Its also a sorted version of the binary tree. The tree
works by appointing one node and the data following the node is separated by
their amount. If the data is bigger than the node then the data goes to the right
subtree and on the other hand, the smaller data goes to the left sub tree. The
system works recursively so the sub-subbranch of the BST also sorts the data,
which makes it easier to search.
Operations
In BST
there is 3 operations which is find(x), insert(x), remove(x).
Find = finding
the key (x) in the BST
Insert =
inserting a new key (x) into the BST
Remove =
removing key (x) from BST
· Find
Because
the BST sorts the data adequately, finding a key (x) is very easy. When we commence
a search, we will begin at the root and the program will compare the key to the
root. If the data is bigger than it will continue to the right subbranch after
that it will also compare the key to the data on the right subbranch if the key
is smaller than it will go to the left sub subbranch and so on.
· Insert
When
inserting data, the first data begin at the root if key is bigger than the root,
insert to the right subbranch, repeat it until an empty node to put the key. Remember
that the key will always be a new leaf.
· Remove
In
removing a node, there are three cases which needs to be considered. First, if the
key is a leaf, the node can be deleted directly. Second, else if the key is a
node with one child delete the node containing the key and connect the child
with the new parents. Third, if the key is in a node which has two children, find
the right most child of its left subbranch (z) and replace it the value (z) or
alternately you can choose the left most child of its right sub tree.
Comments
Post a Comment