You are on page 1of 31

/* Java implementation to convert

infix expression to postfix*/


// Note that here we use ArrayDeque class for Stack
// operations

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

class Test {

// A utility function to return


// precedence of a given operator
// Higher returned value means
// higher precedence
static int Prec(char ch)
{
switch (ch) {
case '+':
case '-':
return 1;

case '*':
case '/':
return 2;

case '^':
return 3;
}
return -1;
}

// The main method that converts


// given infix expression
// to postfix expression.
static String infixToPostfix(String exp)
{
// initializing empty String for result
String result = new String("");

// initializing empty stack


Deque<Character> stack
= new ArrayDeque<Character>();

for (int i = 0; i < exp.length(); ++i) {


char c = exp.charAt(i);

// If the scanned character is an


// operand, add it to output.
if (Character.isLetterOrDigit(c))
result += c;

// If the scanned character is an '(',


// push it to the stack.
else if (c == '(')
stack.push(c);

// If the scanned character is an ')',


// pop and output from the stack
// until an '(' is encountered.
else if (c == ')') {
while (!stack.isEmpty()
&& stack.peek() != '(') {
result += stack.peek();
stack.pop();
}

stack.pop();
}
else // an operator is encountered
{
while (!stack.isEmpty()
&& Prec(c) <= Prec(stack.peek())) {

result += stack.peek();
stack.pop();
}
stack.push(c);
}
}

// pop all the operators from the stack


while (!stack.isEmpty()) {
if (stack.peek() == '(')
return "Invalid Expression";
result += stack.peek();
stack.pop();
}

return result;
}

// Driver's code
public static void main(String[] args)
{
String exp = "a+b*(c^d-e)^(f+g*h)-i";

// Function call
System.out.println(infixToPostfix(exp));
}
}
/////////////////////

// Java program to demonstrate a Queue

import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {

public static void main(String[] args)


{
Queue<Integer> q
= new LinkedList<>();

// Adds elements {0, 1, 2, 3, 4} to


// the queue
for (int i = 0; i < 5; i++)
q.add(i);

// Display contents of the queue.


System.out.println("Elements of queue "
+ q);

// To remove the head of queue.


int removedele = q.remove();
System.out.println("removed element-"
+ removedele);

System.out.println(q);

// To view the head of queue


int head = q.peek();
System.out.println("head of queue-"
+ head);

// Rest all methods of collection


// interface like size and contains
// can be used with this
// implementation.
int size = q.size();
System.out.println("Size of queue-"
+ size);
}
}

///////////////

public class SinglyLinkedList {


//Represent a node of the singly linked list
class Node{
int data;
Node next;

public Node(int data) {


this.data = data;
this.next = null;
}
}

//Represent the head and tail of the singly linked list


public Node head = null;
public Node tail = null;

//addNode() will add a new node to the list


public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);

//Checks if the list is empty


if(head == null) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point
to newNode
tail.next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}

//display() will display all the nodes present in the list


public void display() {
//Node current will point to head
Node current = head;

if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of singly linked list: ");
while(current != null) {
//Prints each node by incrementing pointer
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}

public static void main(String[] args) {

SinglyLinkedList sList = new SinglyLinkedList();

//Add nodes to the list


sList.addNode(1);
sList.addNode(2);
sList.addNode(3);
sList.addNode(4);

//Displays the nodes present in the list


sList.display();
}
}

///////////////////////////

import java.io.*;

// Java program to implement


// a Singly Linked List
public class LinkedList {

Node head; // head of list

// Linked list Node.


// Node is a static nested class
// so main() can access it
static class Node {

int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}

// **************INSERTION**************

// Method to insert a new node


public static LinkedList insert(LinkedList list,
int data)
{
// Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;

// If the Linked List is empty,


// then make the new node as head
if (list.head == null) {
list.head = new_node;
}
else {
// Else traverse till the last node
// and insert the new_node there
Node last = list.head;
while (last.next != null) {
last = last.next;
}

// Insert the new_node at last node


last.next = new_node;
}

// Return the list by head


return list;
}

// **************TRAVERSAL**************

// Method to print the LinkedList.


public static void printList(LinkedList list)
{
Node currNode = list.head;

System.out.print("\nLinkedList: ");

// Traverse through the LinkedList


while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data + " ");

// Go to next node
currNode = currNode.next;
}
System.out.println("\n");
}
// **************DELETION BY KEY**************

