您的位置:首页 > 理论基础 > 数据结构算法

数据结构之二叉树

2011-12-17 22:52 281 查看
该代码为二叉树的类模板,包括构造函数,拷贝构造函数,析构函数,二叉树的建立,二叉树的前序、中序、后序及按层遍历四种遍历方法,以及与其相对应的四游标。
//BinaryTree.h
#include<iostream>
#include<stack>
#include<queue>
#include<deque>
using namespace std;
template<class T>
class InorderIterator;
template<class T>
class BinaryTree;
template<class T>
class LevelorderIterator;
template<class T>
class StackItem;
template<class T>
class PostorderIterator;
template<class T>
class PreorderIterator;
template<class T>
class TreeNode
{
friend class StackItem<T>;
friend class PostorderIterator<T>;
friend class InorderIterator<T>;
friend class BinaryTree<T>;
friend class PreorderIterator<T>;
friend class LevelorderIterator<T>;
public:
TreeNode(T d,TreeNode<T>*l=NULL,TreeNode<T>*R=NULL):data(d),leftChild(NULL),rightChild(NULL){}
private:
T data;
TreeNode<T>*leftChild;
TreeNode<T>*rightChild;
};
template<class T>
class BinaryTree
{
friend class InorderIterator<T>;
friend class PostorderIterator<T>;
friend class PreorderIterator<T>;
friend class LevelorderIterator<T>;
public:
BinaryTree();
BinaryTree(const BinaryTree<T>&);
~BinaryTree();
TreeNode<T>*Copy(TreeNode<T>*origNode);
void Build()
{
Build(root,1);
}
void Build(TreeNode<T>*,int);
void Preorder(){Preorder(root);}
void Inorder(){Inorder(root);}
void Postorder(){Postorder(root);}
void Levelorder(){Levelorder(root);}
void Preorder(TreeNode<T>*);
void Inorder(TreeNode<T>*);
void Postorder(TreeNode<T>*);
void Levelorder(TreeNode<T>*);
void Visit(TreeNode<T>*);
void Delete(TreeNode<T>*);
private:
TreeNode<T>*root;

};
template<class T>
class InorderIterator
{
public:
T*Next();
InorderIterator(BinaryTree<T> tree):t(tree)
{
CurrentNode=t.root;
}
private:
BinaryTree<T> t;
stack<TreeNode<T>*> s;
TreeNode<T>*CurrentNode;
};
template<class T>
class StackItem
{
public:
bool RCVisited;
TreeNode<T>*p;
};
template<class T>
class PostorderIterator
{
public:
T*Next();
PostorderIterator(BinaryTree<T> tree):t(tree)
{
CurrentNode=t.root;
}
private:
stack<StackItem<T>*> s;
BinaryTree<T> t;
TreeNode<T>*CurrentNode;
};
template<class T>
class PreorderIterator
{
public:
T*Next();
PreorderIterator(BinaryTree<T> tree):t(tree)
{
CurrentNode=t.root;
}
private:
BinaryTree<T> t;
stack<TreeNode<T>*> s;
TreeNode<T>*CurrentNode;
};
template<class T>
class LevelorderIterator
{
public:
T*Next();
LevelorderIterator(BinaryTree<T> tree):t(tree)
{
CurrentNode=t.root;
}
private:
queue<TreeNode<T>*> q;
BinaryTree<T> t;
TreeNode<T>*CurrentNode;
};
//BinaryTree.cpp
#include"BinaryTree.h"
using namespace std;
template<class T>
BinaryTree<T>::BinaryTree()
{
root=NULL;
}
template<class T>
BinaryTree<T>::BinaryTree(const BinaryTree<T>& bt)
{
root=Copy(bt.root);
}
template<class T>
void BinaryTree<T>::Build(TreeNode<T>*currentNode,int k)
{
TreeNode<T>* point;
point=new TreeNode<T>(0);
cout<<"输入结点数据:";
cin>>point->data;
switch(k)
{
case 1:root=point;break;
case 2:currentNode->leftChild=point;break;
case 3:currentNode->rightChild=point;
}
char temp;
cout<<"该"<<point->data<<"结点是否有左子树 Y/任意键:";
cin>>temp;
if(temp=='y'||temp=='Y')
{
Build(point,2);

}
cout<<"该"<<point->data<<"结点是否有右子树 Y/任意键:";
cin>>temp;
if(temp=='y'||temp=='Y')
{
Build(point,3);
}

}
template<class T>
TreeNode<T>*BinaryTree<T>::Copy(TreeNode<T>*origNode)
{
if(!origNode)
return 0;
return new TreeNode<T>(origNode->data,Copy(origNode->leftChild),Copy(origNode->rightChild));
}
template<class T>
void BinaryTree<T>::Preorder(TreeNode<T>*currentNode)
{
if(currentNode)
{
Visit(currentNode);
Preorder(currentNode->leftChild);
Preorder(currentNode->rightChild);
}
}
template<class T>
void BinaryTree<T>::Inorder(TreeNode<T>*currentNode)
{
if(currentNode)
{
Inorder(currentNode->leftChild);
Visit(currentNode);
Inorder(currentNode->rightChild);
}
}
template<class T>
void BinaryTree<T>::Postorder(TreeNode<T>*currentNode)
{
if(currentNode)
{
Postorder(currentNode->leftChild);
Postorder(currentNode->rightChild);
Visit(currentNode);
}
}
template<class T>
void BinaryTree<T>::Levelorder(TreeNode<T>*currentNode)
{
queue<TreeNode<T>*> q;
while(currentNode)
{
Visit(currentNode);
if(currentNode->leftChild)
q.push(currentNode->leftChild);
if(currentNode->rightChild)
q.push(currentNode->rightChild);
if(q.empty())
return;
currentNode=q.front();
q.pop();

}
}
template<class T>
void BinaryTree<T>::Visit(TreeNode<T>*currentNode)
{
cout<<currentNode->data<<"   ";
}
template<class T>
void BinaryTree<T>::Delete(TreeNode<T>*currentNode)
{
if((currentNode->leftChild==NULL)&&(currentNode->rightChild==NULL))
{
delete currentNode;
}
else if((currentNode->leftChild!=NULL)&&(currentNode->rightChild!=NULL))
{
Delete(currentNode->leftChild);
Delete(currentNode->rightChild);
//delete currentNode;
}
else if((currentNode->leftChild==NULL)&&(currentNode->rightChild!=NULL))
{
Delete(currentNode->rightChild);
}
else
{
Delete(currentNode->leftChild);
}
}
template<class T>
BinaryTree<T>::~BinaryTree()
{
Delete(root);
}
template<class T>
T* InorderIterator<T>::Next()
{

while(CurrentNode)
{
s.push(CurrentNode);
CurrentNode=CurrentNode->leftChild;
}
if(s.empty())
return 0;
CurrentNode=s.top();
s.pop();
T&temp=CurrentNode->data;
CurrentNode=CurrentNode->rightChild;
return &temp;
}
template<class T>
T* PostorderIterator<T>::Next()
{
StackItem<T> *item;

while(1)
{
while(CurrentNode)
{
item->p=CurrentNode;
item->RCVisited=0;
s.push(item);
CurrentNode=CurrentNode->leftChild;

}
if(!s.empty())
{
item=s.top();
s.pop();
CurrentNode=item->p;
if(item->RCVisited=0)
{
item->RCVisited=1;
s.push(item);
CurrentNode=CurrentNode->rightChild;
}
else
{
CurrentNode=0;
return &(item->p->data);
}
}
}

}
template<class T>
T* PreorderIterator<T>::Next()
{

TreeNode<T>*temp;
while(CurrentNode)
{
s.push(CurrentNode);
temp=CurrentNode;
CurrentNode=CurrentNode->leftChild;
return &temp->data;
}
if(s.empty())
return 0;
CurrentNode=s.top();
s.pop();
CurrentNode=CurrentNode->rightChild;
}
template<class T>
T*LevelorderIterator<T>::Next()
{
TreeNode<T>*temp;
if(CurrentNode)
{
temp=CurrentNode;
if(CurrentNode->leftChild)
q.push(CurrentNode->leftChild);
if(CurrentNode->rightChild)
q.push(CurrentNode->rightChild);
if(q.empty)
return 0;
CurrentNode=q.front;
q.pop();
return &temp->data;
}

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