二叉树的实现(C++版)


#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

class CNode                          //结点
{
public:
	int data;
	CNode *left;
	CNode *right;
	CNode()
	{
		left=NULL;
		right=NULL;
	}	
};

class CBtree
{
public:
	CNode *root;                                     //根结点
	static int n;                                        //存储叶子结点数,配合函数countleaf
	static int m;                                        //存储出度为1的结点数(单结点)
	CBtree();
	void Addnode(int x);                                           //创建二叉树
	void Preorder(CNode *p)const;                           //先序遍历
	void inorder(CNode *p)const;                             //中序遍历
	void Postorder(CNode *p)const;                         //后序遍历
	int count(CNode *p) const;                                //求结点数
	bool isEmpty() const;                                          //判断树是否为空
	int countleaf(CNode *p) const;                          //求叶子结点数
	int countsingle(CNode *temp)const;
	void Delete(CNode *p);                                     //删除头结点为p的树
	~CBtree();                                                         //销毁链表
	bool Find(const int e) const;                             //是否含有值为e的结点
	CNode* Getparent(const int e) const;               //返回值为e的父结点
};
int CBtree::n=0;
int CBtree::m=0;
CBtree::CBtree()
{
	root=NULL;	
}

void CBtree::Addnode(int x)
{
	CNode *newnode;	
	newnode=new CNode();
	newnode->data=x;
	if(root==NULL)
	{
		root=newnode;
		return;
	}
	else
	{
		CNode *parent,*current;
		current=root;
		parent=NULL;
		while(current!=NULL)
		{
			parent=current;
			if(x<current->data)
				current=current->left;
			else
				current=current->right;
		}
		if(x<parent->data)
			parent->left=newnode;
		else
			parent->right=newnode;
	}
}

int CBtree::count(CNode *p) const             
{
	if(p==NULL)
		return 0;
	else
		return count(p->left)+count(p->right)+1;
}

void CBtree::Preorder(CNode *p)const
{
	if(p!=NULL)
	{
		cout<<p->data<<" ";
		Preorder(p->left);
		Preorder(p->right);
	}
}

void CBtree::inorder(CNode *p) const
{
	if(p!=NULL)
	{
		inorder(p->left);
		cout<<p->data<<" ";
		inorder(p->right);
	}
}

void CBtree::Postorder(CNode *p)const
{
	if(p!=NULL)
	{
		Postorder(p->left);
		Postorder(p->right);
		cout<<p->data<<" ";
	}
}

bool CBtree::isEmpty()const
{
	return (root==NULL);
}

int CBtree::countleaf(CNode *p) const
{
	if(p==NULL)
		return 0;
	else
	{
		if(p->left==NULL&&p->right==NULL)
			return n+=1;
		else
		{
			countleaf(p->left);
			countleaf(p->right);
		}
		return n;
	}
}

int CBtree::countsingle(CNode *temp)const
{
	if(temp==NULL) return 0;
	else
	{
		if(temp->left!=NULL&&temp->right!=NULL)
		{
			countsingle(temp->left);
			countsingle(temp->right);
		}
		if(temp->left!=NULL&&temp->right==NULL)
		{
			m+=1;
			countsingle(temp->left);
		}
		if(temp->left==NULL&&temp->right!=NULL)
		{
			m+=1;
			countsingle(temp->right);
		}
	}
	return m;
}

void CBtree::Delete(CNode *p)
{
	if(p==NULL)                                                        
		return ;
	else if(p->left==NULL&&p->right==NULL)
	{
		delete p;
		return ;
	}
	else if(p->left!=NULL&&p->right==NULL)
	{
		Delete(p->left);
		delete p;
		return ;
	}
	else if(p->left==NULL&&p->right!=NULL)
	{
		Delete(p->right);
		delete p;
		return ;
	}
	else
	{
		Delete(p->left);
		Delete(p->right);
		delete p;
		return ;
	}
}

CBtree::~CBtree()
{
	Delete(root);
}

bool CBtree::Find(const int e) const
{
	CNode *p=root;
	while(p)
	{
		if(p->data==e)
			return true;
		else if(e<p->data)
		{
			p=p->left;
		}
		else
		{
			p=p->right;
		}
	}
	return false;
}

CNode* CBtree::Getparent(const int e) const
{
	CNode *parent=root;
	CNode *q=NULL;
	if(root==NULL)                    //树为空
		throw e;
	if(root->data==e)               //根结点没有父结点
		throw e;
	if(!Find(e))                            //树中没有值为e的结点
		throw e;
	else
	{		
		if(e<parent->data)
			q=parent->left;
		else
			q=parent->right;
		while(q)
		{			
			if(e==q->data)
				break;
			parent=q;
			if(e<q->data)
				q=q->left;
			else
			{
				q=q->right;
			}			
		}
	}
	return parent;
}

int main()
{
	CBtree A;
	int array[]={7,4,2,3,15,35,6,45,55,20,1,14,56,57,58};
	int k;
	k=sizeof(array)/sizeof(array[0]);
	cout<<"建立排序二叉树顺序: "<<endl;
	for(int i=0;i<k;i++)
	{
		cout<<array[i]<<" ";
		A.Addnode(array[i]);
	}
	cout<<endl;
	cout<<"二叉树节点个数: "<<A.count(A.root)<<endl;
	cout<<"二叉树叶子个数:"<<A.countleaf(A.root)<<endl;
	cout<<"二叉树中出度为1的结点数量为:"<<A.countsingle(A.root)<<endl;
	cout<<endl<<"先序遍历序列: "<<endl;
	A.Preorder(A.root);
	cout<<endl<<"中序遍历序列: "<<endl;
	A.inorder(A.root);
	cout<<endl<<"后序遍历序列: "<<endl;
	A.Postorder(A.root);
	cout<<endl;
	A.Delete(A.root->right);
	A.root->right=NULL;
	A.Postorder(A.root);
	cout<<endl;
	cout<<A.Find(10)<<endl;
	try
	{
		cout<<(A.Getparent(3))->data<<endl;
	}catch(int e)
	{
		cout<<"空树或根结点没有父结点或树中不含值为"
			<<e<<"的结点"<<endl;
	}
	system("pause");
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值