// Method to delete a node in the LinkedList by KEY


public static LinkedList deleteByKey(LinkedList list,
int key)
{
// Store head node
Node currNode = list.head, prev = null;

//
// CASE 1:
// If head node itself holds the key to be deleted

if (currNode != null && currNode.data == key) {


list.head = currNode.next; // Changed head

// Display the message


System.out.println(key + " found and deleted");

// Return the updated List


return list;
}

//
// CASE 2:
// If the key is somewhere other than at head
//

// Search for the key to be deleted,


// keep track of the previous node
// as it is needed to change currNode.next
while (currNode != null && currNode.data != key) {
// If currNode does not hold key
// continue to next node
prev = currNode;
currNode = currNode.next;
}

// If the key was present, it should be at currNode


// Therefore the currNode shall not be null
if (currNode != null) {
// Since the key is at currNode
// Unlink currNode from linked list
prev.next = currNode.next;

// Display the message


System.out.println(key + " found and deleted");
}

//
// CASE 3: The key is not present
//

// If key was not present in linked list


// currNode should be null
if (currNode == null) {
// Display the message
System.out.println(key + " not found");
}

// return the List


return list;
}

// **************DELETION AT A POSITION**************

// Method to delete a node in the LinkedList by POSITION


public static LinkedList
deleteAtPosition(LinkedList list, int index)
{
// Store head node
Node currNode = list.head, prev = null;

//
// CASE 1:
// If index is 0, then head node itself is to be
// deleted

if (index == 0 && currNode != null) {


list.head = currNode.next; // Changed head

// Display the message


System.out.println(
index + " position element deleted");

// Return the updated List


return list;
}

//
// CASE 2:
// If the index is greater than 0 but less than the
// size of LinkedList
//
// The counter
int counter = 0;

// Count for the index to be deleted,


// keep track of the previous node
// as it is needed to change currNode.next
while (currNode != null) {

if (counter == index) {
// Since the currNode is the required
// position Unlink currNode from linked list
prev.next = currNode.next;

// Display the message


System.out.println(
index + " position element deleted");
break;
}
else {
// If current position is not the index
// continue to next node
prev = currNode;
currNode = currNode.next;
counter++;
}
}

// If the position element was found, it should be


// at currNode Therefore the currNode shall not be
// null
//
// CASE 3: The index is greater than the size of the
// LinkedList
//
// In this case, the currNode should be null
if (currNode == null) {
// Display the message
System.out.println(
index + " position element not found");
}

// return the List


return list;
}

// **************MAIN METHOD**************

// method to create a Singly linked list with n nodes


public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();

//
// ******INSERTION******
//

// Insert the values


list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);

// Print the LinkedList


printList(list);

//
// ******DELETION BY KEY******
//

// Delete node with value 1


// In this case the key is ***at head***
deleteByKey(list, 1);

// Print the LinkedList


printList(list);

// Delete node with value 4


// In this case the key is present ***in the
// middle***
deleteByKey(list, 4);

// Print the LinkedList


printList(list);

// Delete node with value 10


// In this case the key is ***not present***
deleteByKey(list, 10);

// Print the LinkedList


printList(list);

//
// ******DELETION AT POSITION******
//

// Delete node at position 0


// In this case the key is ***at head***
deleteAtPosition(list, 0);

// Print the LinkedList


printList(list);

// Delete node at position 2


// In this case the key is present ***in the
// middle***
deleteAtPosition(list, 2);

// Print the LinkedList


printList(list);

// Delete node at position 10


// In this case the key is ***not present***
deleteAtPosition(list, 10);

// Print the LinkedList


printList(list);
}
}

//////////////////////

public class SearchList {

//Represent a node of the doubly linked list


class Node{
int data;
Node previous;
Node next;

public Node(int data) {


this.data = data;
}
}

//Represent the head and tail of the doubly linked list


Node head, tail = null;

//addNode() will add a node to the list


public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);

//If list is empty


if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next will point to null, as it is the last node of the
list
tail.next = null;
}
else {
//newNode will be added after tail such that tail's next will point
to newNode
tail.next = newNode;
//newNode's previous will point to tail
newNode.previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to null
tail.next = null;
}
}

//searchNode() will search a given node in the list


public void searchNode(int value) {
int i = 1;
boolean flag = false;
//Node current will point to head
Node current = head;

//Checks whether the list is empty


if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
//Compare value to be searched with each node in the list
if(current.data == value) {
flag = true;
break;
}
current = current.next;
i++;
}
if(flag)
System.out.println("Node is present in the list at the position :
" + i);
else
System.out.println("Node is not present in the list");
}

public static void main(String[] args) {

SearchList dList = new SearchList();


//Add nodes to the list
dList.addNode(1);
dList.addNode(5);
dList.addNode(4);
dList.addNode(2);
dList.addNode(3);

//Search for node 4 in the list


dList.searchNode(4);
//Search for node 9 in the list
dList.searchNode(9);
}
}

////////////////////////

public class InsertMid {

//Represent a node of the doubly linked list

class Node{
int data;
Node previous;
Node next;

public Node(int data) {


this.data = data;
}
}

public int size = 0;


//Represent the head and tail of the doubly linked list
Node head, tail = null;

//addNode() will add a node to the list


public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);

//If list is empty


if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next will point to null, as it is the last node of the
list
tail.next = null;
}
else {
//newNode will be added after tail such that tail's next will point
to newNode
tail.next = newNode;
//newNode's previous will point to tail
newNode.previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to null
tail.next = null;
}
//Size will count the number of nodes present in the list
size++;
}

//addInMid() will add a node to the middle of the list


public void addInMid(int data) {
//Create a new node
Node newNode = new Node(data);

//If list is empty


if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next point to null, as it is the last node of the list
tail.next = null;
}
else {
//current will point to head
Node current = head, temp = null;

//Store the mid position of the list


int mid = (size % 2 == 0) ? (size/2) : ((size+1)/2);

//Iterate through list till current points to mid position


for(int i = 1; i < mid; i++){
current = current.next;
}

//Node temp will point to node next to current


temp = current.next;
temp.previous = current;
//newNode will be added between current and temp
current.next = newNode;
newNode.previous = current;
newNode.next = temp;
temp.previous = newNode;
}
size++;
}

//display() will print out the nodes of the list


public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
//Prints each node by incrementing the pointer.

System.out.print(current.data + " ");


current = current.next;
}
System.out.println();
}

public static void main(String[] args) {

InsertMid dList = new InsertMid();


//Add nodes to the list
dList.addNode(1);
dList.addNode(2);

System.out.println("Original list: ");


dList.display();

//Adding node '3' in the middle


dList.addInMid(3);
System.out.println( "Updated List: ");
dList.display();

//Adding node '4' in the middle


dList.addInMid(4);
System.out.println("Updated List: ");
dList.display();

//Adding node '5' in the middle


dList.addInMid(5);
System.out.println("Updated List: ");
dList.display();
}
}
///////////////////////

public class InsertEnd {


//Represent a node of the doubly linked list
class Node{
int data;
Node previous;
Node next;
public Node(int data) {
this.data = data;
}
}
//Represent the head and tail of the doubly linked list
Node head, tail = null;
//addAtEnd() will add a node to the end of the list
public void addAtEnd(int data) {
//Create a new node
Node newNode = new Node(data);
//If list is empty
if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next will point to null, as it is the last node of the
list
tail.next = null;
}
//Add newNode as new tail of the list
else {
//newNode will be added after tail such that tail's next will point
to newNode
tail.next = newNode;
//newNode's previous will point to tail
newNode.previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to null
tail.next = null;
}
}
//display() will print out the nodes of the list
public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Adding a node to the end of the list: ");
while(current != null) {
//Prints each node by incrementing the pointer.

System.out.print(current.data + " ");


current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
InsertEnd dList = new InsertEnd();
//Adding 1 to the list
dList.addAtEnd(1);
dList.display();
//Adding 2 to the list
dList.addAtEnd(2);
dList.display();
//Adding 3 to the list
dList.addAtEnd(3);
dList.display();
//Adding 4 to the list
dList.addAtEnd(4);
dList.display();
//Adding 5 to the list
dList.addAtEnd(5);
dList.display();
}
}

