mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-09 05:29:13 +08:00
style: format code
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
@@ -18,30 +17,27 @@ struct queue
|
||||
|
||||
queue q;
|
||||
|
||||
|
||||
void enqueue(node *n)
|
||||
{
|
||||
q.t[q.rear++]=n;
|
||||
q.t[q.rear++] = n;
|
||||
}
|
||||
|
||||
node * dequeue()
|
||||
node *dequeue()
|
||||
{
|
||||
return (q.t[q.front++]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Insert(node *n, int x)
|
||||
{
|
||||
if (x<n->val)
|
||||
if (x < n->val)
|
||||
{
|
||||
if (n->left==NULL)
|
||||
if (n->left == NULL)
|
||||
{
|
||||
node *temp=new node;
|
||||
temp->val=x;
|
||||
temp->left=NULL;
|
||||
temp->right=NULL;
|
||||
n->left=temp;
|
||||
node *temp = new node;
|
||||
temp->val = x;
|
||||
temp->left = NULL;
|
||||
temp->right = NULL;
|
||||
n->left = temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -50,13 +46,13 @@ void Insert(node *n, int x)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (n->right==NULL)
|
||||
if (n->right == NULL)
|
||||
{
|
||||
node *temp=new node;
|
||||
temp->val=x;
|
||||
temp->left=NULL;
|
||||
temp->right=NULL;
|
||||
n->left=temp;
|
||||
node *temp = new node;
|
||||
temp->val = x;
|
||||
temp->left = NULL;
|
||||
temp->right = NULL;
|
||||
n->left = temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -65,63 +61,60 @@ void Insert(node *n, int x)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int findMaxInLeftST(node *n)
|
||||
{
|
||||
while(n->right!=NULL)
|
||||
while (n->right != NULL)
|
||||
{
|
||||
n=n->right;
|
||||
n = n->right;
|
||||
}
|
||||
return n->val;
|
||||
}
|
||||
|
||||
void Remove(node *p, node *n, int x)
|
||||
{
|
||||
if (n->val==x)
|
||||
if (n->val == x)
|
||||
{
|
||||
if (n->right==NULL && n->left==NULL)
|
||||
if (n->right == NULL && n->left == NULL)
|
||||
{
|
||||
if (x<p->val)
|
||||
if (x < p->val)
|
||||
{
|
||||
p->right=NULL;
|
||||
p->right = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
p->left=NULL;
|
||||
}
|
||||
}
|
||||
else if (n->right==NULL)
|
||||
{
|
||||
if (x<p->val)
|
||||
{
|
||||
p->right=n->left;
|
||||
}
|
||||
else
|
||||
{
|
||||
p->left=n->left;
|
||||
p->left = NULL;
|
||||
}
|
||||
}
|
||||
else if (n->left==NULL)
|
||||
else if (n->right == NULL)
|
||||
{
|
||||
if (x<p->val)
|
||||
if (x < p->val)
|
||||
{
|
||||
p->right=n->right;
|
||||
p->right = n->left;
|
||||
}
|
||||
else
|
||||
{
|
||||
p->left=n->right;
|
||||
p->left = n->left;
|
||||
}
|
||||
}
|
||||
else if (n->left == NULL)
|
||||
{
|
||||
if (x < p->val)
|
||||
{
|
||||
p->right = n->right;
|
||||
}
|
||||
else
|
||||
{
|
||||
p->left = n->right;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int y=findMaxInLeftST(n->left);
|
||||
n->val=y;
|
||||
int y = findMaxInLeftST(n->left);
|
||||
n->val = y;
|
||||
Remove(n, n->right, y);
|
||||
}
|
||||
}
|
||||
else if (x<n->val)
|
||||
else if (x < n->val)
|
||||
{
|
||||
Remove(n, n->left, x);
|
||||
}
|
||||
@@ -131,14 +124,11 @@ void Remove(node *p, node *n, int x)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void BFT(node *n)
|
||||
{
|
||||
if(n!=NULL)
|
||||
if (n != NULL)
|
||||
{
|
||||
cout<<n->val<<" ";
|
||||
cout << n->val << " ";
|
||||
enqueue(n->left);
|
||||
enqueue(n->right);
|
||||
BFT(dequeue());
|
||||
@@ -147,9 +137,9 @@ void BFT(node *n)
|
||||
|
||||
void Pre(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
if (n != NULL)
|
||||
{
|
||||
cout<<n->val<<" ";
|
||||
cout << n->val << " ";
|
||||
Pre(n->left);
|
||||
Pre(n->right);
|
||||
}
|
||||
@@ -157,76 +147,72 @@ void Pre(node *n)
|
||||
|
||||
void In(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
if (n != NULL)
|
||||
{
|
||||
In(n->left);
|
||||
cout<<n->val<<" ";
|
||||
cout << n->val << " ";
|
||||
In(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Post(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
if (n != NULL)
|
||||
{
|
||||
Post(n->left);
|
||||
Post(n->right);
|
||||
cout<<n->val<<" ";
|
||||
cout << n->val << " ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
q.front=0;
|
||||
q.rear=0;
|
||||
q.front = 0;
|
||||
q.rear = 0;
|
||||
int value;
|
||||
int ch;
|
||||
node *root=new node;
|
||||
cout<<"\nEnter the value of root node :";
|
||||
cin>>value;
|
||||
root->val=value;
|
||||
root->left=NULL;
|
||||
root->right=NULL;
|
||||
node *root = new node;
|
||||
cout << "\nEnter the value of root node :";
|
||||
cin >> value;
|
||||
root->val = value;
|
||||
root->left = NULL;
|
||||
root->right = NULL;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Insert";
|
||||
cout<<"\n2. Delete";
|
||||
cout<<"\n3. Breadth First";
|
||||
cout<<"\n4. Preorder Depth First";
|
||||
cout<<"\n5. Inorder Depth First";
|
||||
cout<<"\n6. Postorder Depth First";
|
||||
cout << "\n1. Insert";
|
||||
cout << "\n2. Delete";
|
||||
cout << "\n3. Breadth First";
|
||||
cout << "\n4. Preorder Depth First";
|
||||
cout << "\n5. Inorder Depth First";
|
||||
cout << "\n6. Postorder Depth First";
|
||||
|
||||
cout<<"\nEnter Your Choice : ";
|
||||
cin>>ch;
|
||||
cout << "\nEnter Your Choice : ";
|
||||
cin >> ch;
|
||||
int x;
|
||||
switch(ch)
|
||||
switch (ch)
|
||||
{
|
||||
case 1:
|
||||
cout<<"\nEnter the value to be Inserted : ";
|
||||
cin>>x;
|
||||
Insert(root, x);
|
||||
break;
|
||||
case 2:
|
||||
cout<<"\nEnter the value to be Deleted : ";
|
||||
cin>>x;
|
||||
Remove(root, root, x);
|
||||
break;
|
||||
case 3:
|
||||
BFT(root);
|
||||
break;
|
||||
case 4:
|
||||
Pre(root);
|
||||
break;
|
||||
case 5:
|
||||
In(root);
|
||||
break;
|
||||
case 6:
|
||||
Post(root);
|
||||
break;
|
||||
case 1:
|
||||
cout << "\nEnter the value to be Inserted : ";
|
||||
cin >> x;
|
||||
Insert(root, x);
|
||||
break;
|
||||
case 2:
|
||||
cout << "\nEnter the value to be Deleted : ";
|
||||
cin >> x;
|
||||
Remove(root, root, x);
|
||||
break;
|
||||
case 3:
|
||||
BFT(root);
|
||||
break;
|
||||
case 4:
|
||||
Pre(root);
|
||||
break;
|
||||
case 5:
|
||||
In(root);
|
||||
break;
|
||||
case 6:
|
||||
Post(root);
|
||||
break;
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
} while (ch != 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user