没有合适的资源?快使用搜索试试~ 我知道了~
这是数据结构C++二叉树的源代码,希望对大家有用
资源推荐
资源详情
资源评论
#include<iostream>
using namespace std;
#include<assert.h>
class BinTreeNode { //结点类的定义
public:
BinTreeNode () {leftChild =NULL;rightChild =NULL;}
BinTreeNode(int x,BinTreeNode *l=NULL,BinTreeNode *r=NULL)
{data=x;leftChild=l;rightChild=r;}
~BinTreeNode () {} //析构函数
BinTreeNode *leftChild, *rightChild; //左、右子女链域
int data; //数据域
};
class BinaryTree{
public:BinTreeNode *root;
int RefValue;
//void destroy(BinTreeNode* subTree);
BinaryTree(): root (NULL) {}
BinaryTree (int value) {RefValue=value;root=NULL;}
// ~BinaryTree(){ destroy(root);}
int IsEmpty () { return (root == NULL) ? true : false; }
BinTreeNode *Parent (BinTreeNode *current )
{ return (root == NULL||root == current)?
NULL:Parent(root, current ); }
BinTreeNode *LeftChild (BinTreeNode *current )
{ return (current!= NULL)? current->leftChild :NULL; }
BinTreeNode *RightChild (BinTreeNode *current )
{ return (current!=NULL) ? current->rightChild : NULL; }
BinTreeNode *GetRoot () const { return root; }
int Insert(const int& item);
BinTreeNode *Find(int &item)const;
using namespace std;
#include<assert.h>
class BinTreeNode { //结点类的定义
public:
BinTreeNode () {leftChild =NULL;rightChild =NULL;}
BinTreeNode(int x,BinTreeNode *l=NULL,BinTreeNode *r=NULL)
{data=x;leftChild=l;rightChild=r;}
~BinTreeNode () {} //析构函数
BinTreeNode *leftChild, *rightChild; //左、右子女链域
int data; //数据域
};
class BinaryTree{
public:BinTreeNode *root;
int RefValue;
//void destroy(BinTreeNode* subTree);
BinaryTree(): root (NULL) {}
BinaryTree (int value) {RefValue=value;root=NULL;}
// ~BinaryTree(){ destroy(root);}
int IsEmpty () { return (root == NULL) ? true : false; }
BinTreeNode *Parent (BinTreeNode *current )
{ return (root == NULL||root == current)?
NULL:Parent(root, current ); }
BinTreeNode *LeftChild (BinTreeNode *current )
{ return (current!= NULL)? current->leftChild :NULL; }
BinTreeNode *RightChild (BinTreeNode *current )
{ return (current!=NULL) ? current->rightChild : NULL; }
BinTreeNode *GetRoot () const { return root; }
int Insert(const int& item);
BinTreeNode *Find(int &item)const;
void CreateBinTree(BinTreeNode* &subTree) ;
BinTreeNode *Parent (BinTreeNode * subTree, BinTreeNode *current );
int Height(BinTreeNode *subTree);
int Size(BinTreeNode *subTree);
void PreOrder(BinTreeNode *subTree );//前序遍历
void InOrder(BinTreeNode *subTree );//中序遍历
void PostOrder(BinTreeNode *subTree );//后序遍历
void levelOrder(BinTreeNode &subTree);//层序遍历
void destroy(BinTreeNode* &subTree);
int leafsize(BinTreeNode* &subTree);
};
BinTreeNode *BinaryTree::Parent(BinTreeNode *subTree, BinTreeNode *current) {
//私有函数: 从结点 subTree 开始, 搜索结点 current 的
//双亲, 若找到则返回双亲结点地址, 否则返回NULL
if (subTree == NULL) return NULL;
if (subTree->leftChild ==current ||
subTree->rightChild == current )
return subTree;//找到, 返回父结点地址
BinTreeNode *p;
if ((p=Parent(subTree->leftChild, current))!= NULL)
return p; //递归在左子树中搜索
else return Parent(subTree->rightChild, current);
//递归在右子树中搜索
};
/*void BinaryTree::destroy(BinTreeNode* subTree) {
//私有函数: 删除根为subTree的子树
if (subTree != NULL) {
destroy(subTree->leftChild); //删除左子树
destroy(subTree->rightChild); //删除右子树
delete subTree; //删除根结点
BinTreeNode *Parent (BinTreeNode * subTree, BinTreeNode *current );
int Height(BinTreeNode *subTree);
int Size(BinTreeNode *subTree);
void PreOrder(BinTreeNode *subTree );//前序遍历
void InOrder(BinTreeNode *subTree );//中序遍历
void PostOrder(BinTreeNode *subTree );//后序遍历
void levelOrder(BinTreeNode &subTree);//层序遍历
void destroy(BinTreeNode* &subTree);
int leafsize(BinTreeNode* &subTree);
};
BinTreeNode *BinaryTree::Parent(BinTreeNode *subTree, BinTreeNode *current) {
//私有函数: 从结点 subTree 开始, 搜索结点 current 的
//双亲, 若找到则返回双亲结点地址, 否则返回NULL
if (subTree == NULL) return NULL;
if (subTree->leftChild ==current ||
subTree->rightChild == current )
return subTree;//找到, 返回父结点地址
BinTreeNode *p;
if ((p=Parent(subTree->leftChild, current))!= NULL)
return p; //递归在左子树中搜索
else return Parent(subTree->rightChild, current);
//递归在右子树中搜索
};
/*void BinaryTree::destroy(BinTreeNode* subTree) {
//私有函数: 删除根为subTree的子树
if (subTree != NULL) {
destroy(subTree->leftChild); //删除左子树
destroy(subTree->rightChild); //删除右子树
delete subTree; //删除根结点
剩余5页未读,继续阅读
资源评论
smiholy
- 粉丝: 3
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功