///////////////////////
public class DeleteEnd {

//Represent a node of the doubly linked list

class Node{
int data;
Node previous;
Node next;

public Node(int data) {


this.data = data;
}
}

//Represent the head and tail of the doubly linked list


Node head, tail = null;

//addNode() will add a node to the list


public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);

//If list is empty


if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next will point to null, as it is the last node of the
list
tail.next = null;
}
else {
//newNode will be added after tail such that tail's next will point
to newNode
tail.next = newNode;
//newNode's previous will point to tail
newNode.previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to null
tail.next = null;
}
}

//deleteFromEnd() will delete a node from the end of the list


public void deleteFromEnd() {
//Checks whether list is empty
if(head == null) {
return;
}
else {
//Checks whether the list contains only one node
if(head != tail) {
//Previous node to the tail will become new tail
tail = tail.previous;
//Node next to current tail will be made null
tail.next = null;
}
//If the list contains only one element
//Then it will remove node and now both head and tail will point to
null
else {
head = tail = null;
}
}
}

//display() will print out the nodes of the list


public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
//Prints each node by incrementing the pointer.

System.out.print(current.data + " ");


current = current.next;
}
System.out.println();
}

public static void main(String[] args) {

DeleteEnd dList = new DeleteEnd();


//Add nodes to the list
dList.addNode(1);
dList.addNode(2);
dList.addNode(3);
dList.addNode(4);
dList.addNode(5);

//Printing original list


System.out.println("Original List: ");
dList.display();
while(dList.head != null) {
dList.deleteFromEnd();
//Printing updated list
System.out.println("Updated List: ");
dList.display();
}
}
}
///////////////////////////

public class DeleteStart


{

//Represent a node of the doubly linked list

class Node{
int data;
Node previous;
Node next;

public Node(int data) {


this.data = data;
}
}

//Represent the head and tail of the doubly linked list


Node head, tail = null;

//addNode() will add a node to the list


public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);

//If list is empty


if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next will point to null, as it is the last node of the
list
tail.next = null;
}
else {
//newNode will be added after tail such that tail's next will point
to newNode
tail.next = newNode;
//newNode's previous will point to tail
newNode.previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to null
tail.next = null;
}
}

//deleteFromStart() will delete a node from the beginning of the list


public void deleteFromStart() {
//Checks whether list is empty
if(head == null) {
return;
}
else {
//Checks whether the list contains only one element
if(head != tail) {
//head will point to next node in the list
head = head.next;
//Previous node to current head will be made null
head.previous = null;
}
//If the list contains only one element
//then, it will remove node and now both head and tail will point
to null
else {
head = tail = null;
}
}
}

//display() will print out the nodes of the list


public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
//Prints each node by incrementing the pointer.

System.out.print(current.data + " ");


current = current.next;
}
System.out.println();
}

public static void main(String[] args) {

DeleteStart dList = new DeleteStart();


//Add nodes to the list
dList.addNode(1);
dList.addNode(2);
dList.addNode(3);
dList.addNode(4);
dList.addNode(5);

//Printing original list


System.out.println("Original List: ");
dList.display();
while(dList.head != null) {
dList.deleteFromStart();
//Printing updated list
System.out.println("Updated List: ");
dList.display();
}
}
}

//////////////////////////

