Copy Code code as follows:
Binary tree. CPP: Defines the entry point for a console application.
//
/*
* Two fork Tree operation
*2012.12.1 13:55
*made by Karld Vorn Doenitz
*/
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace Std;
Class treenode{//Building Node class
Public
Char num;
TreeNode *leftchild,*rightchild;
};
Class queue{//Build Queue class
Public
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 () {//main function
CMD ();
return 0;
}
void cmd () {//Command function
/*
* Following is the command line instruction
* A total of six orders
*/
char commands;
TreeNode *t=null;
cout<< "*" << "___________ commands are as follows _______________" <<endl;
cout<< "*" << "1, press the C key first preface to create two fork tree;" << "*" <<endl;
cout<< "*" << "2, press the M key in the sequential recursive traversal of the binary tree;" << "*" <<endl;
cout<< "*" << "3, press the O-key hierarchy traverse the binary tree;" << "*" <<endl;
cout<< "*" << "4, press the S-key given element lookup node;" << "*" <<endl;
cout<< "*" << "5, press the I key to specify the position to insert the node;" << "*" <<endl;
cout<< "*" << "6, press the D key to delete the specified node; <<" * "<<endl;
cout<< "*" << "Please enter your choice:" << "*" <<endl;
cout<< "*" << "__________________________________" <<endl;
cin>>commands;
while (commands) {
/*
* Use switch statement
*while Cycle
*/
Switch (commands) {
Case ' C ':
{
cout<< "Enter the two-fork tree you want to create with a # empty node. "<<endl;
Createbitree (T);
}break;
Case ' m ':
{if (t==null) cout<< "This binary tree is empty, first create a two-fork tree!!!" <<endl;
else{
cout<< "The result of the sequence traversal binary tree is:";
Midorder (T);
Cout<<endl;}
}break;
Case ' O ': {
if (t==null) cout<< "This binary tree is empty, first create a two-fork tree!!!" <<endl;
else{cout<< "Hierarchical traversal of the binary tree result is:";
Order (T);
cout<<endl;
}}break;
Case ' s ': {char ch;
cout<< "Enter the element you want to find:" <<endl;
cin>>ch;
cout<< "To find the node of the left child is:";
TreeNode *bt=prefind (T,CH);
if (bt->leftchild!=null)
cout<<bt->leftchild->num<<endl;
else cout<< "This node is a leaf, no left child!!!" <<endl;
cout<< "The right child of the node to find is:";
if (bt->rightchild!=null)
cout<<bt->rightchild->num<<endl;
else cout<< "This node is a leaf, no right child!!!" <<endl;
}break;
Case ' I ': {char clue,add;
string side;
cout<< "Enter insert position:";
cin>>clue;
cout<< "Enter the inserted element:";
cin>>add;
cout<< "Select whether you want to insert the Zuozi (enter left) or the right subtree (please enter it)";
cin>>side;
AddChild (T,clue,add,side);
}break;
Case ' d ': {
Char del;
cout<< "Enter the element value of the node you want to delete:" <<endl;
cin>>del;
Deletenode (T,del);
}break;
default:cout<< "Input selection illegal";
}
cout<< "Enter your choice:" <<endl;
cin>>commands;
}
}
void Initqueue (Queue *q) {//initialization queue
Q->elem=new TreeNode;
q->front=q->rear=0;
}
BOOL IsEmpty (Queue *q) {//Check whether the queue is empty
if (q->front==q->rear)
Return true;//team is empty
else return false;//team is not empty
}
void EnQueue (Queue *q,treenode *e) {//Brigade
q->elem[q->rear]=*e;
q->rear++;
}
void Outqueue (Queue *q,treenode *e) {//Outbound Team
if (q->front==q->rear) return;
*e=q->elem[q->front];
q->front++;
}
void Createbitree (TreeNode * &t) {//Create two fork Tree
Char ch;
cin>>ch;
if (ch== ' # ') t=null;
else {//use recursive first order to create a two-fork tree
T=new TreeNode;
t->num=ch;
Createbitree (T->leftchild);
Createbitree (T->rightchild);
}
}
treenode* prefind (TreeNode *t,char da) {///The given element value finds the node pointer position and returns its pointer, which takes the first order traversal
TreeNode *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) {//sequence traversal binary tree
Queue *q=new queue;
TreeNode *p=new TreeNode;
if (t!=null) {
Initqueue (q);
EnQueue (q,t);
while (!isempty (q)) {//Push the left and right two child nodes of the root node into the queue
Outqueue (Q,P);
cout<<p->num<< "";
if (p->leftchild!=null)
EnQueue (Q,p->leftchild);
if (p->rightchild!=null)
EnQueue (Q,p->rightchild);
}
}else
cout<< "This two fork tree is empty!!!";
}
void Midorder (TreeNode * T) {//two sequential recursive traversal in a fork tree
if (t!=null) {
Midorder (t->leftchild);//sequence Traversal T Zuozi
cout<<t->num<< ""; Accessing the root node
Midorder (t->rightchild);//in-order traversal of the right subtree of T
}
}
void AddChild (TreeNode *t,char clue,char add,string side) {//insert left and right child operation, find node according to Clue, decide whether to insert the child by side
TreeNode *&late=new TreeNode;
late->num=add;
late->leftchild=null;
late->rightchild=null;
TreeNode *original=prefind (T,clue);//Find the specified node
if (side== "left") {
The left child node of the IF (original->leftchild==null) {//root node is empty
original->leftchild=late;
}
}else{
The right child node of the IF (original->rightchild==null) {//root node is empty
original->rightchild=late;
}
}
}
void Deletenode (TreeNode *t,char Delchar) {//delete node and its child nodes
if (t!=null) {//If the root node is not empty
if (T->num==delchar) {//If the root node is the node to be deleted
Delete t->leftchild;//Remove left child node
t->leftchild=null;//left pointer points to NULL
Delete t->rightchild;//Remove right child node
t->rightchild=null;//right pointer points to NULL
Delete t;//Remove node T
}else if (T->leftchild!=null&&t->leftchild->num==delchar) {//If the left child is the node to be deleted
Delete t->leftchild->leftchild;//remove left child's left child first
Delete t->leftchild->rightchild;//then remove the left child's right child
Delete t->leftchild;//finally remove the left child
t->leftchild=null;//left pointer is empty
}else if (T->rightchild!=null&&t->rightchild->num==delchar) {//If the right child is the node to be deleted
Delete t->rightchild->leftchild;//first remove the right child's left child
Delete t->rightchild->rightchild;//and remove right child's right child
Delete t->rightchild;//last deleted right child
t->rightchild=null;//Right pointer is empty
}else {
if (t->leftchild!=null) {//If the left child is not empty
Deletenode (T->leftchild,delchar)//delete left child node
}if (t->rightchild!=null) {//If the right child is not empty
Deletenode (T->rightchild,delchar);//Delete Right child node
}
}
}
}