标签:clu empty 注意 str 后序遍历 tno sub 返回 order
1 数据结构的练习与巩固 2 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 3 #include<iostream> 4 using namespace std; 5 6 template <class T> 7 struct BTNode 8 { //二叉树结点类定义 9 T data; //数据域 10 BTNode<T> *lChild, *rChild; 11 //左子女、右子女链域 12 BTNode () //构造函数 13 { 14 lChild = NULL; 15 rChild = NULL; 16 } 17 18 BTNode (T& x, BTNode<T> *l = NULL, BTNode<T> *r = NULL) 19 { 20 data = x; 21 lChild = l; 22 rChild = r; 23 } 24 }; 25 26 //二叉树类定义 27 template <class T> 28 class BT 29 { 30 public: 31 BTNode<T> *root; //二叉树的根指针 32 T RefValue; //数据输入停止标志 33 public: 34 35 BT () : root (NULL) {} //构造函数 36 BT (T value) : RefValue(value), root(NULL) {} //构造函数 37 38 BTNode<T> *lChild (BTNode<T> *t) 39 { return (t != NULL)?t->lChild : NULL; } //返回左子女 40 BTNode<T> *rChild (BTNode<T> *t) 41 { return (t != NULL)?t->rChild : NULL; } //返回右子女 42 43 bool IsEmpty() { return root == NULL; } //判二叉树空否 44 BTNode<T> * & getRoot () { return root; } //取根 45 46 void PreOrder (BTNode<T>*& subTree); //前序遍历 47 void InOrder (BTNode<T>*& subTree); //中序遍历 48 void postOrder(BTNode<T>*& subTree); //后序遍历 49 50 void CreateBT(BTNode<T>* & subTree); 51
52 53 void Size (BTNode<T> * &subTree,int &count); //返回结点数 54 55 void leaf (BTNode<T> * &subTree,int &count); //返回叶子数 56 }; 57 58 template<class T> 59 void BT<T>::PreOrder(BTNode<T>* &subTree) 60 { 61 if (subTree != NULL) 62 { 63 cout << subTree->data <<‘ ‘; 64 PreOrder(subTree->lChild); 65 PreOrder(subTree->rChild); 66 } 67 } 68 69 template<class T> 70 void BT<T>::InOrder(BTNode<T>* &subTree) 71 { 72 if (subTree != NULL) 73 { 74 InOrder(subTree->lChild); 75 cout << subTree->data <<‘ ‘; 76 InOrder(subTree->rChild); 77 } 78 } 79 80 template<class T> 81 void BT<T>::postOrder(BTNode<T>*& subTree) 82 { 83 if (subTree != NULL) 84 { 85 postOrder(subTree->lChild); 86 postOrder(subTree->rChild); 87 cout << subTree->data <<‘ ‘; 88 } 89 } 90 91 //前序创建二叉树 92 template<class T> 93 void BT<T>::CreateBT(BTNode<T>*&subTree) //注意用引用接收指针 如果只传入指针 形参的指针指向改变 实参不会变 94 { 95 T ch; 96 cin >> ch; 97 if (ch == RefValue) subTree = NULL; 98 else 99 { 100 subTree = new BTNode<T>(ch); //有参构造函数 建立结点 101 CreateBT(subTree->lChild); //递归 102 CreateBT(subTree->rChild); 103 } 104 } 105 106 template<class T> 107 void BT<T>::Size (BTNode<T> * &subTree,int &count) 108 { 109 if (subTree != NULL) 110 { 111 count++; 112 Size(subTree->lChild,count); 113 Size(subTree->rChild,count); 114 } 115 else 116 return; 117 } 118 119 template<class T> 120 void BT<T>::leaf (BTNode<T> * &subTree,int &count) 121 { 122 if(subTree!=NULL) 123 { 124 if(subTree->lChild==NULL && subTree->rChild==NULL) 125 { 126 count++; 127 return; 128 } 129 else 130 { 131 leaf(subTree->lChild,count); 132 leaf(subTree->rChild,count); 133 } 134 } 135 } 136 137 int main() 138 { 139 int c1=0,c2=0; 140 BT<char> t(‘#‘); 141 t.CreateBT(t.getRoot()); 142 cout<<"前序遍历"<<endl; 143 t.PreOrder(t.getRoot()); 144 cout<<endl<<"中序遍历"<<endl; 145 t.InOrder(t.getRoot()); 146 cout<<endl<<"后序遍历"<<endl; 147 t.postOrder(t.getRoot()); 148 t.Size(t.getRoot(),c1); 149 cout<<endl<<"结点总个数:"<<c1<<endl; 150 t.leaf(t.getRoot(),c2); 151 cout<<"叶子结点总个数:"<<c2<<endl; 152 }
标签:clu empty 注意 str 后序遍历 tno sub 返回 order
原文地址:https://www.cnblogs.com/nagishiro/p/13236747.html