public class InsertStart {

//Represent a node of the doubly linked list

class Node{
int data;
Node previous;
Node next;

public Node(int data) {


this.data = data;
}
}

//Represent the head and tail of the doubly linked list


Node head, tail = null;

//addAtStart() will add a node to the starting of the list


public void addAtStart(int data) {
//Create a new node
Node newNode = new Node(data);

//If list is empty


if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next will point to null, as it is the last node of the
list
tail.next = null;
}
//Add newNode as new head of the list
else {
//head's previous node will be newNode
head.previous = newNode;
//newNode's next node will be head
newNode.next = head;
//newNode's previous will point to null
newNode.previous = null;
//newNode will become new head
head = newNode;
}
}

//display() will print out the nodes of the list


public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Adding a node to the start of the list: ");
while(current != null) {
//Prints each node by incrementing the pointer.

System.out.print(current.data + " ");


current = current.next;
}
System.out.println();
}

public static void main(String[] args) {

InsertStart dList = new InsertStart();

//Adding 1 to the list


dList.addAtStart(1);
dList.display();
//Adding 2 to the list
dList.addAtStart(2);
dList.display();
//Adding 3 to the list
dList.addAtStart(3);
dList.display();
//Adding 4 to the list
dList.addAtStart(4);
dList.display();
//Adding 5 to the list
dList.addAtStart(5);
dList.display();
}
}

/////////////////////

// Java program to insert element in binary tree


import java.util.LinkedList;
import java.util.Queue;
public class GFG {

/* A binary tree node has key, pointer to


left child and a pointer to right child */
static class Node {
int key;
Node left, right;

// constructor
Node(int key)
{
this.key = key;
left = null;
right = null;
}
}
static Node root;
static Node temp = root;

/* Inorder traversal of a binary tree*/


static void inorder(Node temp)
{
if (temp == null)
return;
inorder(temp.left);
System.out.print(temp.key + " ");
inorder(temp.right);
}

/*function to insert element in binary tree */


static void insert(Node temp, int key)
{

if (temp == null) {
root = new Node(key);
return;
}
Queue<Node> q = new LinkedList<Node>();
q.add(temp);

// Do level order traversal until we find


// an empty place.
while (!q.isEmpty()) {
temp = q.peek();
q.remove();

if (temp.left == null) {
temp.left = new Node(key);
break;
}
else
q.add(temp.left);

if (temp.right == null) {
temp.right = new Node(key);
break;
}
else
q.add(temp.right);
}
}

// Driver code
public static void main(String args[])
{
root = new Node(10);
root.left = new Node(11);
root.left.left = new Node(7);
root.right = new Node(9);
root.right.left = new Node(15);
root.right.right = new Node(8);

System.out.print(
"Inorder traversal before insertion:");
inorder(root);

int key = 12;


insert(root, key);

System.out.print(
"\nInorder traversal after insertion:");
inorder(root);
}
}
// This code is contributed by Sumit Ghosh

////////////////////////////////

import java.io.*;
import java.util.*;

class GFG {
public static void main(String[] args)
{
BST tree = new BST();
tree.insert(30);
tree.insert(50);
tree.insert(15);
tree.insert(20);
tree.insert(10);
tree.insert(40);
tree.insert(60);
tree.inorder();
}
}
class Node {
Node left;
int val;
Node right;
Node(int val) { this.val = val; }
}

class BST {
Node root;

public void insert(int key)


{
Node node = new Node(key);
if (root == null) {
root = node;
return;
}
Node prev = null;
Node temp = root;
while (temp != null) {
if (temp.val > key) {
prev = temp;
temp = temp.left;
}
else if (temp.val < key) {
prev = temp;
temp = temp.right;
}
}
if (prev.val > key)
prev.left = node;
else
prev.right = node;
}

public void inorder()


{
Node temp = root;
Stack<Node> stack = new Stack<>();
while (temp != null || !stack.isEmpty()) {
if (temp != null) {
stack.add(temp);
temp = temp.left;
}
else {
temp = stack.pop();
System.out.print(temp.val + " ");
temp = temp.right;
}
}
}
}

////////////////////
// A utility function to search a given key in BST
public Node search(Node root, int key)
{
// Base Cases: root is null or key is present at root
if (root==null || root.key==key)
return root;

// Key is greater than root's key


if (root.key < key)
return search(root.right, key);

// Key is smaller than root's key


return search(root.left, key);
}

