C++ 二叉树类 创建、插入、删除 源代码

17 篇文章 0 订阅
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <windows.h> 
using namespace std;
struct TreeNode{
	struct TreeNode *left;
	struct TreeNode *right;
	float data;
};
class Tree{
	private :
		struct TreeNode *root;//树根 
	public :
		Tree(){
			this->root=NULL;//初始化 
		}
		struct TreeNode * create(float *p,int len); //创建二叉树 返回树根地址 
		void print(struct TreeNode *p);//输出二叉树前序遍历
		void print1(struct TreeNode *p);//输出二叉树中序遍历 
		void print2(struct TreeNode *p);//输出二叉树后序遍历
		void insert(float num);//插入节点
		struct TreeNode* search(float num);//搜索
		struct TreeNode* getFather(struct TreeNode *p);//父节点 
		struct TreeNode* getSuccessor(struct TreeNode *p);//后继 
		void deleteNode(struct TreeNode *p);//删除 
};

struct TreeNode * Tree::create(float *p,int len){
		struct TreeNode *temp;
		struct TreeNode *currentNode;
		struct TreeNode *fatherNode;
		//循环读入数据 
		for(int i=0;i<len;i++){
			temp=(struct TreeNode*)malloc(sizeof(struct TreeNode));
			temp->data=p[i];
			temp->left=NULL;
			temp->right=NULL;
			//如果是空树 
			if(this->root==NULL){
				this->root=temp;
			}else{
				//从根开始遍历 
				currentNode=this->root;
				while(currentNode!=NULL){
					fatherNode=currentNode;
					//往左遍历 
					if(temp->data<currentNode->data){
						currentNode=currentNode->left;
					}else{
						//往右遍历 
						currentNode=currentNode->right;
					}
				}
				//插入左子树还是右子树 
				if(temp->data<fatherNode->data){
					fatherNode->left=temp;
				}else{
					fatherNode->right=temp;	
				}
			}
		}
	return this->root;
}
//递归输出二叉树 
//前序 
void Tree::print(struct TreeNode * p){
	if(p!=NULL){
		cout<<p->data<<" ";
		print(p->left);
		print(p->right);
	}
}
//中序 
void Tree::print1(struct TreeNode * p){
	if(p!=NULL){ 
		print(p->left);
		cout<<p->data<<" ";
		print(p->right);
	}
} 
//后序 
void Tree::print2(struct TreeNode * p){
	if(p!=NULL){
		print(p->left);
		print(p->right);
		cout<<p->data<<" ";
	}
}
//插入 
void Tree::insert(float num){
	struct TreeNode *temp;
	struct TreeNode *fatherNode;
	struct TreeNode *p;
	p=(struct TreeNode*)malloc(sizeof(struct TreeNode));
	p->data=num;
	p->left=NULL;
	p->right=NULL;
	for(temp=this->root;temp!=NULL;){
		fatherNode=temp;
		if(num<temp->data){
			temp=temp->left;
		}else{
			temp=temp->right;
		}
	}
	if(num<fatherNode->data){
		fatherNode->left=p;
	}else{
		fatherNode->right=p;
	}
}
//搜索 
struct TreeNode* Tree::search(float num){
	struct TreeNode *temp;
	for(temp=this->root;temp!=NULL;){
		if(num<temp->data){
			temp=temp->left;
		}else if(num>temp->data){
			temp=temp->right;
		}else{
			return temp;
		}
	}
	return NULL;
}
//查父节点 
struct TreeNode* Tree::getFather(struct TreeNode *p){
	struct TreeNode *temp,*father;
	temp=root;
	while(temp->data!=p->data){
		father=temp;
		if(p->data<temp->data){
			temp=temp->left;
		}else{
			temp=temp->right;
		}
	}
	return father;
}
//查找后继 
struct TreeNode* Tree::getSuccessor(struct TreeNode *p){
	struct TreeNode *temp=p;
	struct TreeNode *fatherNode;
	fatherNode=p->right;
	temp=p->right->left;
	while(temp!=NULL){
		fatherNode=temp;
		temp=temp->left; 
	}
	return fatherNode;
}
//删除 
void Tree::deleteNode(struct TreeNode *p){
	//叶节点
	struct TreeNode *father=this->getFather(p);
	struct TreeNode *successor;
	if(p->left==NULL && p->right==NULL){
		if(p->data<father->data){
			father->left=NULL;
		}else{
			father->right=NULL;
		}
		free(p);
		return ;
	}
	//只含有左节点
	if(p->left!=NULL && p->right==NULL){
		father->left=p->left; 
		free(p);
		return ;
	}
	//只含有右节点
	if(p->left==NULL && p->right!=NULL){
		father->right=p->right; 
		free(p);
		return ;
	}
	//既含有左节点,又含有根节点
	if(p->left!=NULL && p->right!=NULL){
		successor=this->getSuccessor(p);
		father=this->getFather(successor);
		p->data=successor->data;
		father->left=successor->left;
		free(successor);
		return ;
	} 
	
}
int main(void){
	Tree tree;
	struct TreeNode *root,*temp,*successor,*find,*father;
	float node[9]={6,3,8,5,2,9,4,7,10};
	root=tree.create(node,sizeof(node)/sizeof(float));
	tree.insert(1);
	tree.insert(8.5);
	tree.insert(8.2);
	tree.insert(8.1);
	tree.insert(6.5);
	tree.insert(6.1);
	tree.print1(root);
	cout<<endl;
	find=tree.search(8.1);
	tree.deleteNode(find);
	tree.print1(root);
	cout<<endl;
	find=tree.search(7);
	tree.deleteNode(find);
	tree.print1(root);
	cout<<endl;
	find=tree.search(8);
	tree.deleteNode(find);
	tree.print1(root);
	return 0;
} 

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值