/*Binary Search Tree 节点查找操作*/ Status SearchBST(BinaryTree btree, int key, BinaryTree &f, BinaryTree &p) { if (!btree) { p = f;//指针回溯 return ERROR; } else { if (btree->key == key) { p = btree; return OK; } else if (btree->key > key) { return SearchBST(btree->lchild, key, btree, p);//要有返回值,递归 } else { return SearchBST(btree->rchild, key, btree, p);//要有返回值,递归 } //return OK; } }
/*主函数*/ int main() { int n; int data; int key; BinaryTree btree = NULL; BinaryTree f = NULL, p = NULL; scanf("%d", &n); //btree = (BinaryTree)malloc(sizeof(BinaryTree)*n); for (int i = 1; i <= n; i++) { scanf("%d", &data); InsertBST(btree, data); } preOrder(btree, printNode);//前序遍历递归实现 printf("\n"); InOrder(btree, printNode);//中序遍历递归实现 printf("\n"); PostOrder(btree, printNode);//后序遍历递归实现 printf("\n"); scanf("%d", &key); printf("%d\n", SearchBST(btree, key, f, p));//查找操作 scanf("%d", &key); printf("%d\n", SearchBST(btree, key, f, p));//查找操作 scanf("%d", &data); InsertBST(btree, data); preOrder(btree, printNode);//前序遍历递归实现 printf("\n"); InOrder(btree, printNode);//中序遍历递归实现 printf("\n"); PostOrder(btree, printNode);//后序遍历递归实现 printf("\n"); InOrderSecond(btree);//中序遍历非递归实现 LevelOrder(btree);//层次遍历 return 0; }
BSTNode *SearchBST(BSTNode *bt,KeyType k) { if (bt==NULL || bt->key==k) /*递归终结条件*/ return bt; if (k<bt->key) return SearchBST(bt->lchild,k); /*在左子树中递归查找*/ else return SearchBST(bt->rchild,k); /*在右子树中递归查找*/ }
BSTree SearchBST(BSTree T, ElemType *e) { if (T == NULL || T->data == *e){ return T; } else if (*e < T->data){ return SearchBST(T->lchild, e); } else { return SearchBST(T->rchild, e); } }
Status SearchBST(BiTree T,int key,BiTree f,BiTree *p){ if(!T){ *p=f; return FALSE; } else if(key==T->data){ *p=T; return TRUE; } else if(key<T->data)return SearchBST(T->lchild,key,T,p); else return SearchBST(T->right,key,T,p); }
Status SearchBST(BiTree T, KeyType key, BiTree f, BiTree &p) { // 算法9.5(b) // 在根指针T所指二叉排序树中递归地查找其关键字等于key的数据元素, // 若查找成功,则指针p指向该数据元素结点,并返回TRUE, // 否则指针p指向查找路径上访问的最后一个结点并返回FALSE, // 指针f指向T的双亲,其初始调用值为NULL if (!T) { p = f; return FALSE; } // 查找不成功 else if (EQ(key, T->data.key)) { p = T; return TRUE; } // 查找成功 else if (LT(key, T->data.key)) return SearchBST(T->lchild, key, T, p); // 在左子树中继续查找 else return SearchBST(T->rchild, key, T, p); // 在右子树中继续查找 } // SearchBST
BSTree *SearchBST(BSTree* t, int key) { if (!t || t->data == key) { return t; } else if (key < t->data) { return SearchBST(t->left, key); } else { return SearchBST(t->right, key); } }
//二叉排序树:左子树上所有结点的值均小于它的根节点的值,右子树上所有结点的值均大于根节点的值 //查找 Status SearchBST(BiTree T,KeyType key,BiTree f,BiTree &p) //在二叉排序树T中查找key。若查找成功,则p指向该数据元素结点,并返回TRUE。否则,p指向查找路径上访问的最后一个结点并返回FALSE。f为T节点的双亲。 { if(!T) { p=f; return FALSE; } else if (EQ(key,T->data.key)) { p=T; return T; } else if(LT(key,T->data.key))return SearchBST(T->lchild,key,T,p); else return SearchBST(T->rchild,key,T,p); }
void test1() { int data[] = {35, 6, 15, 65, 7, 46, 17, 43, 3, 7}; // int data[] = {3}; int size = sizeof(data)/sizeof(*data); // printf("size = %d\n", size); BSTree BST = NULL; BuildBSTree(BST, data, 0, size - 1); PreOrderTraverse(BST); printf("\n"); MidOrderTraverse(BST); printf("\n"); PostOrderTraverse(BST); printf("\n"); BSTree node = SearchBST(BST, 17, 0); if (!node) printf("Search failed.\n"); else printf("Search success, the search value is %d.\n", node->value); int n; scanf("%d", &n); while (n != -1) { DeleteNode(BST, n); // printf("Delete node %d.\n", n); MidOrderTraverse(BST); printf("\n"); scanf("%d", &n); } printf("\n"); }
void main() { BiTree dt, p; int i; KeyType j; ElemType r[N] = { {45,1},{12,2},{53,3},{3,4},{37,5},{24,6},{100,7},{61,8},{90,9},{78,10} }; /* 以教科书图9.7(a)为例 */ InitDSTable(&dt); /* 构造空表 */ for (i = 0; i < N; i++) InsertBST(&dt, r[i]); /* 依次插入数据元素 */ TraverseDSTable(dt, print); printf("\n请输入待查找的值: "); scanf("%d", &j); p = SearchBST(dt, j); if (p) { printf("表中存在此值。"); DeleteBST(&dt, j); printf("删除此值后:\n"); TraverseDSTable(dt, print); printf("\n"); } else printf("表中不存在此值\n"); DestroyDSTable(&dt); }
/*Binary Search Tree 节点插入操作*/ Status InsertBST(BinaryTree &btree, int data) { BinaryTree p = NULL, node = NULL, f = NULL; //没有找到节点才可以插入 if (!SearchBST(btree, data, f, p)) { node = (BinaryTree)malloc(sizeof(TreeNode)); node->key = data; node->lchild = NULL; node->rchild = NULL; if (!p) { btree = node; } else if (p->key>data) { p->lchild = node; } else { p->rchild = node; } return OK; } else return ERROR; }
/* 否则指针p指向查找路径上访问的最后一个结点并返回FALSE */ Status SearchBST(BiTree T, int key, BiTree f, BiTree *p) { if (!T) /* 查找不成功 */ { *p = f; return FALSE; } else if (key==T->data) /* 查找成功 */ { *p = T; return TRUE; } else if (key<T->data) return SearchBST(T->lchild, key, T, p); /* 在左子树中继续查找 */ else return SearchBST(T->rchild, key, T, p); /* 在右子树中继续查找 */ }
bool SearchBST (BiTree T , int data , BiTree f , BiTree &p) { if(!T) { p = f ; return false ; } else if ( data == T->digit ) { p = T ; return true ; } else if ( data < T->digit ) return SearchBST ( T->left_child , data , T , p ); else return SearchBST ( T->right_child , data , T , p ); }
BiTree SearchBST(BiTree T,KeyType key) { /* 在根指针T所指二叉排序树中递归地查找某关键字等于key的数据元素, */ /* 若查找成功,则返回指向该数据元素结点的指针,否则返回空指针。算法9.5(a) */ if((!T)||EQ(key,T->data.key)) return T; /* 查找结束 */ else if LT(key,T->data.key) /* 在左子树中继续查找 */ return SearchBST(T->lchild,key); else return SearchBST(T->rchild,key); /* 在右子树中继续查找 */
BSTree SearchBST(BSTree T,KeyType key) { // 在根指针T所指二叉排序树中递归地查找某关键字等于key的数据元素, // 若查找成功,则返回指向该数据元素结点的指针,否则返回空指针。算法9.5(a) if((!T)||EQ(key,T->data.key)) return T; // 查找结束 else if LT(key,T->data.key) // 在左子树中继续查找 return SearchBST(T->lchild,key); else return SearchBST(T->rchild,key); // 在右子树中继续查找
/* Search Binary Sort Tree to find 'key' * f points to T's parents, inlilated NULL * if succeeded, p points to data structure, and return FALSE * or, p points to last data structure of the path */ Status SearchBST(BitTree T, int key, BitTree f, BitTree *p) { if(!T) //terminal leaf { *p = f; //结点为叶结点时,*p指向双亲结点 return FALSE; } else if(key == T->data) //successfully searched { *p = T; // printf("T->data:%d\n", T->data); return TRUE; } else if(key < T->data) { return SearchBST(T->lchild, key, T, p); } else return SearchBST(T->rchild, key, T, p); }
Status deleteBST(BinaryTree btree, int key) { BinaryTree p, f; if (!SearchBST(btree, key, f, p)) { return ERROR;//无法找到关键字,删除失败 } else { deleteNode(p, f); } }
void main() { BSTree T; char ch1,ch2; KeyType Key; printf("建立一棵二叉排序树的二叉链表存储\n"); T=CreateBST(); ch1='y'; while (ch1=='y' || ch1=='Y') { printf("请选择下列*作:\n"); printf("1------------------更新二叉排序树存储\n"); printf("2------------------二叉排序树上的查找\n"); printf("3------------------二叉排序树上的插入\n"); printf("4------------------二叉排序树上的删除\n"); printf("5------------------二叉排序树中序输出\n"); printf("6------------------退出\n"); scanf("\n%c",&ch2); switch (ch2) { case '1': T=CreateBST(); break; case '2': printf("\n请输入要查找的数据:"); scanf("\n%d",&Key); SearchBST(T,Key); printf("查找*作完毕。\n"); break; case '3': printf("\n请输入要插入的数据:"); scanf("\n%d",&Key); InsertBST(&T,Key); printf("插入*作完毕。\n"); break; case '4': printf("\n请输入要删除的数据:"); scanf("\n%d",&Key); DelBSTNode(&T,Key); printf("删除*作完毕。\n"); break; case '5': InorderBST(T); printf("\n二叉排序树输出完毕。\n"); break; case '6': ch1='n'; break; default: ch1='n'; } } }
//�����㷨 int BiSearchT::InsertBST(BiTree *t,int e){ BiTree p; BiTree s; if(!SearchBST(*t,e,NULL,&p)){ s=(BiTree)malloc(sizeof(BiNode)); s->data=e;s->l=s->r=NULL; if(!p) *t=s; else if(e<p->data) p->l=s; else p->r=s; return true; } else return false; }
Status InsertBST(BiTree *T,int key){ BiTree p,s; if(!SearchBST(*T,key,NULL,&p)){ s=(BiTree)malloc(sizeof(BiTNode)); s->data=key; s->lchild=s->rchild=NULL; if(!p)*T=s; else if(key<p->data)p->lchild=s; else p->rchild=s; return TRUE; } else return FALSE; }
//返回key值的节点 Node* SearchBST(BST t, int key) { Node *left; Node *right; if (t != NULL && t->key == key) { return t; } else if (t != NULL) { left = SearchBST(t->lChild, key); //搜索左子树 if (left == NULL) { right = SearchBST(t->rChild, key); //搜索右子树 if (right != NULL) return right; else return NULL; } else { return left; } } else { return NULL; } }
Status InsertBST(BiTree &T,ElemType e) { if(!SearchBST(T,e.key,NULL,p)){ s=(BiTree)malloc (sizeof(BiTNode)); s->data=e; s->lchild=s->rchild=NULL; if(!p)T=s; else if(LT(e.key,p->data.key))p->lchild=s; else p->rchild=s; return TRUE; } else return FALSE; }
inline bool InsertBST ( BiTree &T , int data ) { BiTree p = NULL , s = NULL ; if( SearchBST (T, data , NULL , p ) ) return false ; else { s = new BiNode ; s->digit = data ; if ( p == NULL ) T = s ; else if ( data < p->digit ) p->left_child = s ; else p->right_child = s ; return true ; } }
int main() { int a[] = {16, 53, 54, 75, 80, 99, 100, 110, 160, 161, 162}; int i, res, need = 17; BitTree T = NULL, p; for(i = 0; i < sizeof(a)/sizeof(a[0]); i++) { InsertBST(&T, a[i]); } res = SearchBST(T, need, NULL, &p); if(FALSE == res) fprintf(stderr, "Not found %d\n", need); else fprintf(stdout, "Has found %d\n", need); }
/* 插入key并返回TRUE,否则返回FALSE */ Status InsertBST(BiTree *T, int key) { BiTree p,s; if (!SearchBST(*T, key, NULL, &p)) /* 查找不成功 */ { s = (BiTree)malloc(sizeof(BiTNode)); s->data = key; s->lchild = s->rchild = NULL; if (!p) *T = s; /* 插入s为新的根结点 */ else if (key<p->data) p->lchild = s; /* 插入s为左孩子 */ else p->rchild = s; /* 插入s为右孩子 */ return TRUE; } else return FALSE; /* 树中已有关键字相同的结点,不再插入 */ }
void main() { BSTNode *bt,*p,*f; int n=12,x=46; KeyType a[]={25,18,46,2,53,39,32,4,74,67,60,11}; bt=CreateBST(a,n); printf("BST:");DispBST(bt);printf("\n"); printf("删除%d结点\n",x); if (SearchBST(bt,x)!=NULL) { DeleteBST(bt,x); printf("BST:");DispBST(bt);printf("\n"); } x=18; p=SearchBST1(bt,x,NULL,f); if (f!=NULL) printf("%d的双亲是%d\n",x,f->key); }
/* * 二叉排序树插入操作 * 当二叉排序树中存在关键字key的数据元素时 * 插入key并返回TRUE, 否则返回FALSE */ Status InsertBST(BitTree *T, int key) { BitTree p, s; if(SearchBST(*T, key, NULL, &p)) //查找不成功 { s = (BitTree)malloc(sizeof(BiTNode)); s->data = key; printf("%d\n", key); s->lchild = s->rchild = NULL; if(!p) *T = s; //插入s为新的根结点 else if(key < p->data) p->lchild = s; else p->rchild = s; return TRUE; } else return FALSE; }
void main() { BSTree dt, p; Status k; int i; KeyType j; ElemType r[N] = { {13,1},{24,2},{37,3},{90,4},{53,5} }; /* (以教科书图9.12为例) */ InitDSTable(&dt); /* 初始化空树 */ for (i = 0; i < N; i++) InsertAVL(&dt, r[i], &k); /* 建平衡二叉树 */ TraverseDSTable(dt, print); /* 按关键字顺序遍历二叉树 */ printf("\n请输入待查找的关键字: "); scanf("%d", &j); p = SearchBST(dt, j); /* 查找给定关键字的记录 */ if (p) print(p->data); else printf("表中不存在此值"); printf("\n"); DestroyDSTable(&dt); }
int main() { int key, i; BSTree bst, *pos; CreateBST(&bst, source, ARRAYLEN); printf("遍历二叉排序树结果:"); BST_LDR(&bst); scanf_s("%d", &key); pos = SearchBST(&bst, key); if (pos) { printf("search successfull!"); } else { printf("search failed!"); } getchar(); getchar(); return 0; }
int main() { int num[1000] = {15, 55, 6, 7, 25, 2, 1, 66, 94, 64}; int n = 10; BST T = NULL; InitBST(T); T->key = num[0]; int k = 1; for (; k < n; k++) { Node* node = NULL; InitBST(node); node->key = num[k]; InsertBST(T, node); } while (1) { printf("现有的数字为:"); PrintNum(num, n); printf("请选择:\n"); printf("1.添加节点并重新构造排序树\n2.删除节点\n3.先序遍历\n4.中序遍历\n5.后序遍历\n6.搜索结点\n"); int select; scanf("%d", &select); switch (select) { case 1: { printf("请添加key:"); int addNum; scanf("%d", &addNum); int i; int flag = 0; for (i = 0; i < n; i++) { if (num[i] == addNum) { printf("在数组中已存在该数字\n"); flag = -1; break; } } if (flag == -1) { continue; } num[n] = addNum; n++; DestroyBST(T); InitBST(T); T->key = num[0]; i = 1; for (; i < n; i++) { Node* node = NULL; InitBST(node); node->key = num[i]; InsertBST(T, node); } break; } case 2: { printf("请输入key:"); int delNum; scanf("%d", &delNum); int i; int index; int flag = 0; for (i = 0; i < n; i++) { if (num[i] == delNum) { flag = 1; index = i; break; } } if (flag == 0) { printf("数组中没有这个key\n"); continue; } DeleteBST(T, delNum); for (i = index; i < n - 1; i++) { num[i] = num[i + 1]; } n--; break; } case 3: PreOrderTraverseBST(T, Visit); printf("\n"); break; case 4: InOrderTraverseBST(T, Visit); printf("\n"); break; case 5: PostOrderTraverseBST(T, Visit); printf("\n"); break; case 6: int searchKey; printf("请输入key值:"); scanf("%d", &searchKey); Node* node = SearchBST(T, searchKey); if (node != NULL) { printf("搜索成功!节点key值%d\n", node->key); } default: break; fflush(stdin); } } }