///////////////////////////

public class SearchBinaryTree {

//Represent a node of binary tree


public static class Node{
int data;
Node left;
Node right;

public Node(int data){


//Assign data to the new node, set left and right children to null
this.data = data;
this.left = null;
this.right = null;
}
}

//Represent the root of binary tree


public Node root;
public static boolean flag = false;

public SearchBinaryTree(){
root = null;
}

//searchNode() will search for the particular node in the binary tree
public void searchNode(Node temp, int value){
//Check whether tree is empty
if(root == null){
System.out.println("Tree is empty");
}
else{
//If value is found in the given binary tree then, set the flag to true
if(temp.data == value){
flag = true;
return;
}
//Search in left subtree
if(flag == false && temp.left != null){
searchNode(temp.left, value);
}
//Search in right subtree
if(flag == false && temp.right != null){
searchNode(temp.right, value);
}
}
}

public static void main(String[] args) {

SearchBinaryTree bt = new SearchBinaryTree();


//Add nodes to the binary tree
bt.root = new Node(1);
bt.root.left = new Node(2);
bt.root.right = new Node(3);
bt.root.left.left = new Node(4);
bt.root.right.left = new Node(5);
bt.root.right.right = new Node(6);

//Search for node 5 in the binary tree


bt.searchNode(bt.root, 5);

if(flag)
System.out.println("Element is present in the binary tree");
else
System.out.println("Element is not present in the binary tree");
}
}
/////////////////

#include <iostream>
using namespace std;
struct Node {
int data;
Node *left;
Node *right;
};
Node* create(int item)
{
Node* node = new Node;
node->data = item;
node->left = node->right = NULL;
return node;
}
/*Inorder traversal of the tree formed*/
void inorder(Node *root)
{
if (root == NULL)
return;
inorder(root->left); //traverse left subtree
cout<< root->data << " "; //traverse root node
inorder(root->right); //traverse right subtree
}
Node* findMinimum(Node* cur) /*To find the inorder successor*/
{
while(cur->left != NULL) {
cur = cur->left;
}
return cur;
}
Node* insertion(Node* root, int item) /*Insert a node*/
{
if (root == NULL)
return create(item); /*return new node if tree is empty*/
if (item < root->data)
root->left = insertion(root->left, item);
else
root->right = insertion(root->right, item);
return root;
}
void search(Node* &cur, int item, Node* &parent)
{
while (cur != NULL && cur->data != item)
{
parent = cur;
if (item < cur->data)
cur = cur->left;
else
cur = cur->right;
}
}
void deletion(Node*& root, int item) /*function to delete a node*/
{
Node* parent = NULL;
Node* cur = root;
search(cur, item, parent); /*find the node to be deleted*/
if (cur == NULL)
return;
if (cur->left == NULL && cur->right == NULL) /*When node has no children*/

{
if (cur != root)
{
if (parent->left == cur)
parent->left = NULL;
else
parent->right = NULL;
}
else
root = NULL;
free(cur);
}
else if (cur->left && cur->right)
{
Node* succ = findMinimum(cur->right);
int val = succ->data;
deletion(root, succ->data);
cur->data = val;
}
else
{
Node* child = (cur->left)? cur->left: cur->right;
if (cur != root)
{
if (cur == parent->left)
parent->left = child;
else
parent->right = child;
}
else
root = child;
free(cur);
}
}
int main()
{
Node* root = NULL;
root = insertion(root, 45);
root = insertion(root, 30);
root = insertion(root, 50);
root = insertion(root, 25);
root = insertion(root, 35);
root = insertion(root, 45);
root = insertion(root, 60);
root = insertion(root, 4);
printf("The inorder traversal of the given binary tree is - \n");
inorder(root);
deletion(root, 25);
printf("\nAfter deleting node 25, the inorder traversal of the given binary
tree is - \n");
inorder(root);
insertion(root, 2);
printf("\nAfter inserting node 2, the inorder traversal of the given binary
tree is - \n");
inorder(root);
return 0;
}

///////////////

You might also like