您的位置:首页 > 编程语言 > C语言/C++

二叉树的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);

}

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: