Create binary tree binary tree how to remove the node operation tutorial
//CPP: defines the entry point for the console application.//#include "stdafx.h"#include<iostream>#include<string>using namespace std;class TreeNode{//Create node classpublic:char num;TreeNode *leftchild,*rightchild;};class Queue{//Create queue classpublic:int front,rear;TreeNode *elem;};void cmd();void initQueue(Queue *q);bool isEmpty(Queue *q);void enQueue(Queue *q,TreeNode *e);void outQueue(Queue *q,TreeNode *e);void createBiTree(TreeNode * &T);TreeNode* PreFind(TreeNode *T,char da);void order(TreeNode *T);void midOrder(TreeNode * T);void addChild(TreeNode *T,char clue,char add,string side);void deleteNode(TreeNode *T,char delchar);int main(){//The main functioncmd();return 0;}void cmd(){//Command functionchar commands;TreeNode *T=NULL;cout<<"*"<<"___________ The command is _______________"<<endl;cout<<"*"<<"1 , press the c Create binary tree with key first; "<<"*"<<endl;cout<<"*"<<"2 , press the m Recursive traversal of binary tree in the key sequence; "<<"*"<<endl;cout<<"*"<<"3 , press the o Key hierarchy traverses binary tree; "<<"*"<<endl;cout<<"*"<<"4 , press the s Key given element to find the node; "<<"*"<<endl;cout<<"*"<<"5 , press the i Key to specify the position of the insert node; "<<"*"<<endl;cout<<"*"<<"6 , press the d Key deletes the specified node; "<<"*"<<endl;cout<<"*"<<" Please enter your choice: "<<"*"<<endl;cout<<"*"<<"__________________________________"<<endl;cin>>commands;while(commands){switch (commands){case 'c':{cout<<" Enter the binary tree to create to # Is an empty node. "<<endl;createBiTree(T);}break;case 'm':{ if(T==NULL)cout<<" The binary tree is empty. Create the binary tree first !!!"<<endl;else{cout<<" The result of middle order traversal binary tree is: ";midOrder(T);cout<<endl;}}break;case 'o':{if(T==NULL)cout<<" The binary tree is empty. Create the binary tree first !!!"<<endl;else{cout<<" The result of hierarchical traversal binary tree is: ";order(T);cout<<endl;} }break;case 's':{char ch;cout<<" Enter the element to look for: "<<endl;cin>>ch;cout<<" The left child of the node to find is: ";TreeNode *bt=PreFind(T,ch);if(bt->leftchild!=NULL)cout<<bt->leftchild->num<<endl;else cout<<" This node is a leaf without a left child !!!"<<endl;cout<<" The right child of the node to be found is: ";if(bt->rightchild!=NULL)cout<<bt->rightchild->num<<endl;else cout<<" This node is a leaf without right child !!!"<<endl;}break;case 'i':{char clue,add;string side;cout<<" Input insertion position: ";cin>>clue;cout<<" Enter the inserted element :";cin>>add;cout<<" Select the left subtree to insert ( Please enter the left) Right subtree again ( Please enter the right)";cin>>side;addChild(T,clue,add,side);}break;case 'd':{char del;cout<<" Enter the element value of the node to be deleted: "<<endl;cin>>del;deleteNode(T,del);}break;default:cout<<" Illegal input selection ";break;}cout<<" Enter your choice: "<<endl;cin>>commands;}}void initQueue(Queue *q){//Initialize queueq->elem=new TreeNode;q->front=q->rear=0;}bool isEmpty(Queue *q){//Check that the queue is emptyif(q->front==q->rear)return true;//Team is emptyelse return false;//The team is not null}void enQueue(Queue *q,TreeNode *e){//The teamq->elem[q->rear]=*e;q->rear++;}void outQueue(Queue *q,TreeNode *e){//Out of the teamif(q->front==q->rear)return;*e=q->elem[q->front];q->front++;}void createBiTree(TreeNode * &T){//Create a binary treechar ch;cin>>ch;if(ch=='#') T=NULL;else {// Use recursive first order Create a binary treeT=new TreeNode;T->num=ch;createBiTree(T->leftchild);createBiTree(T->rightchild);}}TreeNode* PreFind(TreeNode *T,char da){//Given element value to find the node pointer position and return its pointer, this method USES the first order traversalTreeNode *temp;TreeNode *tree[20];int top=0;while(T!=NULL||top!=0){while(T!=NULL){if(T->num==da)temp=T;top++;tree[top]=T;T=T->leftchild;}if(top!=0){T=tree[top]->rightchild;top--;}}return temp;}void order(TreeNode *T){//The sequence traverses the binary treeQueue *q=new Queue;TreeNode *p=new TreeNode;if(T!=NULL) {initQueue(q);enQueue(q,T);while(!isEmpty(q)){//Push the left and right children of the root node into the queueoutQueue(q,p);cout<<p->num<<" ";if(p->leftchild!=NULL)enQueue(q,p->leftchild);if(p->rightchild!=NULL)enQueue(q,p->rightchild);}}elsecout<<" The binary tree is empty !!!";}void midOrder(TreeNode * T){//Order recursive traversal in binary treesif(T!=NULL){midOrder(T->leftchild);//The middle order traverses the left subtree of Tcout<<T->num<<" "; //Accessing the root nodemidOrder(T->rightchild);//The middle order traverses the right subtree of T}}void addChild(TreeNode *T,char clue,char add,string side){//Insert left child operation, look up the node according to clue, and it is up to side to insert left child or right childTreeNode *&late=new TreeNode;late->num=add;late->leftchild=NULL;late->rightchild=NULL;TreeNode *original=PreFind(T,clue);//Finds the specified nodeif(side=="left"){if(original->leftchild==NULL){//The left child of the root is niloriginal->leftchild=late;}}else{if(original->rightchild==NULL){//The right child of the root is niloriginal->rightchild=late;}}}void deleteNode(TreeNode *T,char delchar){ //Delete the node and its childrenif (T!=NULL){//If the root node is not emptyif (T->num==delchar){//If the root node is the node to be deleteddelete T->leftchild;//Delete the left child nodeT->leftchild=NULL;//The left pointer points to NULLdelete T->rightchild;//Delete the right child nodeT->rightchild=NULL;//The right pointer points to NULLdelete T;//Delete node T}else if (T->leftchild!=NULL&&T->leftchild->num==delchar){//If the left child is the node to be deleteddelete T->leftchild->leftchild;//Delete the left child of the left child firstdelete T->leftchild->rightchild;//Delete the left child's right childdelete T->leftchild;//Finally delete left childT->leftchild=NULL;//The left pointer is null}else if (T->rightchild!=NULL&&T->rightchild->num==delchar){//If the right child is the node to be deleteddelete T->rightchild->leftchild;//Delete the left child of the right child firstdelete T->rightchild->rightchild;//Delete the right child of the right childdelete T->rightchild;//Finally delete the right childT->rightchild=NULL;//The right pointer is null}else {if(T->leftchild!=NULL){ //If the left child is not emptydeleteNode(T->leftchild,delchar);//Delete left child node}if(T->rightchild!=NULL){ //If the right child is not emptydeleteNode(T->rightchild,delchar);//Delete the right child node}}}}