# Tutorial on Binary Search Tree

Binary Search Tree is a node-based binary tree data structure which has the following properties:

• The left subtree of a node contains only nodes with keys lesser 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.
There must be no duplicate nodes.

Below are the various operations that can be performed on a BST:

1. Insert a node into a BST: A new key is always inserted at leaf. Start searching a key from root till a leaf node. Once a leaf node is found, the new node is added as a child of the leaf node.

 `// C program to insert a node ` `// in a BST ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function to do inorder traversal of BST ` `void` `inorder(``struct` `node* root) ` `{ ` `    ``if` `(root != NULL) { ` `        ``inorder(root->left); ` `        ``printf``(``"%d "``, root->key); ` `        ``inorder(root->right); ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// inserting value 50 ` `    ``root = insert(root, 50); ` ` `  `    ``// inserting value 30 ` `    ``insert(root, 30); ` ` `  `    ``// inserting value 20 ` `    ``insert(root, 20); ` ` `  `    ``// inserting value 40 ` `    ``insert(root, 40); ` ` `  `    ``// inserting value 70 ` `    ``insert(root, 70); ` ` `  `    ``// inserting value 60 ` `    ``insert(root, 60); ` ` `  `    ``// inserting value 80 ` `    ``insert(root, 80); ` ` `  `    ``// print the BST ` `    ``inorder(root); ` ` `  `    ``return` `0; ` `} `

Output:

```20 30 40 50 60 70 80
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

2. Inorder traversal: In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order.

 `// C program to implement ` `// inorder traversal of BST ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function to do inorder traversal of BST ` `void` `inorder(``struct` `node* root) ` `{ ` `    ``if` `(root != NULL) { ` `        ``inorder(root->left); ` `        ``printf``(``"%d "``, root->key); ` `        ``inorder(root->right); ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``inorder(root); ` ` `  `    ``return` `0; ` `} `

Output:

```20 30 40 50 60 70 80
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

3. Preorder traversal: Preorder traversal first visits the root node and then traverses the left and the right subtree. It is used to create a copy of the tree. Preorder traversal is also used to get prefix expression on of an expression tree.

 `// C program to implement ` `// preorder traversal ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function to do preorder traversal of BST ` `void` `preOrder(``struct` `node* root) ` `{ ` `    ``if` `(root != NULL) { ` `        ``printf``(``"%d "``, root->key); ` `        ``preOrder(root->left); ` `        ``preOrder(root->right); ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``preOrder(root); ` ` `  `    ``return` `0; ` `} `

Output:

```50 30 20 40 70 60 80
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

4. Postorder traversal: Postorder traversal first traverses the left and the right subtree and then visits the root node. It is used to delete the tree.

 `// C program to implement ` `// postorder traversal ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function to do postorder traversal of BST ` `void` `postOrder(``struct` `node* root) ` `{ ` `    ``if` `(root != NULL) { ` `        ``postOrder(root->left); ` `        ``postOrder(root->right); ` `        ``printf``(``"%d "``, root->key); ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``postOrder(root); ` ` `  `    ``return` `0; ` `} `

Output:

```20 40 30 60 80 70 50
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

5. Level order traversal: Level order traversal of a BST is breadth first traversal for the tree. It visits all nodes at a particular level first before moving to the next level.

 `// C program to implement ` `// level order traversal ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Returns height of the BST ` `int` `height(``struct` `node* node) ` `{ ` `    ``if` `(node == NULL) ` `        ``return` `0; ` `    ``else` `{ ` ` `  `        ``// Compute the depth of each subtree ` `        ``int` `lDepth = height(node->left); ` `        ``int` `rDepth = height(node->right); ` ` `  `        ``// Use the larger one ` `        ``if` `(lDepth > rDepth) ` `            ``return` `(lDepth + 1); ` `        ``else` `            ``return` `(rDepth + 1); ` `    ``} ` `} ` ` `  `// Print nodes at a given level ` `void` `printGivenLevel(``struct` `node* root, ` `                     ``int` `level) ` `{ ` `    ``if` `(root == NULL) ` `        ``return``; ` `    ``if` `(level == 1) ` `        ``printf``(``"%d "``, root->key); ` `    ``else` `if` `(level > 1) { ` ` `  `        ``// Recursive Call ` `        ``printGivenLevel(root->left, level - 1); ` `        ``printGivenLevel(root->right, level - 1); ` `    ``} ` `} ` ` `  `// Function to line by line print ` `// level order traversal a tree ` `void` `printLevelOrder(``struct` `node* root) ` `{ ` `    ``int` `h = height(root); ` `    ``int` `i; ` `    ``for` `(i = 1; i <= h; i++) { ` `        ``printGivenLevel(root, i); ` `        ``printf``(``"\n"``); ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``printLevelOrder(root); ` ` `  `    ``return` `0; ` `} `

