在上篇二叉树中又增加了递归查找父结点和非递归查找父结点,还有增加二叉搜索树的插入和删除结点的算法
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
enum Tags
{
Left, Right
};
template<class T>
class StackElement;
template<class T>
class BinaryTree;
template<class T>
class BinaryTreeNode
{
friend class StackElement<T>;
friend class BinaryTree<T>;
T data;
BinaryTreeNode<T> *leftChild;
BinaryTreeNode<T> *rightChild;
};
template<class T>
class StackElement
{
public:
BinaryTreeNode<T>*pointer;
Tags tag;
};
template<class T>
class BinaryTree
{
BinaryTreeNode<T>*root;
public:
BinaryTree();
BinaryTreeNode<T>*&Root();
void CreatBinaryTree(BinaryTreeNode<T>*&rt);
void PreOrder(BinaryTreeNode<T>*rt);
void InOrder(BinaryTreeNode<T>*rt);
void PostOrder(BinaryTreeNode<T>*rt);
void PreOrderWithoutRecusion();
void InOrderWithoutRecusion();
void PostOrderWithoutRecusion();
void LevelOrder();
BinaryTreeNode<T>* Parent(BinaryTreeNode<T>*rt, T Value);
BinaryTreeNode<T>* ParentWithoutRecusion(T Value);
void insert(BinaryTreeNode<T>*&rt, T Value);
void remove(BinaryTreeNode<T>*&rt, T Value);
BinaryTreeNode<T>* deletemin(BinaryTreeNode<T>*&rt);
void OptionMenu()
{
cout << "请选择功能" << endl;
cout << "1:递归前序遍历" << endl;
cout << "2:递归中序遍历" << endl;
cout << "3:递归后续遍历" << endl;
cout << "4:非递归前续遍历" << endl;
cout << "5:非递归中续遍历" << endl;
cout << "6:非递归后续遍历" << endl;
cout << "7:层序遍历" << endl;
cout << "8:前序递归查找父结点" << endl;
cout<< "9:前序非递归查找父结点" << endl;
int n;
cin >> n;
T Value;
BinaryTreeNode<T>*Pointer;
switch (n)
{
case 1:
cout << "递归前序遍历:";
PreOrder(root);
break;
case 2:
cout << "递归中序遍历:";
InOrder(root);
break;
case 3:
cout << "递归后序遍历:";
PostOrder(root);
break;
case 4:
cout << "非递归前序遍历:";
PreOrderWithoutRecusion();
break;
case 5:
cout << "非递归中续遍历:";
InOrderWithoutRecusion();
break;
case 6:
cout << "非递归后续遍历:";
PostOrderWithoutRecusion();
break;
case 7:
cout << "层序遍历:" << endl;
LevelOrder();
cout << endl;
break;
case 8:
cout << "请输入待查找的结点" << endl;
cin >> Value;
Pointer = Parent(root, Value);
if (Pointer != NULL)
{
cout << Value << "的父节点为:" <<Pointer->data << endl;
}
else
{
cout << "没有找到" << Value << "的父结点" << endl;
}
break;
case 9:
cout << "请输入待查找的结点" << endl;
cin >> Value;
Pointer = ParentWithoutRecusion(Value);
if (Pointer != NULL)
{
cout << Value << "的父节点为:" << Pointer->data << endl;
}
else
{
cout << "没有找到" << Value << "的父结点" << endl;
}
break;
}
}
};
template<class T>
BinaryTree<T>::BinaryTree()
{
cout << "请输入根节点:" << endl;
cout << "输入#代表结点指针指向空" << endl;
CreatBinaryTree(root);
if (root != NULL)
cout << "root=" << root->data << endl;
else
cout << "The BinaryTree is empty." << endl;
}
template<class T>
BinaryTreeNode<T>*&BinaryTree<T>::Root()
{
return root;
}
template<class T>
void BinaryTree<T>::CreatBinaryTree(BinaryTreeNode<T>*&rt)
{
T ch;
cin >> ch;
if (ch == '#')
{
rt = NULL;
}
else
{
rt = new BinaryTreeNode<T>;
rt->data = ch;
cout << "调用左孩子" << endl;
CreatBinaryTree(rt->leftChild);
cout << "调用右孩子" << endl;
CreatBinaryTree(rt->rightChild);
}
}
template<class T>
void BinaryTree<T>::PreOrder(BinaryTreeNode<T>*rt)
{
if (rt != NULL)
{
cout << rt->data << " ";
PreOrder(rt->leftChild);
PreOrder(rt->rightChild);
}
}
template<class T>
void BinaryTree<T>::InOrder(BinaryTreeNode<T>*rt)
{
if (rt != NULL)
{
InOrder(rt->leftChild);
cout << rt->data << " ";
InOrder(rt->rightChild);
}
}
template<class T>
void BinaryTree<T>::PostOrder(BinaryTreeNode<T>*rt)
{
if (rt != NULL)
{
PostOrder(rt->leftChild);
PostOrder(rt->rightChild);
cout << rt->data << " ";
}
}
template<class T>
void BinaryTree<T>::PreOrderWithoutRecusion()
{
stack<BinaryTreeNode<T>*>aStack;
BinaryTreeNode<T>*pointer = root;
aStack.push(NULL);
while (pointer)
{
cout << pointer->data << " ";
if (pointer->rightChild != NULL)
aStack.push(pointer->rightChild);
if (pointer->leftChild != NULL)
pointer = pointer->leftChild;
else
{
pointer = aStack.top();
aStack.pop();
}
}
}
template<class T>
void BinaryTree<T>::InOrderWithoutRecusion()
{
stack<BinaryTreeNode<T>*>aStack;
BinaryTreeNode<T>*Pointer = root;
while (!aStack.empty()||Pointer)
{
if (Pointer)
{
aStack.push(Pointer);
Pointer = Pointer->leftChild;
}
else
{
Pointer = aStack.top();
aStack.pop();
cout << Pointer->data << " ";
Pointer = Pointer->rightChild;
}
}
}
template<class T>
void BinaryTree<T>::PostOrderWithoutRecusion()
{
StackElement<T> element;
stack<StackElement<T>>aStack;
BinaryTreeNode<T>*pointer;
pointer = root;
while (!aStack.empty()||pointer)
{
while (pointer != NULL)
{
element.pointer = pointer;
element.tag = Left;
aStack.push(element);
pointer = pointer->leftChild;
}
element = aStack.top();
aStack.pop();
pointer = element.pointer;
if (element.tag == Left)
{
element.tag = Right;
aStack.push(element);
pointer = pointer->rightChild;
}
else
{
cout << pointer->data << " ";
pointer = NULL;
}
}
}
template<class T>
void BinaryTree<T>::LevelOrder()
{
queue<BinaryTreeNode<T>*>aQueue;
BinaryTreeNode<T>*pointer = root;
aQueue.push(pointer);
while (!aQueue.empty())
{
pointer = aQueue.front();
aQueue.pop();
cout << pointer->data << " ";
if (pointer->leftChild != NULL)
{
aQueue.push(pointer->leftChild);
}
if (pointer->rightChild != NULL)
{
aQueue.push(pointer->rightChild);
}
}
}
template<class T>
BinaryTreeNode<T>* BinaryTree<T>::Parent(BinaryTreeNode<T>*rt, T Value)
{
BinaryTreeNode<T>*tmp;
if (rt == NULL)
return NULL;
if (rt->leftChild != NULL&&Value == rt->leftChild->data)
{
return rt;
}
if (rt->rightChild != NULL&&Value == rt->rightChild->data)
{
return rt;
}
if ((tmp = Parent(rt->leftChild, Value)) != NULL)
{
return tmp;
}
if ((tmp = Parent(rt->rightChild, Value)) != NULL)
{
return tmp;
}
return NULL;
}
template<class T>
BinaryTreeNode<T>* BinaryTree<T>::ParentWithoutRecusion(T Value)
{
BinaryTreeNode<T>* Pointer = root;
stack<BinaryTreeNode<T>*> aStack;
aStack.push(NULL);
while (Pointer)
{
if (Pointer->leftChild != NULL && Pointer->leftChild->data == Value)
return Pointer;
if (Pointer->rightChild != NULL && Pointer->rightChild->data == Value)
{
return Pointer;
}
if (Pointer->rightChild != NULL)
{
aStack.push(Pointer->rightChild);
}
if (Pointer->leftChild != NULL)
{
Pointer = Pointer->leftChild;
}
else
{
Pointer = aStack.top();
aStack.pop();
}
}
}
template<class T>
void BinaryTree<T>::insert(BinaryTreeNode<T>*&rt, T Value)
{
if (rt == NULL)
{
rt = new BinaryTreeNode<T>;
rt->data = Value;
}
else if (Value < rt->data)
insert(rt->leftChild, Value);
else if (Value > rt->data)
insert(rt->rightChild, Value);
}
template<class T>
void BinaryTree<T>::remove(BinaryTreeNode<T>*&rt, T Value)
{
if (rt == NULL)
cout << Value << " is not in the tree" << endl;
else if (Value < rt->data)
remove(rt->leftChild, Value);
else if (Value > rt->data)
remove(rt->rightChild, Value);
else
{
BinaryTreeNode<T>*temp = rt;
if (rt->leftChild == NULL)
rt = rt->rightChild;
else if (rt->rightChild == NULL)
rt = rt->leftChild;
else
{
temp = deletemin(rt->rightChild);
rt->data = temp->data;
}
}
}
template<class T>
BinaryTreeNode<T>* BinaryTree<T>::deletemin(BinaryTreeNode<T>*&rt)
{
if (rt->leftChild != NULL)
return deletemin(rt->leftChild);
else
{
BinaryTreeNode<T>*temp = rt;
rt = rt->rightChild;
return temp;
}
}
int main()
{
BinaryTree<char>a;
a.OptionMenu();
a.remove(a.Root(), 'G');
a.LevelOrder();
}