template <class T>
class BinaryTreeNode
{
friend class BinaryTree<T>;
private:
T info;
public:
BinaryTreeNode<T> *leftchild() const;
BinaryTreeNode<T> *rightchild() const;
BinaryTreeNode();
BinaryTreeNode(const T &ele); // 声明二叉树类为友元类 // 二叉树结点数据域
// 缺省构造函数 // 给定数据的构造
BinaryTreeNode(const T &ele, BinaryTreeNode<T> *l, BinaryTreeNode<T> *r); // 子树构造结点
T value() const;
// 返回当前结点数据 // 返回左子树 // 返回右子树
void setLeftchild(BinaryTreeNode<T> *); // 设置左子树
void setRightchild(BinaryTreeNode<T> *); // 设置右子树
void setValue(const T &val);
bool isLeaf() const;
// 设置数据域 // 判断是否为叶结点
BinaryTreeNode<T> &operator=(const BinaryTreeNode<T> &Node);
};
template <class T>
class BinaryTree
{
private:
BinaryTreeNode<T> *root;
public:
BinaryTree() { root = NULL; };
~BinaryTree() { DeleteBinaryTree(root); };
bool isEmpty() const;
//二叉树根结点
//构造函数 //析构函数
//判定二叉树是否为空树 BinaryTreeNode<T>* Root() {return root;}; //返回根结点
BinaryTreeNode<T> *Parent(BinaryTreeNode<T> *current); //返回父
BinaryTreeNode<T> *LeftSibling(BinaryTreeNode<T> *current); //左兄
BinaryTreeNode<T> *RightSibling(BinaryTreeNode<T> *current); //右兄
void CreateTree(const T &info, BinaryTree<T> &leftTree, BinaryTree<T> &rightTree); // 构造树
void PreOrder(BinaryTreeNode<T> *root); // 前序遍历二叉树
void InOrder(BinaryTreeNode<T> *root);
// 中序遍历二叉树
void PostOrder(BinaryTreeNode<T> *root); // 后序遍历二叉树
void LevelOrder(BinaryTreeNode<T> *root); // 按层次遍历二叉树
void DeleteBinaryTree(BinaryTreeNode<T> *root); // 删除二叉树
};
enum Tags
{
Left,
Right
};
template <class T>
class StackElement
{
public:
BinaryTreeNode<T> *pointer;
Tags tag;
};
template <class T>
void BinaryTree<T>::PostOrderWithoutRecursion(BinaryTreeNode<T> *root)
{
using std::stack;
// 使用STL的栈
StackElement<T> element;
stack<StackElement<T>> aStack;
BinaryTreeNode<T> *pointer;
pointer = root;
while (!aStack.empty() || pointer)
{
if (pointer != NULL)
{
// 沿非空指针压栈,并左路下降
element.pointer = pointer;
element.tag = Left;
aStack.push(element);
pointer = pointer->leftchild();
}
else
{
element = aStack.pop();
pointer = element.pointer;
if (element.tag == Left)
{
// 获得栈顶元素,并退栈 // 如果从左子树回来
element.tag = Right;
aStack.push(element); //置标志位为Right pointer = pointer->rightchild();
}
else
{
Visit(pointer);
pointer = NULL;
}
}
} //end while
}
void BinaryTree<T>::DeleteBinaryTree(BinaryTreeNode<T> *root)
{
if (root != NULL)
{
DeleteBinaryTree(root->left);
DeleteBinaryTree(root->right);
delete root;
//递归删除左子树 //递归删除右子树 // 删除根结点
}
}
template <class T>
void BinarySearchTree<T>::InsertNode(BinaryTreeNode<T> *root, *newpointer)
{
//向二叉搜索树插入新结点newpointer
BinaryTreeNode<T> *pointer;
if (root == NULL)
{ //用指针newpointer初始化二叉搜索树树根,赋值实现
Initialize(newpointer);
return;
}
else
pointer = root;
while (1)
{
if (newpointer->value() == pointer->value())
return; //=则不处理
else if (newpointer->value() < pointer->value())
{ //左子树
if (pointer->leftchild() == NULL)
{
//作为左孩子
pointer->left = newpointer;
return;
}
else
pointer = pointer->leftchild();
}
else
{ //作为右子树
if (pointer->rightchild() == NULL)
{ //右孩子空,则作为右孩子
pointer->right = newpointer;
return;
}
else
pointer = pointer->rightchild();
}
}
}
template <class T>
void BinarySearchTree<T>::DeleteNodeEx(BinaryTreeNode<T> *delpointer)
{
BinaryTreeNode<T> *replpointer;
BinaryTreeNode<T> *replparent = NULL; //替换结点 //替换结点的父结点
BinaryTreeNode<T> *delparent = Parent(delpointer); //待删结点父结点
//若待删除结点的左子树为空,就将其右子树代替它
if (delpointer->leftchild() == NULL)
replpointer = delpointer->rightchild();
else
{
replpointer = delpointer->leftchild();
while (replpointer->rightchild() != NULL)
{
replparent = replpointer;
replpointer = replpointer->rightchild();
} //替换结点就是被删结点的左子结点, 左子树挂接为其父(被删)的左子树
if (replparent == NULL)
delpointer->left = replpointer->leftchild();
// 替换结点的左子树挂接为其父的右子树 // 暂时
else
replparent->right = replpointer->leftchild();
replpointer->left = delpointer->leftchild(); //继承待删结点左子树
replpointer->right = delpointer->rightchild(); //继承待删结点右子树
}
// 用替换结点去替代真正的删除结点
if (delparent == NULL)
root = replpointer;
else if (delparent->leftchild() == delpointer)
delparent->left = replpointer;
else
delparent->right = replpointer;
delete delpointer;
delpointer = NULL;
return;
}
template <class T>
class BinaryTreeNode