Output:

```50
30 70
20 40 60 80
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

6. Print nodes at given Level : It prints all the nodes at a particular level of the BST.

 `// C program to print nodes ` `// at a given level ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Print nodes at a given level ` `void` `printGivenLevel(``struct` `node* root, ` `                     ``int` `level) ` `{ ` `    ``if` `(root == NULL) ` `        ``return``; ` `    ``if` `(level == 1) ` `        ``printf``(``"%d "``, root->key); ` `    ``else` `if` `(level > 1) { ` ` `  `        ``// Recursive Call ` `        ``printGivenLevel(root->left, level - 1); ` `        ``printGivenLevel(root->right, level - 1); ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``printGivenLevel(root, 2); ` ` `  `    ``return` `0; ` `} `

Output:

```30 70
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

7. Print all leaf nodes: A node is a leaf node if both left and right child nodes of it are NULL.

 `// C program to print all ` `// leaf nodes of a BST ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function to print leaf nodes ` `// from left to right ` `void` `printLeafNodes(``struct` `node* root) ` `{ ` `    ``// If node is null, return ` `    ``if` `(!root) ` `        ``return``; ` ` `  `    ``// If node is leaf node, ` `    ``// print its data ` `    ``if` `(!root->left && !root->right) { ` ` `  `        ``printf``(``"%d "``, root->key); ` `        ``return``; ` `    ``} ` ` `  `    ``// If left child exists, ` `    ``// check for leaf recursively ` `    ``if` `(root->left) ` `        ``printLeafNodes(root->left); ` ` `  `    ``// If right child exists, ` `    ``// check for leaf recursively ` `    ``if` `(root->right) ` `        ``printLeafNodes(root->right); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``printLeafNodes(root); ` ` `  `    ``return` `0; ` `} `

Output:

```20 40 60 80
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

8. Print all non leaf node: A node is a non leaf node if either of its left or right child nodes are not NULL.

 `// C program to print all ` `// non leaf nodes of a BST ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function to print all non-leaf ` `// nodes in a tree ` `void` `printNonLeafNode(``struct` `node* root) ` `{ ` `    ``// Base Cases ` `    ``if` `(root == NULL ` `        ``|| (root->left == NULL ` `            ``&& root->right == NULL)) ` `        ``return``; ` ` `  `    ``// If current node is non-leaf, ` `    ``if` `(root->left != NULL ` `        ``|| root->right != NULL) { ` `        ``printf``(``"%d "``, root->key); ` `    ``} ` ` `  `    ``// If root is Not NULL and its one ` `    ``// of its child is also not NULL ` `    ``printNonLeafNode(root->left); ` `    ``printNonLeafNode(root->right); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``printNonLeafNode(root); ` ` `  `    ``return` `0; ` `} `

Output:

```50 30 70
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

9. Right view of BST: Right view of a Binary Search Tree is set of nodes visible when tree is visited from Right side.

 `// C program to print ` `// right view of a BST ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function to print the right view ` `// of a binary tree. ` `void` `rightViewUtil(``struct` `node* root, ` `                   ``int` `level, ` `                   ``int``* max_level) ` `{ ` `    ``// Base Case ` `    ``if` `(root == NULL) ` `        ``return``; ` ` `  `    ``// If this is the last Node of its level ` `    ``if` `(*max_level < level) { ` `        ``printf``(``"%d\t"``, root->key); ` `        ``*max_level = level; ` `    ``} ` ` `  `    ``// Recur for right subtree first, ` `    ``// then left subtree ` `    ``rightViewUtil(root->right, level + 1, ` `                  ``max_level); ` ` `  `    ``rightViewUtil(root->left, level + 1, ` `                  ``max_level); ` `} ` ` `  `// Wrapper over rightViewUtil() ` `void` `rightView(``struct` `node* root) ` `{ ` `    ``int` `max_level = 0; ` `    ``rightViewUtil(root, 1, &max_level); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``rightView(root); ` ` `  `    ``return` `0; ` `} `

Output:

