Professional Documents
Culture Documents
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;
class Test {
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
stack.pop();
}
else // an operator is encountered
{
while (!stack.isEmpty()
&& Prec(c) <= Prec(stack.peek())) {
result += stack.peek();
stack.pop();
}
stack.push(c);
}
}
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));
}
}
/////////////////////
import java.util.LinkedList;
import java.util.Queue;
System.out.println(q);
///////////////
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();
}
///////////////////////////
import java.io.*;
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}
// **************INSERTION**************
// **************TRAVERSAL**************
System.out.print("\nLinkedList: ");
// Go to next node
currNode = currNode.next;
}
System.out.println("\n");
}
// **************DELETION BY KEY**************
//
// CASE 1:
// If head node itself holds the key to be deleted
//
// CASE 2:
// If the key is somewhere other than at head
//
//
// CASE 3: The key is not present
//
// **************DELETION AT A POSITION**************
//
// CASE 1:
// If index is 0, then head node itself is to be
// deleted
//
// CASE 2:
// If the index is greater than 0 but less than the
// size of LinkedList
//
// The counter
int counter = 0;
if (counter == index) {
// Since the currNode is the required
// position Unlink currNode from linked list
prev.next = currNode.next;
// **************MAIN METHOD**************
//
// ******INSERTION******
//
//
// ******DELETION BY KEY******
//
//
// ******DELETION AT POSITION******
//
//////////////////////
////////////////////////
class Node{
int data;
Node previous;
Node next;
///////////////////////
public class DeleteEnd {
class Node{
int data;
Node previous;
Node next;
class Node{
int data;
Node previous;
Node next;
//////////////////////////
class Node{
int data;
Node previous;
Node next;
/////////////////////
// constructor
Node(int key)
{
this.key = key;
left = null;
right = null;
}
}
static Node root;
static Node temp = root;
if (temp == null) {
root = new Node(key);
return;
}
Queue<Node> q = new LinkedList<Node>();
q.add(temp);
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);
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;
////////////////////
// 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;
///////////////////////////
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);
}
}
}
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;
}
///////////////