1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| #include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define EQ(a,b) (a==b) #define LT(a,b) (a<b) typedef int Status; typedef char TElemType; typedef int KeyType; typedef struct { KeyType key; }ElemType; typedef struct BiTNode { ElemType data; struct BiTNode *lchild, *rchild; }BiTNode,*BiTree;
Status InsertBST(BiTree &T,ElemType e) { if (T == NULL) { T = (BiTNode *)malloc(sizeof(BiTNode)); T->lchild = T->rchild = NULL; T->data = e; return 1; } else { if (e.key == T->data.key) return 0; else if (e.key < T->data.key) return InsertBST(T->lchild, e); else return InsertBST(T->rchild, e); } }
void PreOrderTraverse(BiTree T) { if (T) { cout << T->data.key << " "; if (T->lchild) PreOrderTraverse(T->lchild); if (T->rchild) PreOrderTraverse(T->rchild); } }
void InOrderTraverse(BiTree T) { if (T) { if (T->lchild) InOrderTraverse(T->lchild); cout << T->data.key << " "; if (T->rchild) InOrderTraverse(T->rchild); } }
void PostOrderTraverse(BiTree T) { if (T) { if (T->lchild) PostOrderTraverse(T->lchild); if (T->rchild) PostOrderTraverse(T->rchild); cout << T->data.key<<" "; } }
BiTree SearchBSTa(BiTree T, KeyType e) { if (T == NULL) return NULL; else { if (T->data.key == e) return T; else if (e < T->data.key) return SearchBSTa(T->lchild, e); else return SearchBSTa(T->rchild, e); } }
Status Delete(BiTree &T) { BiTNode *p=new BiTNode(); if (!T->rchild) { p = T; T = T->lchild; free(p); } else if (!T->lchild) { p = T; T = T->rchild; free(p); } else { p = T; BiTNode *s = new BiTNode(); s = T->lchild; while (s->rchild) { T = s; s = s->rchild; } T->data = s->data; if (p != T) p->rchild = s->lchild; else T->lchild = s->lchild; delete s; } return TRUE; }
Status DeleteBST(BiTree &T, KeyType key) { if (!T) return FALSE; else { if (EQ(key, T->data.key)) return Delete(T); else if (LT(key, T->data.key)) return DeleteBST(T->lchild, key); else return DeleteBST(T->rchild, key); } } void main() { BiTree T = NULL, s; int n, i; ElemType e; cout << "输入二叉排序树的结点数:"; cin >> n; cout << "输入" << n << "个结点的值:" << endl; for (i = 1; i <= n; ++i) { cin >> e.key; InsertBST(T, e); } cout << "先序遍历二叉排序树,结果是:" << endl; PreOrderTraverse(T); cout << endl; cout << "中序遍历二叉排序树,结果是:" << endl; InOrderTraverse(T); cout << endl; cout << "后序遍历二叉排序树,结果是:" << endl; PostOrderTraverse(T); cout << endl; cout << "输入要查找结点的值:\n"; cin >> e.key; s = SearchBSTa(T, e.key); if (s) { printf("已找到!结点的八进制地址为:%o\n", s); } else { printf("没找到"); } cout << "输入插入结点的值:"; cin >> e.key; InsertBST(T, e); cout << "先序遍历二叉排序树,结果是:" << endl; PreOrderTraverse(T); cout << endl; cout << "中序遍历二叉排序树,结果是:" << endl; InOrderTraverse(T); cout << endl; cout << "后序遍历二叉排序树,结果是:" << endl; PostOrderTraverse(T); cout << endl; cout << "输入要删除结点的值:" << endl; cin >> e.key; DeleteBST(T, e.key); cout << "先序遍历二叉排序树,结果是:" << endl; PreOrderTraverse(T); cout << endl; cout << "中序遍历二叉排序树,结果是:" << endl; InOrderTraverse(T); cout << endl; cout << "后序遍历二叉排序树,结果是:" << endl; PostOrderTraverse(T); cout << endl; system("pause"); }
|