You are on page 1of 6

Study notes on Binary search tree - Study notes on Binary search tree EXIT

Day 25 | Binary Search Tree | Binary Search Tree

A binary tree T, is called binary search tree (or binary sorted tree), if each node N of T has the following
property. The value at N is greater than every value in the left subtree of N and is less than or equal to
every value in the right subtree of N. A BST holds the following properties:

Each node can have up to two child nodes.


The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
The left and right subtree each must also be a binary search tree.
A unique path exists from the root to every other node.

Traversals of Binary Search Tree

Inorder Tree Walk: A binary search tree is an AVL tree if and only if
each node in the tree satis es the following property: Rotations: A tree rotation is required when we have
inserted or deleted a node which leaves the tree in an unbalanced form.
During this type of walk, we visit the root of a subtree between the left subtree visit and right subtree
visit.
Inorder (x):

If x ≠ NIL {

Inorder (left[x]);

print key[x];

Inorder (right[x]);

Preorder Tree Walk:


In which we visit the root node before the nodes in either subtree.
Preorder (x) :

If x ≠ NIL {

print key[x];

Preorder (left[x]);
NEXT
Preorder (right[x]);

Postorder Tree Walk:


In which we visit the root node after the nodes in its subtrees.
Postorder(x):

If x ≠NIL {

Postorder (left[x]);

Postorder (right[x]);

print key [x];

Search an element in BST:


The most basic operator is search, which can be a recursive or an iterative function. A search can start
from any node, If the node is NULL (i.e. the tree is empty), then return NULL which means the key does
not exist in the tree. Otherwise, if the key equals that of the node, the search is successful and we return
the node. If the key is less than that of the node, we search its left subtree. Similarly, if the key is greater
than that of the node, we search its right subtree. This process is repeated until the key is found or the
remaining subtree is null. To search the key in the BFS, just call the method from the root node.
Insertion of an element:
Insertion begins as a search would begin; We examine the root and recursively insert the new node to the
left subtree if its key is less than that of the root, or the right subtree if its key is greater than the root. If
the key exists, we can either replace the value by the new value or just return without doing anything.
Deletion of an element:
The deletion is a little complex. Basically, to delete a node by a given key, we need to nd the node with
the key, and remove it from the tree. There are three possible cases to consider:

Deleting a leaf: we can simply remove it from the tree.


Deleting a node with one child: remove the node and replace it with its child.
Deleting a node with two children: nd its in-order successor (left-most node in its right sub-tree),
let's say R. Then copy R's key and value to the node, and remove R from its right sub-tree.

Key Points of BST

It takes θ (n) time to walk (inorder, preorder and pastorder) a tree of n nodes.
On a binary search tree of height h, Search, Minimum, Maximum, Successor, Predecessor, Insert,
and Delete can be made to run in O(h) time.
The height of the Binary Search Tree equals the number of links from the root node to the deepest
node.

The disadvantage of a BST is that if every item which is inserted to be next is greater than the previous
item, then we will get a right skewed BST or if every item which is to be inserted is less than to the
previous item, then we will get a left skewed BST.
NEXT
So, to overcome the skewness problem in BST, the concept of AVL- tree or height balanced tree came into
existence.
Balanced Binary Trees: Balancing ensures that the internal path lengths are close to the optimal n log n.
A balanced tree will have the lowest possible overall height. AVL trees and B trees are balanced binary
trees.
 Example 1: Number of leaves in a binary search tree

int numberofleaves(struct bstnode * node)

int total = 0;

if(node->Left == 0 && node->Right == 0)

return 1;

if(node->Left!= 0)

total += numberofleaves(node->Left);

if(node->Right!=0)

total += numberofleaves(node->Right);

return total;

Example  2: Find the Diameter of a binary tree


(Diameter of a tree is the longest path between two leaf nodes in a tree.)
NEXT
int diameter(struct btnode *root, int *height)

int leftH = 0, rightH = 0;

int leftD = 0, rightD = 0;

if(root == NULL)

*height = 0;

return 0;

leftD = diameter(root->left, &leftH);

rightD = diameter(root->right, &rightH);

*height = max(leftH, rightH) + 1;

return max(leftH + rightH + 1, max(leftD, rightD));

Example 3: Search an element in the Binary tree using iteration NEXT


struct bstnode *search(int value, struct bstnode *root){

while(root!=NULL && value!=root->value)

if(value < root->value) root = root->left;

else root = root->right;

return(root);

Example 4: Search an item (element) in the Binary tree using Recursive function

struct btnode *search(int item, struct btnode *root){

if(root==NULL || item == root->value) return(root);

if(item < root->info) return recursive_search(item, root->left);

else return recursive_search(item, root->right);

Example 5: Check if given binary tree is Binary Search Tree

bool isbst(struct btnode* root,int min,int max)

{
NEXT
if(root==NULL) return true;

if(n->data<=min || n->data > max) return false;

if(!isbst(root->left,min,n->data) || !isbst(root->right,n->data,max)) return false;

return true;

Example 6: Write a recursive function to count the number of nodes in binary tree

int count(stuct btnode* root) {

if(root == NULL) return 0;

else return count(root->left) + count(root->right) + 1;

NEXT

You might also like