|
TOP
|
|
二叉排序树及其C代码(二)
ft; } /*移花接木,直接赋值,避免交换节点*/ p->key = mid->key; /*将mid节点的子节点作为pre的子节点,并将mid所指向的节点删除*/ if(pre->right == mid) pre->right = mid->right; else pre->left = mid->right; free(mid); } return 1; } /*中序输出bst树*/ void bst_print(bst_tree *root) { if(root == NULL) return; bst_print(root->left); printf(" %d ", root->key); bst_print(root->right); } |
测试代码:main.cpp
| #include
#include "bi_search_tree.h" int main() { int a[10] = {5,4,2,8,7,1,9,3,6,10}; int i=0; bst_tree *root=NULL; for(i=0; i<10; i++) root = bst_insert(root, a[i]); bst_delete(root, 5); bst_print(root); printf("\n%d %s\n", root->key, bst_search(root, 10) ? "yes":"no"); return 0; } |
|