链式二叉树

来源:互联网 发布:济宁网络教育报名 编辑:程序博客网 时间:2024/06/05 08:20
清华数据结构 殷人昆版本的代码。 传到博客上,作为备份。
底下实现了绝大部分的代码(部分使用了文件操作)
#include<iostream>#include<stdlib.h>#include<algorithm>#include <fstream>using namespace std;template<class T>struct BinTreeNode{T data;BinTreeNode<T> *leftChild, *rightChild;BinTreeNode() :leftChild(NULL), rightChild(NULL){}BinTreeNode(T x, BinTreeNode<T>*l = NULL, BinTreeNode<T> *r = NULL) :data(x), leftChild(l), rightChild(r){}};template<class T>class BinaryTree{public:BinaryTree() :root(NULL){}BinaryTree(T value) :RefValue(value), root(NULL){}BinaryTree(BinaryTree<T>&s);~BinaryTree(){ destroy(root); }bool IsEmpty() { return (root == NULL) ? true : false; }//BinTreeNode<T> *Parent(BinTreeNode<T>*current){ return (root == NULL) || (root == current) ? NULL : Parent(root, current); }BinTreeNode<T> *LeftChild(BinTreeNode<T>*current){ return (current != NULL) ? current->leftChild : NULL; }BinTreeNode<T> *RightChild(BinTreeNode<T>*current){ return (current != NULL) ? current->rightChild : NULL; }int Height(){ return Height(root); }int Size() { return Size(root); }BinTreeNode<T>  *getRoot()const { return root; }//void preOrder(void(*visit)(BinTreeNode<T>*p)){ preOrder(root, visit); }//void inOrder(void(*visit)(BinTreeNode<T>*p)){ inOrder(root, visit); }//void postOrder(void(*visit)(BinTreeNode<T>*p)){ postOrder(root, visit); }void inOrder(BinTreeNode<T> *node );void postOrder(BinTreeNode<T> *node );void preOrder(BinTreeNode<T> *node);void levelOrder(void(*visit)(BinTreeNode<T>*P));//int Insert(const T& item);bool Insert(BinTreeNode<T> *&subTree, const T &x,int d);//BinTreeNode<T> * Find(BinTreeNode<T> *subTree, T& x)const;BinTreeNode<T> *Find(T &item) const;void CreateBinTree(ifstream &in, BinTreeNode<T> *&subTree);void Visit(BinTreeNode<T> &B);int NodeNum(BinTreeNode<T> *node);//protected:BinTreeNode<T> *root;T RefValue;//void CreateBinTree(ifstream &in, BinTreeNode<T> *&subTree);//bool Insert(BinTreeNode<T> *&subTree, const T &x);void destroy(BinTreeNode<T> *&subTree);//BinTreeNode<T> Find(BinTreeNode<T> *subTree,  T &x)const;BinTreeNode<T> *Copy(BinTreeNode<T>*orignNode);int Height(BinTreeNode<T> *subTree );int Size(BinTreeNode<T> *subTree);void Parent(BinTreeNode<T>*subTree, T x);//BinTreeNode<T>*Parent(BinTreeNode<T> *subTree, BinTreeNode<T>*current);//bool Find(BinTreeNode<T> *subTree, const T&x) const;BinTreeNode<T> * Find(BinTreeNode<T> *subTree, T& x)const;void Traverse(BinTreeNode<T> *subTree, ostream &out);//void preOrder(BinTreeNode<T> &subTree, void(*visit)(BinTreeNode<T> *p));//void inOrder(BinTreeNode<T> &subTree, void(*visit)(BinTreeNode<T> *p));//void postOrder(BinTreeNode<T> &subTree, void(*visit)(BinTreeNode<T> *p));friend istream &operator >>(istream &in, BinaryTree<T>&Tree);friend ostream &operator<<(ostream &os, BinaryTree<T>&Tree);};template<class T>void BinaryTree<T>::destroy(BinTreeNode<T>*&subTree){if (subTree != NULL){destroy(subTree->leftChild);destroy(subTree->rightChild);delete subTree;}} template<class T>//确保子树不是NULL,才能输出data,否则出错.void BinaryTree<T>::Parent(BinTreeNode<T>*subTree, T x){if (subTree == NULL) { cout << "subTree is  NULL" << endl; return; }while (subTree->leftChild != NULL || subTree->rightChild != NULL){if (NULL != subTree->leftChild && subTree->leftChild->data == x){ cout << "The parent is:" << subTree->data << endl; break; }else if (NULL != subTree->rightChild && subTree->rightChild->data == x) { cout << "The parent is:" << subTree->data << endl; break;}else{if (NULL!=subTree->leftChild)Parent(subTree->leftChild, x);if (NULL != subTree->rightChild)Parent(subTree->rightChild, x);break;}} }template<class T>void BinaryTree<T>::Traverse(BinTreeNode<T> *subTree, ostream &out){if (subTree!= NULL){out << subTree->data << " ";Traverse(subTree->leftChild, out);Traverse(subTree->rightChild, out);}}template<class T>istream &operator>> (ifstream &in, BinaryTree<T> &Tree){CreateBinTree(in, Tree.root);return in;}template<class T>ostream&operator <<(ostream &out, BinaryTree<T> &Tree){cout << "The preorder of BinaryTree." << endl;Tree.Traverse(Tree.root, out);cout << endl;return out;} template<class T>BinTreeNode<T> * BinaryTree<T>::Find(BinTreeNode<T> *subTree, T& x)const //查找  {if (subTree == NULL)return NULL;if (subTree->data == x)//找到  {return subTree;}BinTreeNode<T> *temp;if ((temp = Find(subTree->leftChild, x)) != NULL)//若当前不相等 则递归下去  return temp;elsereturn Find(subTree->rightChild, x);}template<class T>BinTreeNode<T> * BinaryTree<T>::Copy(BinTreeNode<T> *orignNode)//复制 返回根结点指针参数(复制 结点)  {if (orignNode == NULL)return NULL;//如果当前指针为空则不执行后面的复制语句  BinTreeNode<T> *temp = new BinTreeNode<T>;//创立一个新的结点  temp->data = orignNode->data;//复制数据域  temp->leftChild = Copy(orignNode->leftChild);temp->rightChild = Copy(orignNode->rightChild);return temp;//最后的效果是 返回根结点指针  }template<class T>bool BinaryTree<T>::Insert(BinTreeNode<T> *&subTree, const T &x,int d){if (subTree == NULL) return false;else if (d % 2 == 0){//d是偶数则插入右子树BinTreeNode <T> *Child = new BinTreeNode<T>(x); //Child->data = x;if (subTree->rightChild == NULL){  subTree->rightChild=Child;   } }else if (subTree->leftChild == NULL)//否则插入左子树{BinTreeNode <T> *Child = new BinTreeNode<T>(x);subTree->leftChild = Child;}return true;}template<class T>void BinaryTree<T>::CreateBinTree(ifstream &in, BinTreeNode<T> *&subTree){T item;if (!in.eof()){in >> item;if (item != RefValue  ){  subTree = new BinTreeNode<T>(item);if (subTree == NULL){ cout << "error!" << endl; } CreateBinTree(in, subTree->leftChild);CreateBinTree(in, subTree->rightChild);}else subTree = NULL;}} template<class T>int BinaryTree<T>::Height(BinTreeNode<T> *subTree ){//有一个子树就+1,返回有最多子树的分支就是高。 if (subTree == NULL)//计算书本给的数据时,请注意在函数中我们并没有给左右高赋初始值,return 0;                //但是为什么还是能返回正确的高度。因为一旦遇到空节点 就return 0,在0的基础上回溯一次+1,就能得到正确结果; else{ int h2 = 0; int  h1 = 0;   h1 = Height(subTree->rightChild);   h2=Height(subTree->leftChild); return (h2 >= h1) ? h2 + 1 : h1 + 1;}} template<class T>void BinaryTree<T>::preOrder(BinTreeNode<T>* node) {if (NULL == node) return;cout << node->data << " ";preOrder(node ->leftChild);preOrder(node->rightChild);}template<class T>void BinaryTree<T>::inOrder(BinTreeNode<T>* node) {if (NULL == node) return;inOrder(node->leftChild);cout << node->data << " ";inOrder(node->rightChild);}template<class T>void BinaryTree<T>::postOrder(BinTreeNode<T>* node) {if (NULL == node) return;postOrder(node->leftChild);postOrder(node->rightChild);cout << node->data << " ";}template<class T>int BinaryTree<T>::NodeNum(BinTreeNode<T> *node){if (node == NULL)return 0;else return 1 + NodeNum(node->leftChild) + NodeNum(node->rightChild);}
<pre name="code" class="cpp">#include"BinaryTree.h" #include<string>int main(){BinaryTree<char>A('#');//确定隔断符号是#BinTreeNode<char> *B= new BinTreeNode<char>; ifstream result_2("result_2.txt");//在该cpp默认目录下创建文件cout << "Create binarytree and output it; ";A.CreateBinTree(result_2, B);//读取result_2,写入B A.inOrder(B);//中 前 后序输出cout << endl;A.preOrder(B);cout << endl;A.postOrder(B);cout << endl;/*测试节点数函数*/cout << "The Node num of A is:" << A.NodeNum(B) << endl;/*测试父节点函数*/char x = 'G'; A.Parent(B,x);//G的父节点是E/*两种方式输出左右子树的地址*/cout << "<2>Two ways to output children's address"<<endl;cout << A.LeftChild(B) << " ";  cout << B->leftChild << endl;cout << A.RightChild(B) <<" " ; cout << B->rightChild << endl;cout << "The height of this tree is:" << A.Height(B) << endl;/*插入节点H*/char y = 'H';A.Insert(B, y, 2);// 往<根节点>插入右子树,赋值data='H';  第三个参数输入的是偶数:代表将数据插入到右子树中(奇数代表插入到左子树)cout << "After insert H." << endl;A.inOrder(B);/*前序,后序输出*/cout << endl;A.preOrder(B);cout << endl;A.postOrder(B); cout << endl; char t = 'E';cout <<"The address of E is:"<<A.Find(B, t);ofstream result("result.txt"); A.Traverse(B, result);//把B写入result.txt中 system("pause");return 0;} 



0 0
原创粉丝点击