直接上代码
1 #ifndef TREE_H 2 #define TREE_H 3 4 #include <iostream> 5 #include <malloc.h> 6 7 typedef int ElemType; 8 9 typedef struct Bintree 10 { 11 ElemType key; 12 struct Bintree* left; 13 struct Bintree* right; 14 }Bintree,*pBintree; 15 16 17 pBintree BT_Insert(ElemType target,pBintree* ppTree); 18 void BT_InOrder(pBintree root); 19 void BT_InOrderNoRec(pBintree root); 20 void BT_PreOrder(pBintree root); 21 void BT_PreOrderNoRec(pBintree root); 22 void BT_PostOrder(pBintree root); 23 void BT_PostOrderNoRec(pBintree root); 24 void BT_LevelOrder(pBintree root); 25 26 void BT_Destory(pBintree root); 27 #endif
基本函数实现
1 /********************************** 2 对二叉树的各种操作 3 BY wei 2012-09-09 4 5 ***********************************/ 6 7 #include <queue> 8 #include <stack> 9 #include "bintree.h" 10 using namespace std; 11 12 // 访问 13 static void visit(pBintree root) 14 { 15 if(root != NULL) 16 { 17 printf("%d\n",root->key); 18 } 19 } 20 21 //生成一个新的节点 22 static pBintree BT_MakeNode(ElemType target) 23 { 24 pBintree pNode = (pBintree)malloc(sizeof(Bintree)); 25 if(pNode == NULL) 26 { 27 printf("malloc failed"); 28 return NULL; 29 } 30 pNode->key = target; 31 pNode->left = NULL; 32 pNode->right = NULL; 33 return pNode; 34 } 35 36 //插入 37 pBintree BT_Insert(ElemType target,pBintree* ppTree) 38 { 39 pBintree Node; 40 if(ppTree==NULL) 41 { 42 printf("failed\n"); 43 return NULL; 44 } 45 Node = *ppTree; 46 if(Node == NULL) 47 { 48 printf("--**%d\n",target); 49 return *ppTree = BT_MakeNode(target); 50 } 51 if(Node->key == target) //不允许出现相同的元素 52 { 53 return NULL; 54 } 55 else if(Node->key > target) 56 { 57 //printf("--left:%d\n",target); 58 return BT_Insert(target,&Node->left); 59 } 60 else 61 { 62 //printf("----right:%d\n",target); 63 return BT_Insert(target, &Node->right); 64 } 65 66 } 67 68 //中序遍历的递归实现 69 void BT_InOrder(pBintree root) 70 { 71 if(root != NULL) 72 { 73 BT_InOrder(root->left); 74 visit(root); 75 BT_InOrder(root->right); 76 } 77 } 78 //中序遍历的非递归实现 79 void BT_InOrderNoRec(pBintree root) 80 { 81 stack<pBintree> s; 82 83 while((root != NULL) || (!s.empty()) ) 84 { 85 if(root != NULL) 86 { 87 s.push(root); 88 root = root->left; 89 } 90 else 91 { 92 root = s.top(); 93 visit(root); 94 s.pop(); 95 root = root->right; 96 } 97 } 98 } 99 100 //前序遍历的递归实现 101 void BT_PreOrder(pBintree root) 102 { 103 if(root != NULL) 104 { 105 visit(root); 106 BT_PreOrder(root->left); 107 BT_PreOrder(root->right); 108 } 109 } 110 //前序遍历的非递归实现 111 void BT_PreOrderNoRec(pBintree root) 112 { 113 stack<pBintree> s; 114 115 while((root != NULL) || (!s.empty()) ) 116 { 117 if(root != NULL) 118 { 119 visit(root); 120 s.push(root); 121 root = root->left; 122 } 123 else 124 { 125 root = s.top(); 126 s.pop(); 127 root = root->right; 128 } 129 } 130 } 131 132 133 134 //后序遍历的递归实现 135 void BT_PostOrder(pBintree root) 136 { 137 if(root != NULL) 138 { 139 BT_PostOrder(root->left); 140 BT_PostOrder(root->right); 141 visit(root); 142 } 143 } 144 //后序遍历的非递归实现 145 void BT_PostOrderNoRec(pBintree root) 146 { 147 stack<pBintree> s; 148 pBintree pre = NULL; 149 pBintree top = NULL; 150 while((root != NULL) || (!s.empty())) 151 { 152 if(root != NULL) 153 { 154 s.push(root); 155 root = root->left; 156 } 157 else 158 { 159 top = s.top(); 160 if(top->right != NULL && top->right != pre) 161 root = top->right; 162 else 163 { 164 visit(top); 165 pre = top; 166 s.pop(); 167 } 168 } 169 } 170 } 171 172 //层次遍历的实现 173 void BT_LevelOrder(pBintree root) 174 { 175 queue<pBintree> q; 176 pBintree ptree; 177 178 if(root==NULL) return; 179 180 q.push(root); 181 182 while(!q.empty()) 183 { 184 ptree = q.front(); 185 q.pop(); 186 visit(ptree); 187 if(ptree->left != NULL) 188 { 189 q.push(ptree->left); 190 } 191 if(ptree->right != NULL) 192 { 193 q.push(ptree->right); 194 } 195 } 196 } 197 198 void BT_Destory(pBintree root) 199 { 200 stack<pBintree> s; 201 pBintree top = NULL; 202 203 while((root != NULL) || (!s.empty()) ) 204 { 205 if(root != NULL) 206 { 207 s.push(root); 208 root = root->left; 209 } 210 else 211 { 212 top = root = s.top(); 213 s.pop(); 214 root = root->right; 215 if(top != NULL){ free(top);top=NULL;} 216 } 217 } 218 }
main函数
1 #include <iostream> 2 #include <time.h> 3 #include "bintree.h" 4 5 #define MAX_CNT 10 6 #define BASE 100 7 8 int main() 9 { 10 int i; 11 pBintree root = NULL; 12 13 srand((unsigned)time(NULL)); 14 15 for(i=0; i<MAX_CNT; i++) 16 { 17 BT_Insert(rand()%BASE,&root); 18 } 19 20 //前序 21 printf("PreOrder\n"); 22 BT_PreOrder(root); 23 24 printf("\n"); 25 BT_PreOrderNoRec(root); 26 printf("---------------------\n"); 27 28 29 //中序 30 printf("InOrder:\n"); 31 BT_InOrder(root); 32 printf("\n"); 33 BT_InOrderNoRec(root); 34 printf("_____________________\n"); 35 36 //后序 37 printf("PostOrder\n"); 38 BT_PostOrder(root); 39 printf("\n"); 40 BT_PostOrderNoRec(root); 41 printf("======================\n"); 42 43 //层次 44 printf("BY LEVELOrder\n"); 45 BT_LevelOrder(root); 46 printf("\n"); 47 48 49 //destory 50 BT_Destory(root); 51 return 0; 52 }
参考http://www.cppblog.com/ngaut/archive/2011/10/19/2351.html
cppblog上的东西转不过来,自己把代码敲了一遍
低调做人,高调做事
浙公网安备 33010602011771号