标签:
1 template <class T> 2 struct BinaryTreeNode 3 { 4 private: 5 T element; 6 BinaryTreeNode<T> *leftChild, *rightChild; 7 public: 8 BinaryTreeNode(); 9 BinaryTreeNode(const T& ele); 10 BinaryTreeNode(const T& ele, BinaryTreeNode<T> *left, BinaryTreeNode<T> *right); 11 ~BinaryTreeNode(); 12 bool isLeaf(); 13 BinaryTreeNode<T>& operator=(const BinaryTreeNode<T>& Node) 14 { 15 this = Node; 16 }; 17 18 }; 19 20 template<class T> 21 BinaryTreeNode<T>::BinaryTreeNode(const T& ele) 22 { 23 element = ele; 24 leftChild = rightChild = NULL; 25 } 26 27 template<class T> 28 BinaryTreeNode<T>::BinaryTreeNode(const T& ele,BinaryTreeNode<T> *left, BinaryTreeNode<T> *right) 29 { 30 element = ele; 31 leftChild = left; 32 rightChild = right; 33 } 34 35 template<class T> 36 bool BinaryTreeNode<T>::isLeaf() 37 { 38 if(leftChild == NULL && rightChild == NULL) 39 return true; 40 else 41 return false; 42 }
1 #include "BinaryTreeNode.h" 2 #include <stack> 3 #include <queue> 4 #include <istream> 5 6 using namespace std; 7 8 template <class T> 9 class BinaryTree 10 { 11 private: 12 BinaryTreeNode<T>* root; 13 public: 14 BinaryTree(){root = NULL}; 15 ~BinaryTree(); 16 bool isEmpty(){return (root)?true:false;} 17 //返回当前结点的父结点 18 BinaryTreeNode<T>* getParent(BinaryTreeNode<T>* root,BinaryTreeNode<T>* current); 19 BinaryTreeNode<T>* Parent(BinaryTreeNode<T>* current); 20 //返回当前结点的左兄弟结点 21 BinaryTreeNode<T>* leftSibling(BinaryTreeNode<T>* current); 22 //返回当前结点的右兄弟结点 23 BinaryTreeNode<T>* rightSibling(BinaryTreeNode<T>* current); 24 //构造二叉树 25 void CreatBinaryTree(const T& ele, BinaryTree<T>& left, BinaryTree<T>& right); 26 //删除二叉树 27 void DeleteTree(BinaryTreeNode<T>* root); 28 void PreOrder(BinaryTreeNode<T>* root); //递归前序遍历 29 void InOrder(BinaryTreeNode<T>* root); //递归中序遍历 30 void PostOrder(BinaryTreeNode<T>* root); //递归后序遍历 31 void PreOrderWithoutRec(BinaryTreeNode<T>* root); //非递归前序遍历 32 void InOrderWithoutRec(BinaryTreeNode<T>* root); //非递归中序遍历 33 void PostOrderWithoutRec(BinaryTreeNode<T>* root); //非递归后序遍历 34 void levelOrder(BinaryTreeNode<T>* root); //层次遍历 35 }; 36 //从根结点开始寻找某个结点的父结点 37 template <class T> 38 BinaryTreeNode<T>* BinaryTree<T>::getParent(BinaryTreeNode<T>* root,BinaryTreeNode<T>* current) 39 { 40 if (root == NULL) 41 return NULL; 42 if (root -> leftChild == current || root -> rightChild == current) 43 return root; 44 else if (getParent(root -> leftChild, current) != NULL) 45 return getParent(root -> leftChild, current); 46 else 47 return getParent(root ->rightChild, current); 48 } 49 //返回父结点 50 template<class T> 51 BinaryTreeNode<T>* BinaryTree<T>::Parent(BinaryTreeNode<T>* current) 52 { 53 if (current == NULL || current == root) 54 return NULL; 55 else 56 return getParent(root, current); 57 } 58 //返回左兄弟结点 59 template<class T> 60 BinaryTreeNode<T>* BinaryTree<T>::leftSibling(BinaryTreeNode<T>* current) 61 { 62 if (current == NULL || current == root || current == Parent(current) -> leftChild) 63 return NULL; 64 else 65 return Parent(current) -> leftChild; 66 } 67 //返回右兄弟结点 68 template<class T> 69 BinaryTreeNode<T>* BinaryTree<T>::rightSibling(BinaryTreeNode<T>* current) 70 { 71 if (current == NULL || current == root || current == Parent(current) ->rightChild) 72 return NULL; 73 else 74 return Parent(current) ->rightChild; 75 } 76 77 template <class T> 78 void BinaryTree<T>::CreatBinaryTree(const T& ele, BinaryTree<T>& left, BinaryTree<T>& right) 79 { 80 root ->element = ele; 81 root ->leftChild = left.root; 82 root ->rightChild = right.root; 83 } 84 //删除整个 二叉树 85 template <class T> 86 void BinaryTree<T>::DeleteTree(BinaryTreeNode<T>* root) 87 { 88 if (root ) 89 { 90 DeleteTree(root ->leftChild); 91 DeleteTree(root ->rightChild); 92 delete root; 93 } 94 } 95 96 template <class T> 97 void BinaryTree<T>::PreOrder(BinaryTreeNode<T>* root) 98 { 99 if(root) 100 { 101 visit(root); 102 PreOrder(root ->leftChild); 103 PreOrder(root ->rightChild); 104 } 105 } 106 107 template <class T> 108 void BinaryTree<T>::InOrder(BinaryTreeNode<T>* root) 109 { 110 if(root) 111 { 112 InOrder(root ->leftChild); 113 visit(root); 114 InOrder(root ->rightChild); 115 } 116 } 117 118 template <class T> 119 void BinaryTree<T>::PostOrder(BinaryTreeNode<T>* root) 120 { 121 if(root) 122 { 123 PostOrder(root ->leftChild); 124 PostOrder(root ->rightChild); 125 visit(root); 126 } 127 } 128 /* 129 非递归前序遍历: 130 遇到一个结点,就访问该结点,并把此结点推入栈中,然后下降去周游它的左子树; 131 周游完它的左子树后,从栈顶托出这个结点,并按照它的右链接指示的地址再去周游该结点的右子树结构。 132 */ 133 template <class T> 134 void BinaryTree<T>::PreOrderWithoutRec(BinaryTreeNode<T>* root) 135 { 136 BinaryTreeNode<T>* current; 137 stack<BinaryTreeNode<T>*> aStack; 138 aStack.push(NULL); 139 while (current) 140 { 141 visit(current); 142 if(current ->rightChild) 143 aStack.push(current ->rightChild); 144 if(current ->leftChild) 145 current = current ->leftChild; 146 else 147 { 148 current = aStack.top(); 149 aStack.pop(); 150 } 151 } 152 } 153 /* 154 非递归中序遍历: 155 遇到一个结点,就把它推入栈中,并去周游它的左子树 156 周游完左子树后,从栈顶托出这个结点并访问之,然后按照它的右链接指示的地址再去周游该结点的右子树。 157 158 */ 159 template <class T> 160 void BinaryTree<T>::InOrderWithoutRec(BinaryTreeNode<T>* root) 161 { 162 BinaryTreeNode<T>* current = root; 163 stack<BinaryTreeNode<T>*> aStack; 164 while (current || !aStack.empty()) 165 { 166 if (current) 167 { 168 aStack.push(current); 169 current = current ->leftChild; 170 } 171 else 172 { 173 current = aStack.top(); 174 visit(current); 175 current = current ->rightChild; 176 aStack.pop(); 177 } 178 } 179 } 180 /* 181 非递归后序遍历: 182 遇到一个结点,把它推入栈中,周游它的左子树 183 周游结束后,还不能马上访问处于栈顶的该结点,而是要再按照它的右链接结构指示的地址去周游该结点的右子树 184 周游遍右子树后才能从栈顶托出该结点并访问之 185 186 */ 187 template <class T> 188 void BinaryTree<T>::PostOrderWithoutRec(BinaryTreeNode<T>* root) 189 { 190 BinaryTreeNode<T> *p, *q; 191 stack<BinaryTreeNode<T>*> aStack; 192 p = root; 193 do 194 { 195 while (p) //沿左子树下降 196 { 197 aStack.push(p); 198 p = p ->leftChild; 199 } 200 q = NULL; 201 while (!aStack.empty()) 202 { 203 p = aStack.top(); 204 aStack.pop(); 205 if (p ->rightChild == q) 206 { 207 visit(p); 208 q = p; 209 } 210 else 211 { 212 aStack.push(p); 213 p = p ->rightChild; 214 break; 215 } 216 } 217 }while(!aStack.empty()) 218 } 219 220 template <class T> 221 void BinaryTree<T>::levelOrder(BinaryTreeNode<T>* root) 222 { 223 queue<BinaryTreeNode<T>*> aQueue; 224 BinaryTreeNode<T>* current = root; 225 if(root) 226 aQueue.push(root); 227 while(!aQueue.empty()) 228 { 229 current = aQueue.pop(); 230 visit (current); 231 if(current ->leftChild) 232 aQueue.push(current ->leftChild); 233 if(current ->rightChild) 234 aQueue.push(current ->rightChild); 235 } 236 }
标签:
原文地址:http://www.cnblogs.com/darrensun/p/4438322.html