二叉树的C++实现(简单版)
2015-01-30 13:30
113 查看
#pragma once
#include <queue>
#include <stack>
#ifndef NULL
#define NULL 0
#endif
#ifndef MAX
#define MAX(x,y) ((x) > (y)? (x):(y))
#endif
#define stature(x) ((&x)? (x).height : -1) //空树高为-1
#define IsRoot(x) ((x).parent == NULL)
#define HasLChild(x) ((x).lchild != NULL)
#define HasRChild(x) ((x).rchild != NULL)
#define HasChild(x) (HasLChild(x) || HasRChild((x)))
#define HasBothChild(x) (HasLChild(x) && HasRChild((x)))
#define IsLeafNode(x) (!HasChild(x))
#define IsLChild(x) (IsRoot(x)? NULL : (HasLChild(x) ? (x) == (x).lchild : (x) == (x).rchild))
#define IsRChild(x) (IsRoot(x)? NULL : (HasRChild(x) ? (x) == (x).rchild : (x) == (x).rchild))
#define Brother(x) ((IsRoot(x)? NULL: (IsLChild((x)) ? (x).parent->rchild : (x).parent->lchild))
#define Uncle(x) (IsRoot(x)? NULL : Brother(*((x)->parent)))
template<typename T> struct BinNode
{
T data; //数值
int height;
BinNode<T>* parent;
BinNode<T>* lchild;
BinNode<T>* rchild;
BinNode(){
height = 0;
parent = NULL;
lchild = NULL;
rchild = NULL;
}
BinNode(T e, BinNode<T>* p = NULL, BinNode<T>* l = NULL, BinNode<T>* r = NULL, int h = NULL)
{
data = e;
parent = p;
lchild = l;
rchild = r;
height = h;
}
~BinNode(void){};
int size(BinNode<T>* node) //后代总数(包括本身)
{
if (IsLeafNode(*node)) return 1;
if (HasBothChild(*node))
return size(node->lchild) + size(node->rchild) + 1;
if (HasLChild(*node)) return size(node->lchild) + 1;
if (HasRChild(*node)) return size(node->rchild) + 1;
}
//操作符
bool operator == (BinNode<T>& node) { return data == node.data; }
bool operator != (BinNode<T>& node) { return data != node.data; }
//遍历------------------------------------------
//层次遍历(遍历以本节点开始的树)
template <class VST> void TraveLevel(VST visit)
{
queue<BinNode<T>*> que;
que.push(this);
while (que.empty() == false){
visit(*que.front());
if (HasLChild(*que.front())) que.push(que.front()->lchild);
if (HasRChild(*que.front())) que.push(que.front()->rchild);
que.pop();
}//while
}
//先序遍历(遍历以本节点开始的树)
template <class VST> void TravePre(VST& visit)
{
BinNode<T>* node_temp = this;
stack<BinNode<T>*> stk;
do {
if (HasRChild(node_temp)) push(node_temp->rchild);
visit(*node_temp);
node_temp = node_temp->lchild;
if (node_temp == NULL){
node_temp = stk.top();
stk.pop();
}
} while (!(node_temp == NULL && stk.empty()));
}
};
#pragma once
#include "BinNode.h"
template <class T> class BinTree
{
protected:
int _size;
BinNode<T>* _root; //根节点
virtual int UpdateHeight(BinNode<T>* x) //每一个节点的高度的更新
{
x->height = MAX(stature(*(x->lchild)), stature(*(x->rchild))) + 1;
return x->height;
}
void UpdateHeightAbove(BinNode<T>* x) //更新x以及以上节点的高度
{
while (x) {
UpdateHeight(x);
x = x->parent;
}
}
public:
BinTree() { _size = 0, _root = NULL; }
~BinTree(){};
//操作接口
bool empty() { return !_root; }
int size() const { return _size; }
inline BinNode<T>* RootPtr() const { return _root; }
BinNode<T>* InsertAsRoot(T const & e)
{
BinNode<T>* temp = new BinNode<T>(e,NULL,NULL,NULL,0);
if (empty())
{
_root = temp;
_size = 1;
}
else{
temp->data = _root->data;
temp->height = _root->height;
temp->lchild = _root->lchild;
temp->rchild = _root->rchild;
temp->parent = _root->parent;
_root = temp;
}
return _root;
}
BinNode<T>* InsertAsLNode(BinNode<T>* node, T const & e)
{
BinNode<T>* temp = new BinNode<T>(e, NULL, NULL, NULL, 0);
node->lchild = temp;
temp->parent = node;
UpdateHeightAbove(temp);
_size++;
return temp;
}
BinNode<T>* InsertAsRNode(BinNode<T>* node, T const & e)
{
BinNode<T>* temp = new BinNode<T>(e, NULL, NULL, NULL, 0);
node->rchild = temp;
temp->parent = node;
UpdateHeightAbove(temp);
_size++;
return temp;
}
int AttachAsLC(BinNode<T>* node, BinTree<T> & e)
{
.>RootPtr()->parent = node;
node->lchild = e.RootPtr();
UpdateHeightAbove(node);
_size += node->size();
return e.size();
}
int AttachAsRC(BinNode<T>* node, BinTree<T> & e)
{
e.RootPtr()->parent = node;
node->rchild = e.RootPtr();
UpdateHeightAbove(node);
_size += node->rchild->size(node->rchild);
return e.size();
}
BinTree<T>* Secede(BinNode<T>* p) //移出p以及其子树(返回删除节点作为根节点的树)
{
BinTree<T>* tree_temp = new BinTree<T>;
tree_temp->InsertAsRoot = p;
_size -= tree_temp->size();
UpdateHeightAbove(p->parent);
IsLChild(p) ? p->lchild = NULL : p->rchild = NULL;
return tree_temp;
}
//遍历接口
template <class VST> void TraveLevel(VST visi)
{
if(_root) _root->TraveLevel(visit);
}
template <class VST> void TravePre(VST& visit)
{
if (_root) _root->TravePre(visit);
}
};
#include <queue>
#include <stack>
#ifndef NULL
#define NULL 0
#endif
#ifndef MAX
#define MAX(x,y) ((x) > (y)? (x):(y))
#endif
#define stature(x) ((&x)? (x).height : -1) //空树高为-1
#define IsRoot(x) ((x).parent == NULL)
#define HasLChild(x) ((x).lchild != NULL)
#define HasRChild(x) ((x).rchild != NULL)
#define HasChild(x) (HasLChild(x) || HasRChild((x)))
#define HasBothChild(x) (HasLChild(x) && HasRChild((x)))
#define IsLeafNode(x) (!HasChild(x))
#define IsLChild(x) (IsRoot(x)? NULL : (HasLChild(x) ? (x) == (x).lchild : (x) == (x).rchild))
#define IsRChild(x) (IsRoot(x)? NULL : (HasRChild(x) ? (x) == (x).rchild : (x) == (x).rchild))
#define Brother(x) ((IsRoot(x)? NULL: (IsLChild((x)) ? (x).parent->rchild : (x).parent->lchild))
#define Uncle(x) (IsRoot(x)? NULL : Brother(*((x)->parent)))
template<typename T> struct BinNode
{
T data; //数值
int height;
BinNode<T>* parent;
BinNode<T>* lchild;
BinNode<T>* rchild;
BinNode(){
height = 0;
parent = NULL;
lchild = NULL;
rchild = NULL;
}
BinNode(T e, BinNode<T>* p = NULL, BinNode<T>* l = NULL, BinNode<T>* r = NULL, int h = NULL)
{
data = e;
parent = p;
lchild = l;
rchild = r;
height = h;
}
~BinNode(void){};
int size(BinNode<T>* node) //后代总数(包括本身)
{
if (IsLeafNode(*node)) return 1;
if (HasBothChild(*node))
return size(node->lchild) + size(node->rchild) + 1;
if (HasLChild(*node)) return size(node->lchild) + 1;
if (HasRChild(*node)) return size(node->rchild) + 1;
}
//操作符
bool operator == (BinNode<T>& node) { return data == node.data; }
bool operator != (BinNode<T>& node) { return data != node.data; }
//遍历------------------------------------------
//层次遍历(遍历以本节点开始的树)
template <class VST> void TraveLevel(VST visit)
{
queue<BinNode<T>*> que;
que.push(this);
while (que.empty() == false){
visit(*que.front());
if (HasLChild(*que.front())) que.push(que.front()->lchild);
if (HasRChild(*que.front())) que.push(que.front()->rchild);
que.pop();
}//while
}
//先序遍历(遍历以本节点开始的树)
template <class VST> void TravePre(VST& visit)
{
BinNode<T>* node_temp = this;
stack<BinNode<T>*> stk;
do {
if (HasRChild(node_temp)) push(node_temp->rchild);
visit(*node_temp);
node_temp = node_temp->lchild;
if (node_temp == NULL){
node_temp = stk.top();
stk.pop();
}
} while (!(node_temp == NULL && stk.empty()));
}
};
#pragma once
#include "BinNode.h"
template <class T> class BinTree
{
protected:
int _size;
BinNode<T>* _root; //根节点
virtual int UpdateHeight(BinNode<T>* x) //每一个节点的高度的更新
{
x->height = MAX(stature(*(x->lchild)), stature(*(x->rchild))) + 1;
return x->height;
}
void UpdateHeightAbove(BinNode<T>* x) //更新x以及以上节点的高度
{
while (x) {
UpdateHeight(x);
x = x->parent;
}
}
public:
BinTree() { _size = 0, _root = NULL; }
~BinTree(){};
//操作接口
bool empty() { return !_root; }
int size() const { return _size; }
inline BinNode<T>* RootPtr() const { return _root; }
BinNode<T>* InsertAsRoot(T const & e)
{
BinNode<T>* temp = new BinNode<T>(e,NULL,NULL,NULL,0);
if (empty())
{
_root = temp;
_size = 1;
}
else{
temp->data = _root->data;
temp->height = _root->height;
temp->lchild = _root->lchild;
temp->rchild = _root->rchild;
temp->parent = _root->parent;
_root = temp;
}
return _root;
}
BinNode<T>* InsertAsLNode(BinNode<T>* node, T const & e)
{
BinNode<T>* temp = new BinNode<T>(e, NULL, NULL, NULL, 0);
node->lchild = temp;
temp->parent = node;
UpdateHeightAbove(temp);
_size++;
return temp;
}
BinNode<T>* InsertAsRNode(BinNode<T>* node, T const & e)
{
BinNode<T>* temp = new BinNode<T>(e, NULL, NULL, NULL, 0);
node->rchild = temp;
temp->parent = node;
UpdateHeightAbove(temp);
_size++;
return temp;
}
int AttachAsLC(BinNode<T>* node, BinTree<T> & e)
{
.>RootPtr()->parent = node;
node->lchild = e.RootPtr();
UpdateHeightAbove(node);
_size += node->size();
return e.size();
}
int AttachAsRC(BinNode<T>* node, BinTree<T> & e)
{
e.RootPtr()->parent = node;
node->rchild = e.RootPtr();
UpdateHeightAbove(node);
_size += node->rchild->size(node->rchild);
return e.size();
}
BinTree<T>* Secede(BinNode<T>* p) //移出p以及其子树(返回删除节点作为根节点的树)
{
BinTree<T>* tree_temp = new BinTree<T>;
tree_temp->InsertAsRoot = p;
_size -= tree_temp->size();
UpdateHeightAbove(p->parent);
IsLChild(p) ? p->lchild = NULL : p->rchild = NULL;
return tree_temp;
}
//遍历接口
template <class VST> void TraveLevel(VST visi)
{
if(_root) _root->TraveLevel(visit);
}
template <class VST> void TravePre(VST& visit)
{
if (_root) _root->TravePre(visit);
}
};
相关文章推荐
- 用C++实现二叉树
- 用C++实现二叉树(转)
- 求二叉树中节点间的最大距离(c++代码完整实现)
- ACM 重构二叉树 C++实现
- C/C++面试题(三) 推断二叉树、快速排序递归实现、递归判断数组递增
- 【头文件】c++实现二叉树
- [置顶] 二叉树的建立、节点查找以及节点删除C和C++实现
- 二叉树的遍历(C++非递归实现)
- C++实现二叉树 前序遍历, 后序遍历, 中序遍历, 层序遍历(不用递归)
- C++ 二叉树的构建,先序/中序/后序的递归/非递归实现
- 二叉树的后序遍历非递归算法之C++实现 选择自 xuyongfeng 的 Blog
- 二叉树用顺序表实现 C++代码实现
- [C/C++] 先序建立二叉树| 先序、中序、后序遍历二叉树| 求二叉树深度、节点数、叶节点数 算法实现
- 简单链式二叉树(C++模版技术实现)
- C++ 二叉树的实现以及指针使用注意事项
- 数据结构复习:几种排序算法的C++实现和二叉树的相关算法实现
- 用C++实现二叉树
- C++对二叉树的简单实现。
- 数据结构_树_二叉树的线索化_C++实现
- C++ 二叉树的实现