二叉树创建+删除+增加流程介绍

来源:爱站网时间:2020-09-22编辑:网友分享
本文今天来给大家分享下C语言下,关于二叉树的创建,节点删除,节点增加等一系列操作方法要如何使用,给有需要的朋友参考参考下!

本文今天来给大家分享下C语言下,关于二叉树的创建,节点删除,节点增加等一系列操作方法要如何使用,给有需要的朋友参考参考下! 

 

// 二叉树.cpp : 定义控制台应用程序的入口点。
//
/*
*二叉树作业
*2012.12.1 13:55
*Made By Karld Vorn Doenitz
*/
#include "stdafx.h"
#include
#include
using namespace std;
class TreeNode{//建立节点类
public:
char num;
TreeNode *leftchild,*rightchild;
};
class Queue{//建立队列类
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(){//主函数
cmd();
return 0;
}
void cmd(){//命令函数
/*
*以下为命令行指令
*共有六种命令
*/
char commands;
TreeNode *T=NULL;
cout cout cout cout cout cout cout cout cout cin>>commands;
while(commands){
/*
*采用switch语句
*while循环
*/
switch (commands){
case 'c':
{
cout createBiTree(T);
}break;
case 'm':
{ if(T==NULL)cout else{
cout midOrder(T);
cout }break;
case 'o':{
if(T==NULL)cout else{cout order(T);
cout } }break;
case 's':{char ch;
cout cin>>ch;
cout TreeNode *bt=PreFind(T,ch);
if(bt->leftchild!=NULL)
coutleftchild->num else cout cout if(bt->rightchild!=NULL)
coutrightchild->num else cout }break;
case 'i':{char clue,add;
string side;
cout cin>>clue;
cout cin>>add;
cout cin>>side;
addChild(T,clue,add,side);
}break;
case 'd':{
char del;
cout cin>>del;
deleteNode(T,del);
}break;
default:cout }
cout cin>>commands;
}
}
void initQueue(Queue *q){//初始化队列
q->elem=new TreeNode;
q->front=q->rear=0;
}
bool isEmpty(Queue *q){//检查队列是否为空
if(q->front==q->rear)
return true;//队为空
else return false;//队不为空
}
void enQueue(Queue *q,TreeNode *e){//入队
q->elem[q->rear]=*e;
q->rear++;
}
void outQueue(Queue *q,TreeNode *e){//出队
if(q->front==q->rear)return;
*e=q->elem[q->front];
q->front++;
}
void createBiTree(TreeNode * &T){//创建二叉树
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else {//采用递归先序创建二叉树
T=new TreeNode;
T->num=ch;
createBiTree(T->leftchild);
createBiTree(T->rightchild);
}
}
TreeNode* PreFind(TreeNode *T,char da){//给定元素值查找结点指针位置并返回其指针,此方法采用的先序遍历
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){//层序遍历二叉树
Queue *q=new Queue;
TreeNode *p=new TreeNode;
if(T!=NULL) {
initQueue(q);
enQueue(q,T);
while(!isEmpty(q)){//将根节点的左右两个子节点推入队内
outQueue(q,p);
coutnum if(p->leftchild!=NULL)
enQueue(q,p->leftchild);
if(p->rightchild!=NULL)
enQueue(q,p->rightchild);
}
}else
cout }
void midOrder(TreeNode * T){//二叉树中序递归遍历
if(T!=NULL){
midOrder(T->leftchild);//中序遍历T的左子树
coutnum midOrder(T->rightchild);//中序遍历T的右子树
}
}
void addChild(TreeNode *T,char clue,char add,string side){//插入左右孩子操作,根据clue查找节点,由side决定插入左孩子还是右孩子
TreeNode *&late=new TreeNode;
late->num=add;
late->leftchild=NULL;
late->rightchild=NULL;
TreeNode *original=PreFind(T,clue);//查找指定的节点
if(side=="left"){
if(original->leftchild==NULL){//根结点的左孩子结点为空
original->leftchild=late;
}
}else{
if(original->rightchild==NULL){//根结点的右孩子结点为空
original->rightchild=late;
}
}
}
void deleteNode(TreeNode *T,char delchar){ //删除节点及其子节点
if (T!=NULL){//如果根节点不为空
if (T->num==delchar){//如果根节点为要删除的节点
delete T->leftchild;//删除左孩子节点
T->leftchild=NULL;//左指针指向NULL
delete T->rightchild;//删除右孩子节点
T->rightchild=NULL;//右指针指向NULL
delete T;//删除节点T
}else if (T->leftchild!=NULL&&T->leftchild->num==delchar){//如果左孩子为要删除的节点
delete T->leftchild->leftchild;//先删除左孩子的左孩子
delete T->leftchild->rightchild;//再删除左孩子的右孩子
delete T->leftchild;//最后删除左孩子
T->leftchild=NULL;//左指针为空
}else if (T->rightchild!=NULL&&T->rightchild->num==delchar){//如果右孩子为要删除的节点
delete T->rightchild->leftchild;//先删除右孩子的左孩子
delete T->rightchild->rightchild;//再删除右孩子的右孩子
delete T->rightchild;//最后删除右孩子
T->rightchild=NULL;//右指针为空
}else {
if(T->leftchild!=NULL){ //如果左孩子不为空
deleteNode(T->leftchild,delchar);//删除左孩子结点
}if(T->rightchild!=NULL){ //如果右孩子不为空
deleteNode(T->rightchild,delchar);//删除右孩子节点
}
}
}
}

看完以上密密麻麻的代码后,不知道朋友们对此有没有更加了解些,遇到什么技术类型的文章,可以来js.aizhan.com查找哦!

上一篇:invalid+argument应用在C语言过程

下一篇:C语言右值引用该怎么使用

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载