```50    70    80
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

10. Left view of BST: Left view of a Binary Search Tree is set of nodes visible when tree is visited from Left side.

 `// C program to print ` `// left view of a BST ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function to print left view of ` `// binary tree ` `void` `leftViewUtil(``struct` `node* root, ` `                  ``int` `level, ` `                  ``int``* max_level) ` `{ ` `    ``// Base Case ` `    ``if` `(root == NULL) ` `        ``return``; ` ` `  `    ``// If this is the first node ` `    ``// of its level ` `    ``if` `(*max_level < level) { ` `        ``printf``(``"%d\t"``, root->key); ` `        ``*max_level = level; ` `    ``} ` ` `  `    ``// Recur for left and right subtrees ` `    ``leftViewUtil(root->left, level + 1, ` `                 ``max_level); ` ` `  `    ``leftViewUtil(root->right, level + 1, ` `                 ``max_level); ` `} ` ` `  `// Wrapper over leftViewUtil() ` `void` `leftView(``struct` `node* root) ` `{ ` `    ``int` `max_level = 0; ` `    ``leftViewUtil(root, 1, &max_level); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``leftView(root); ` ` `  `    ``return` `0; ` `} `

Output:

```50    30    20
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

11. Height of BST: It is recursively calculated using height of left and right subtrees of the node and assigns height to the node as max of the heights of two children plus 1.

 `// C program to print ` `// height of a BST ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Returns height of the BST ` `int` `height(``struct` `node* node) ` `{ ` `    ``if` `(node == NULL) ` `        ``return` `0; ` `    ``else` `{ ` ` `  `        ``// Compute the depth of each subtree ` `        ``int` `lDepth = height(node->left); ` `        ``int` `rDepth = height(node->right); ` ` `  `        ``// Use the larger one ` `        ``if` `(lDepth > rDepth) ` `            ``return` `(lDepth + 1); ` `        ``else` `            ``return` `(rDepth + 1); ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``printf``(``"%d"``, height(root)); ` ` `  `    ``return` `0; ` `} `

Output:

```3
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

12. Delete a Node of BST: It is used to delete a node with specific key from the BST and return the new BST.

 `// C program to delete ` `// a node of BST ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function to do inorder traversal of BST ` `void` `inorder(``struct` `node* root) ` `{ ` `    ``if` `(root != NULL) { ` `        ``inorder(root->left); ` `        ``printf``(``"%d "``, root->key); ` `        ``inorder(root->right); ` `    ``} ` `} ` ` `  `// Function that returns the node with minimum ` `// key value found in that tree ` `struct` `node* minValueNode(``struct` `node* node) ` `{ ` `    ``struct` `node* current = node; ` ` `  `    ``// Loop down to find the leftmost leaf ` `    ``while` `(current && current->left != NULL) ` `        ``current = current->left; ` ` `  `    ``return` `current; ` `} ` ` `  `// Function that deletes the key and ` `// returns the new root ` `struct` `node* deleteNode(``struct` `node* root, ` `                        ``int` `key) ` `{ ` `    ``// base Case ` `    ``if` `(root == NULL) ` `        ``return` `root; ` ` `  `    ``// If the key to be deleted is ` `    ``// smaller than the root's key, ` `    ``// then it lies in left subtree ` `    ``if` `(key < root->key) { ` `        ``root->left ` `            ``= deleteNode(root->left, key); ` `    ``} ` ` `  `    ``// If the key to be deleted is ` `    ``// greater than the root's key, ` `    ``// then it lies in right subtree ` `    ``else` `if` `(key > root->key) { ` ` `  `        ``root->right ` `            ``= deleteNode(root->right, key); ` `    ``} ` ` `  `    ``// If key is same as root's key, ` `    ``// then this is the node ` `    ``// to be deleted ` `    ``else` `{ ` ` `  `        ``// Node with only one child ` `        ``// or no child ` `        ``if` `(root->left == NULL) { ` `            ``struct` `node* temp = root->right; ` `            ``free``(root); ` `            ``return` `temp; ` `        ``} ` `        ``else` `if` `(root->right == NULL) { ` `            ``struct` `node* temp = root->left; ` `            ``free``(root); ` `            ``return` `temp; ` `        ``} ` ` `  `        ``// Node with two children: ` `        ``// Get the inorder successor(smallest ` `        ``// in the right subtree) ` `        ``struct` `node* temp = minValueNode(root->right); ` ` `  `        ``// Copy the inorder successor's ` `        ``// content to this node ` `        ``root->key = temp->key; ` ` `  `        ``// Delete the inorder successor ` `        ``root->right ` `            ``= deleteNode(root->right, temp->key); ` `    ``} ` `    ``return` `root; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``root = deleteNode(root, 60); ` `    ``inorder(root); ` ` `  `    ``return` `0; ` `} `

