Copy codeThe Code is as follows: // binary tree. cpp: defines the entry point of the console application.
//
/*
* Binary job
* 2012.12.1
* Made By Karld Vorn Doenitz
*/
# Include "stdafx. h"
# Include <iostream>
# Include <string>
Using namespace std;
Class TreeNode {// create a node class
Public:
Char num;
TreeNode * leftchild, * rightchild;
};
Class Queue {// create a 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
/*
* The following are command lines.
* There are six types of commands
*/
Char commands;
TreeNode * T = NULL;
Cout <"*" <"___________ command _______________" <endl;
Cout <"*" <"1. Press the c key to create a binary tree first." <"*" <endl;
Cout <"*" <"2. Press the m key to recursively traverse the binary tree;" <"*" <endl;
Cout <"*" <"3. Press the o key to traverse the binary tree hierarchy;" <"*" <endl;
Cout <"*" <"4. Press the s key to specify an element to search for a node." <"*" <endl;
Cout <"*" <"5. Press the I key to insert a node." <"*" <endl;
Cout <"*" <"6. Press d to delete the specified node." <"*" <endl;
Cout <"*" <"Enter your choice:" <"*" <endl;
Cout <"*" <"__________________________________" <endl;
Cin> commands;
While (commands ){
/*
* Switch statement
* While Loop
*/
Switch (commands ){
Case 'C ':
{
Cout <"Enter the binary tree to be created, and use # As the empty node. "<Endl;
CreateBiTree (T );
} Break;
Case 'M ':
{If (T = NULL) cout <"this binary tree is empty. Create a binary tree first !!! "<Endl;
Else {
Cout <"result of traversing a binary tree in a forward direction :";
MidOrder (T );
Cout <endl ;}
} Break;
Case 'O ':{
If (T = NULL) cout <"this binary tree is empty. Create a binary tree first !!! "<Endl;
Else {cout <"the result of layered binary tree traversal is :";
Order (T );
Cout <endl;
} Break;
Case's ': {char ch;
Cout <"Enter the element to be searched:" <endl;
Cin> ch;
Cout <"the left child of the node to be searched is :";
TreeNode * bt = PreFind (T, ch );
If (bt-> leftchild! = NULL)
Cout <bt-> leftchild-> num <endl;
Else cout <"this node is a leaf with no 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 with no right child !!! "<Endl;
} Break;
Case 'I': {char clue, add;
String side;
Cout <"input Insert Location :";
Cin> clue;
Cout <"input inserted elements :";
Cin> add;
Cout <"select whether to insert the left subtree (Enter left) or the right subtree (enter 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"; break;
}
Cout <"Enter your choice:" <endl;
Cin> commands;
}
}
Void initQueue (Queue * q) {// initialize the 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; // The team is empty.
Else return false; // The team is not empty.
}
Void enQueue (Queue * q, TreeNode * e) {// enter the Queue
Q-> elem [q-> rear] = * e;
Q-> rear ++;
}
Void outQueue (Queue * q, TreeNode * e) {// team out
If (q-> front = q-> rear) return;
* E = q-> elem [q-> front];
Q-> front ++;
}
Void createBiTree (TreeNode * & T) {// create a binary tree
Char ch;
Cin> ch;
If (ch = '#') T = NULL;
Else {// create a binary tree in recursive first order
T = new TreeNode;
T-> num = ch;
CreateBiTree (T-> leftchild );
CreateBiTree (T-> rightchild );
}
}
TreeNode * PreFind (TreeNode * T, char da) {// find the node pointer position for the given element value and return its pointer. This method uses 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 nodes of the root node to 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 binary tree is empty !!! ";
}
Void midOrder (TreeNode * T) {// recursive traversal in a binary tree
If (T! = NULL ){
MidOrder (T-> leftchild); // traverses the left subtree of T in the middle order
Cout <T-> num <"; // access the root node
MidOrder (T-> rightchild); // traverses the right subtree of T in the middle order
}
}
Void addChild (TreeNode * T, char clue, char add, string side) {// insert left and right children. Search Nodes Based on clue. The side determines whether to insert left or right children.
TreeNode * & late = new TreeNode;
Late-> num = add;
Late-> leftchild = NULL;
Late-> rightchild = NULL;
TreeNode * original = PreFind (T, clue); // you can specify a node.
If (side = "left "){
If (original-> leftchild = NULL) {// The left child node of the root node is empty.
Original-> leftchild = late;
}
} Else {
If (original-> rightchild = NULL) {// The right child node of the root node is empty.
Original-> rightchild = late;
}
}
}
Void deleteNode (TreeNode * T, char delchar) {// delete a node and Its subnodes
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; // delete the left child node
T-> leftchild = NULL; // The left Pointer Points to NULL
Delete T-> rightchild; // delete the right child node
T-> rightchild = NULL; // The right Pointer Points to NULL
Delete T; // delete node T
} Else if (T-> leftchild! = NULL & T-> leftchild-> num = delchar) {// If the left child is the node to be deleted
Delete T-> leftchild; // delete the left child first
Delete T-> leftchild-> rightchild; // delete the right child of the left child
Delete T-> leftchild; // delete the left child
T-> 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 deleted
Delete T-> rightchild-> leftchild; // delete the left child of the right child first
Delete T-> rightchild; // delete the right child of the right child
Delete T-> rightchild; // delete the right child
T-> rightchild = NULL; // The right pointer is NULL.
} Else {
If (T-> leftchild! = NULL) {// If the left child is not empty
DeleteNode (T-> leftchild, delchar); // Delete the left child node
} If (T-> rightchild! = NULL) {// if the right child is not empty
DeleteNode (T-> rightchild, delchar); // Delete the right child node
}
}
}
}