二叉排序树
来源:互联网 发布:手机知乎提问没人回答 编辑:程序博客网 时间:2024/05/21 22:32
#include <stdio.h>#include <stdlib.h>//二叉排序树结点typedef struct BSTreeNode { int val; struct BSTreeNode *lchild; struct BSTreeNode *rchild;} BSTreeNode, *BSTree;BSTree insert(BSTree root, int val) { if (root == NULL) { BSTree node = (BSTree) malloc(sizeof(BSTreeNode)); node->val = val; node->lchild = NULL; node->rchild = NULL; root = node; } else if (val < root->val) { root->lchild = insert(root->lchild, val); } else { root->rchild = insert(root->rchild, val); } return root;}BSTree buildBSTree(BSTree root, int *arr, int n) { int i; for (i = 0; i < n; i++) { root = insert(root, arr[i]); } return root;}//查找元素key,找到返回key的结点指针,没找到返回NULLBSTree search(BSTree root, int key) { if (root == NULL) { return NULL; } if (key > root->val)//查找右子树 { return search(root->rchild, key); } else if (key < root->val)//查找左子树 { return search(root->lchild, key); } else { return root; }}int delNode(BSTree *root) { BSTree q, s; //root为叶子结点 if (!(*root)->lchild && !(*root)->rchild) { *root = NULL; } else if (!(*root)->lchild) {//左子树为空,重接右子树 q = *root; *root = (*root)->rchild; free(q); } else if (!(*root)->rchild) {//右子树为空,重接左子树 q = *root; *root = (*root)->rchild; free(q); } else {//左右子树均不为空 q = *root; s = (*root)->lchild; while (s->rchild) { q = s; s = s->rchild;//转左,然后向右走到尽头 } (*root)->val = s->val; if (q != *root) {//判断是否执行上述while循环 q->rchild = s->lchild;//执行上述while循环,重接右子树 } else { q->lchild = s->lchild;//未执行上述while循环,重接左子树 } free(s); } return 1;}//若二叉排序树T中存在关键字等于key的数据元素时,则删除该数据元素结点int delBST(BSTree *root, int key) { if (!(*root)) { return 0; } else { if (key == (*root)->val) { return delNode(root); } else if (key < (*root)->val) { return delBST(&(*root)->lchild, key); } else { return delBST(&(*root)->rchild, key); } }}void visit(BSTree root) { printf("%d ", root->val);}//前序遍历void preOrder(BSTree root) { if (root != NULL) { visit(root);//访问根结点 preOrder(root->lchild); preOrder(root->rchild); }}//中序遍历void inOrder(BSTree root) { if (root != NULL) { inOrder(root->lchild); visit(root); inOrder(root->rchild); }}//后序遍历void postOrder(BSTree root) { if (root != NULL) { postOrder(root->lchild); postOrder(root->rchild); visit(root); }}int main() { int arr[] = {10, 6, 15, 2, 7, 12, 4, 13}; BSTree root = NULL; root = buildBSTree(root, arr, 8); printf("先序遍历二叉树: "); preOrder(root); printf("\n"); /*printf("中序遍历二叉树: "); inOrder(root); printf("\n"); printf("后序遍历二叉树: "); postOrder(root); printf("\n");*/ //查找元素7 /*BSTree node = search(root,7); if (node != NULL){ printf("二叉排序树存在结点值为%d的结点\n",node->val); } else { printf("二叉排序树不存在结点值为7的结点\n"); }*/ /*//查找元素25 BSTree node1 = search(root,25); if (node1 != NULL){ printf("二叉排序树存在结点值为%d的结点\n",node1->val); } else { printf("二叉排序树不存在结点值为25的结点\n"); }*/ if (delBST(&root,2)) { printf("删除结点元素为2的结点\n先序遍历二叉树: "); preOrder(root); } return 0;}
0 0
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- Android 基础(三)、User Interface
- 利用AppInventor实现登录功能(完整版)
- NAT 四种地址解释
- 基于ThinkPHP3的微信平台开发_1
- Hibernate HQL以及相关方法
- 二叉排序树
- Android中的MediaMetadataRetriever类取得媒体文件信息
- ClipDrawable的使用
- 后缀表达式(逆波兰表达式),并求值(可求浮点、负数与大于10的数),C++实现
- 【css基础】div父元素根据子元素高度自适应高度
- 欢迎使用CSDN-markdown编辑器
- 是什么造就了如此庞大的PHP帝国?
- AppInventor专题:如何实现两个页面之间的跳转
- union的使用