Output:

```20 30 40 50 70 80
```

Time Complexity: O(log N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

13. Smallest Node of the BST: It is used to return the node with the smallest value in the BST.

 `// C program print smallest ` `// element of BST ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function that returns the node with minimum ` `// key value found in that tree ` `struct` `node* minValueNode(``struct` `node* node) ` `{ ` `    ``struct` `node* current = node; ` ` `  `    ``// Loop down to find the leftmost leaf ` `    ``while` `(current && current->left != NULL) ` `        ``current = current->left; ` ` `  `    ``return` `current; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``printf``(``"%d"``, minValueNode(root)->key); ` ` `  `    ``return` `0; ` `} `

Output:

```20
```

Time Complexity: O(log N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

14. Total number of nodes in a BST: The function returns the total count of nodes in the BST.

 `// C program to print total ` `// count of nodes in BST ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function to get the total count of ` `// nodes in a binary tree ` `int` `nodeCount(``struct` `node* node) ` `{ ` `    ``if` `(node == NULL) ` `        ``return` `0; ` ` `  `    ``else` `        ``return` `nodeCount(node->left) ` `               ``+ nodeCount(node->right) + 1; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``// Function Call ` `    ``printf``(``"%d"``, nodeCount(root)); ` ` `  `    ``return` `0; ` `} `

Output:

```7
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

15. Delete a BST: It is used to completely delete the BST and deallocate the memory.

 `// C program to delete a BST ` `#include ` `#include ` ` `  `// Given Node node ` `struct` `node { ` `    ``int` `key; ` `    ``struct` `node *left, *right; ` `}; ` ` `  `// Function to create a new BST node ` `struct` `node* newNode(``int` `item) ` `{ ` `    ``struct` `node* temp ` `        ``= (``struct` `node*)``malloc``( ` `            ``sizeof``(``struct` `node)); ` `    ``temp->key = item; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Function to insert a new node with ` `// given key in BST ` `struct` `node* insert(``struct` `node* node, ``int` `key) ` `{ ` `    ``// If the tree is empty, return a new node ` `    ``if` `(node == NULL) ` `        ``return` `newNode(key); ` ` `  `    ``// Otherwise, recur down the tree ` `    ``if` `(key < node->key) { ` `        ``node->left = insert(node->left, key); ` `    ``} ` `    ``else` `if` `(key > node->key) { ` `        ``node->right = insert(node->right, key); ` `    ``} ` ` `  `    ``// Return the node pointer ` `    ``return` `node; ` `} ` ` `  `// Function to do inorder traversal of BST ` `void` `inorder(``struct` `node* root) ` `{ ` `    ``if` `(root != NULL) { ` `        ``inorder(root->left); ` `        ``printf``(``"%d "``, root->key); ` `        ``inorder(root->right); ` `    ``} ` `} ` ` `  `// Function to delete the BST ` `struct` `node* emptyBST(``struct` `node* root) ` `{ ` `    ``struct` `node* temp; ` `    ``if` `(root != NULL) { ` ` `  `        ``// Traverse to left subtree ` `        ``emptyBST(root->left); ` ` `  `        ``// Traverse to right subtree ` `        ``emptyBST(root->right); ` ` `  `        ``printf``(``"Released node:%d \n"``, root->key); ` `        ``temp = root; ` ` `  `        ``// Require for free memory ` `        ``free``(temp); ` `    ``} ` `    ``return` `root; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/* Let us create following BST  ` `              ``50  ` `           ``/     \  ` `          ``30      70  ` `         ``/  \    /  \  ` `       ``20   40  60   80  ` `   ``*/` `    ``struct` `node* root = NULL; ` ` `  `    ``// Creating the BST ` `    ``root = insert(root, 50); ` `    ``insert(root, 30); ` `    ``insert(root, 20); ` `    ``insert(root, 40); ` `    ``insert(root, 70); ` `    ``insert(root, 60); ` `    ``insert(root, 80); ` ` `  `    ``printf``(``"BST before deleting:\n"``); ` `    ``inorder(root); ` ` `  `    ``// Function Call ` `    ``root = emptyBST(root); ` ` `  `    ``return` `0; ` `} `

Output:

```BST before deleting:
20 30 40 50 60 70 80
Released node:20
Released node:40
Released node:30
Released node:60
Released node:80
Released node:70
Released node:50
```

Time Complexity: O(N), where N is the number of nodes of the BST
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.