标签:
1 #include "stdafx.h" 2 /* ZQU OJ 1377 二叉查找树 */ 3 #include <iostream> 4 #include <cstdlib> 5 #include <string> 6 #include <cstring> 7 #include <stack> 8 bool FLAG = false; 9 using namespace std; 10 typedef int key_type; 11 typedef struct SerachTree { 12 key_type key; 13 struct SerachTree *LChild, *RChild; 14 }SerachTree, *PNode; 15 16 void INSERT(key_type insert_num, PNode *root) { 17 //初始化一个临时节点 18 PNode tmp = (PNode)malloc(sizeof(SerachTree)); 19 tmp->key = insert_num; 20 tmp->LChild = tmp->RChild = NULL; 21 //当为空树时 22 if ((*root) == NULL) { 23 *root = tmp; 24 return; 25 } 26 //树非空 27 else { 28 if (insert_num < (*root)->key) //递归,以左儿子做新的根节点 29 INSERT(insert_num, &(*root)->LChild); 30 if (insert_num >(*root)->key) //递归,以右儿子做新的根节点 31 INSERT(insert_num, &(*root)->RChild); 32 } 33 return; 34 } 35 PNode FIND(key_type find_num, PNode point) { 36 //PNode res = NULL; 37 //cout << "point:" << endl; 38 // cout << point->key << ‘ ‘ << (point->LChild) << ‘ ‘ << point->RChild <<endl; 39 //point = (PNode)malloc(sizeof(SerachTree)); //不能再分配内存(传入一个地址,这个地址指向已有的结构体,不需要开新内存) 40 //cout << "@" << endl; 41 if (point == NULL) 42 return NULL; 43 if (find_num == point->key) { 44 //cout << "point->key=" << point->key << " point->LChild=" << point->LChild << " point->RChild=" << point->RChild <<endl; 45 return point; 46 } 47 else { 48 //往左边走 49 if (find_num < point->key && point->LChild != NULL) 50 FIND(find_num, point->LChild); 51 else if (find_num > point->key && point->RChild != NULL) 52 FIND(find_num, point->RChild); 53 else return NULL; 54 } 55 56 //return res; 57 } 58 //遍历二叉树,中序 59 void TRAVERSE(PNode N) { 60 //如果该树为空 61 //cout << "N->key=" << N->key <<endl; 62 //N = (PNode)malloc(sizeof(SerachTree) ); 63 // cout << N->LChild << ‘ ‘ << N->RChild << endl; 64 //FLAG = false; 65 if (N != NULL) { 66 //if(N->LChild != NULL) 67 FLAG = true; 68 TRAVERSE(N->LChild); 69 // else return; 70 cout << N->key << ‘ ‘; 71 //if(N->RChild != NULL) 72 TRAVERSE(N->RChild); 73 //else return; 74 } 75 else return; 76 } 77 PNode FindMin(PNode T) { 78 if (T == NULL) 79 return NULL; 80 if (T->LChild == NULL) 81 return T; 82 else 83 FindMin(T->LChild); 84 } 85 void DELETE(key_type delete_num, PNode *N) { 86 PNode TmpCell; 87 TmpCell = (PNode)malloc(sizeof(SerachTree)); 88 //空树 89 if ((*N) == NULL) 90 return; 91 else if (delete_num < (*N)->key) { 92 DELETE(delete_num, &(*N)->LChild); 93 } 94 else if (delete_num >(*N)->key) { 95 DELETE(delete_num, &(*N)->RChild); 96 } 97 //两个儿子 98 else if ((*N)->LChild && (*N)->RChild) { 99 TmpCell = FindMin((*N)->RChild); 100 (*N)->key = TmpCell->key; 101 DELETE((*N)->key, &(*N)->RChild); 102 } 103 //一个儿子||叶节点 104 else { 105 TmpCell = *N; 106 if ((*N)->LChild == NULL/* && (*N)->RChild != NULL*/) 107 *N = (*N)->RChild; 108 else if ((*N)->RChild == NULL/* && (*N)->LChild != NULL*/) 109 (*N) = (*N)->LChild; 110 //cout << TmpCell->key << ‘ ‘ << TmpCell->LChild << ‘ ‘ << TmpCell->RChild << endl; 111 //删除节点 112 //cout << "N =" << N << endl; 113 free(TmpCell); 114 //(*N )= NULL; 115 TmpCell = NULL; 116 //*N = NULL; 117 //N = NULL; 118 //cout << TmpCell->key << ‘ ‘ << TmpCell->LChild << ‘ ‘ << TmpCell->RChild << endl; 119 //cout << (*N)->key << ‘ ‘ << (*N)->LChild << ‘ ‘ << (*N)->RChild << endl; 120 return; 121 } 122 123 } 124 int main() 125 { 126 string command; 127 int command_num; 128 PNode Root; 129 Root = (SerachTree*)malloc(sizeof(SerachTree)); 130 Root = NULL; 131 //Root->LChild = Root->RChild = NULL; 132 while (cin >> command) { 133 //getchar(); 134 if (command != "traverse") { 135 //cout << ‘!‘; 136 cin >> command_num; 137 //cout << command << endl; 138 if (command == "insert") 139 INSERT(command_num, &Root); 140 //cout <<Root; 141 if (command == "find") { 142 string find_res[2] = { "yes", "no" }; 143 if (FIND(command_num, Root) != NULL) { 144 cout << find_res[0] << endl; 145 } 146 else cout << find_res[1] << endl; 147 } 148 if (command == "delete") { 149 DELETE(command_num, &Root); 150 } 151 } 152 else if (command == "traverse") { 153 //cout << command <<endl; 154 FLAG = false; 155 TRAVERSE(Root); 156 if (FLAG) 157 cout << endl; 158 } 159 } 160 }
标签:
原文地址:http://www.cnblogs.com/lwhone/p/4424323.html