这是去年的内容,之前放在github的一个被遗忘的reporsity里面,今天看到了就拿出来
#include<iostream>
#include<string>
using namespace std;
/*
question#2:The creation of BinaryTree
goal:
1.建立二叉树(通过先序序列作为输入)
2.实现中序遍历和层序遍历、元素查找
3.实现、解释
*/
/*先定义队列,以实现二叉树的层序遍历
使用链队列,以达到不预先定义队列大小的效果*/
template <typename Type>class LinkQueue;
template <typename Type>
class LinkQueueNode {
friend class LinkQueue<Type>;
public:
LinkQueueNode(Type &e, LinkQueueNode<Type>*p = NULL) :elem(e), next(p) {};
private:
Type elem;
LinkQueueNode<Type>*next;
};
template<typename Type>
class LinkQueue {
public:
LinkQueue() :front(NULL), rear(NULL) {};
~LinkQueue();
int IsEmpty()const { return front == NULL; };
void LinkQueueClear();
int LinkQueueLength()const;
Type GetFront();
void InQueue(Type&e);
Type OutQueue();
private:
LinkQueueNode<Type>*front, *rear;
};
template <typename Type>
LinkQueue<Type>::~LinkQueue() {
LinkQueueNode<Type>*p;
while (front != NULL) {
p = front;
front = front->next;
delete p;
}
};
template <typename Type>
void LinkQueue<Type>::InQueue(Type &e) {
if (front == NULL)front = rear = new LinkQueueNode<Type>(e, NULL);
else
rear = rear->next = new LinkQueueNode<Type>(e, NULL);
};
template <typename Type>
Type LinkQueue<Type>::OutQueue() {
if (IsEmpty())
{
cout << "链队列为空!" << endl;
exit(0);
}
LinkQueueNode<Type>*p = front;
Type e = p->elem;
front = front->next;
if (front == NULL)rear = NULL;
delete p;
return e;
};
template <typename Type>
Type LinkQueue<Type>::GetFront() {
if (IsEmpty()) {
cout << "链队列为空!" << endl;
exit(0);
}
else
return front->elem;
};
template <typename Type>
int LinkQueue<Type>::LinkQueueLength()const {
LinkQueueNode<Type>*p = front;
int i = 0;
while (p) {
i++;
p = p->next;
}
return i;
};
template<typename T>class BinaryTree;
template<typename T>
class BinaryTreeNode {//define the node of binary tree
friend class BinaryTree<T>;
//friend class BinarySearchTree<T>;
private:
T data;
BinaryTreeNode<T>* left;
BinaryTreeNode<T>* right;
public:
BinaryTreeNode();
BinaryTreeNode<T>(const T&ele) :data(ele) {};
BinaryTreeNode<T>(const T&ele, BinaryTreeNode<T>*l, BinaryTreeNode<T>*r) :data(ele), left(l), right(r) {};
~BinaryTreeNode() {};
T value()const { return data; }
BinaryTreeNode<T>& operator=(const BinaryTreeNode<T>&Node) { this = Node; };
BinaryTreeNode<T>*leftchild()const { return left; };
BinaryTreeNode<T>*rightchild()const { return right; };
void setLeftchild(BinaryTreeNode<T>*);
void setRightchild(BinaryTreeNode<T>*);
void setValue(const T&val);
bool isLeaf()const;
};
template<typename T>
class BinaryTree {//define the binary tree
protected:
BinaryTreeNode<T>*root;
public:
BinaryTree() {
root = NULL;
}
BinaryTree(BinaryTreeNode<T>*r) { root = r; }
//~BinaryTree() { DeleteBinaryTree(root); };
//bool isEmpty()const;
void visit(const T&data) { cout << data << ' '; };
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&data, BinaryTree<T>&leftTree, BinaryTree<T>&rightTree);
void CreateTree(BinaryTreeNode<T>*&r);
void DeleteBinaryTree(BinaryTreeNode<T>*root);
//void PreOrder(BinaryTreeNode<T>*root);
void InOrder(BinaryTreeNode<T>*root);
/*void PostOrder(BinaryTreeNode<T>*root);
void PreOrderWithoutRecusion(BinaryTreeNode<T>*root);
void InOrderWithoutRecusion(BinaryTreeNode<T>*root);
void PostOrderWithoutRecusion(BinaryTreeNode<T>*root);*/
void LevelOrder(BinaryTreeNode<T>*root);
bool Search(BinaryTreeNode<T>*root,T&data);
};
template<typename T>
/*利用用户输入的先序遍历序列来初始化二叉树*/
void BinaryTree<T>::CreateTree(BinaryTreeNode<T>*& r)
{
char ch;
cin >> ch;
if (ch == '#')r = NULL;
else {
r = new BinaryTreeNode<T>(ch);
CreateTree(r->left);
CreateTree(r->right);
}
};
template<typename T>
bool BinaryTree<T>::Search(BinaryTreeNode<T>*root,T &data)
{
/*前序遍历,递归进行元素的搜索*/
int flag = 0;
if (root == NULL)
return 0;
if (root->data == data)
{
flag = 1;
return flag;
}
flag=flag+Search(root->left, data);
flag=flag+Search(root->right, data);
return flag;
};
template<typename T>
void BinaryTree<T>::InOrder(BinaryTreeNode<T>*root)
{
/*二叉树的中序遍历*/
if (root == NULL)return;
InOrder(root->left);
visit(root->data);
InOrder(root->right);
};
template<typename T>
void BinaryTree<T>::LevelOrder(BinaryTreeNode<T>*root)
{
/*二叉树的层序遍历*/
LinkQueue<BinaryTreeNode<T>*>tQueue;//链队列,节点类型为二叉树节点指针类型
BinaryTreeNode<T>*pointer = root;
if (pointer)tQueue.InQueue(pointer);
while (!tQueue.IsEmpty()) {
pointer = tQueue.GetFront();
tQueue.OutQueue();
visit(pointer->value());
if (pointer->leftchild() != NULL)
tQueue.InQueue(pointer->left);
if (pointer->rightchild() != NULL)
tQueue.InQueue(pointer->right);
}
};
int main()
{
char te ='A';
BinaryTree<char> test;//注意,用模板来创建对象时不使用new语句!!!
//BinaryTreeNode<char>* temp;
//temp = test.Root();
test.CreateTree(test.Root());//建立二叉树(通过先序序列作为输入)
test.InOrder(test.Root());//中序遍历
test.LevelOrder(test.Root());//层序遍历
if (test.Search(test.Root(), te))
cout << "True" << endl;
else
cout << "False" << endl;
return 0;
}