二叉排序树的建立、结点插入及删除
来源:互联网 发布:java二维数组初始化为0 编辑:程序博客网 时间:2024/05/16 19:20
#include<stdio.h>#include<stdlib.h>#define STATUS int#define FAIL 0#define SUCCESS 1typedef struct bstnode//定义排序二叉树结点结构体{ int data; struct bstnode *lchild,*rchild;}bstnode,*bstptr;void bitinsert(bstptr *thead,int elm)//二叉排序树插入节点{ bstptr temp; if (*thead==NULL)//没有后续节点 { temp = (bstptr)malloc(sizeof(bstnode)); temp->data = elm;//初始化二叉树结点 temp->lchild = NULL; temp->rchild = NULL; *thead = temp;//完成节点插入; } else if(elm<(*thead)->data) { bitinsert(&(*thead)->lchild,elm); } else if(elm>(*thead)->data) { bitinsert(&(*thead)->rchild,elm); } else return;}void InOrderTranverse(bstptr thead)//中序遍历{ if (thead==NULL)// { return; } else { InOrderTranverse(thead->lchild);//遍历左子树 printf("%p,%d\n",thead,thead->data);//打印根结点 InOrderTranverse(thead->rchild);//遍历右子树 }}STATUS BST_Search(bstptr bsthead,int key,bstptr *keyptr)//二叉排序树查找{ if (bsthead==NULL)//没有找到 { *keyptr = NULL; return FAIL; } else if(key ==bsthead->data)//找到 { *keyptr = bsthead;//返回对应地址 return SUCCESS; } else if(key <bsthead->data) { BST_Search(bsthead->lchild,key,keyptr);//遍历左子树 } else if(key >bsthead->data) { BST_Search(bsthead->rchild,key,keyptr);//遍历右子树 }}STATUS BST_Delete(bstptr bsthead,int key)//删除结点,先查找,找到后再删除{ if(bsthead==NULL) { return FAIL; } else if(key == bsthead->data)//找到该结点 { return Delete(&bsthead);//删除该结点 } else if(key < bsthead->data)//找到该结点 { BST_Delete(bsthead->lchild,key);//删除值比 } else if(key > bsthead->data)//找到该结点 { BST_Delete(bsthead->rchild,key); }}STATUS Delete(bstptr * t)//传输的是指针域的地址,直接修改lchild or rchild{ bstptr q,s; if((*t)->lchild==NULL)//左子树为空,连接右子树 { q = *t; (*t) = (*t)->rchild;//连接右子树,直接改变指针 free(q);//删除结点所占空间 } else if((*t)->rchild==NULL)//右子树为空,连接左子树 { q = *t; (*t) = (*t)->lchild;//连接左子树,直接改变指针 free(q);//删除结点所占空间 } else//有两个孩子 { q = (*t);//找到中序遍历时的前驱结点,即孩子结点的右子树最右边的叶子结点 s = (*t)->lchild; while(s->rchild!=NULL) { q = s;//记录当前结点值 s = s->rchild;//找到右子树最右边的叶子结点 } (*t)->data = s->data; if (q==*t)//孩子结点为目标结点 { q->lchild = s->lchild;//链接 } else//存在右子树 { q->rchild = s->lchild;//目标结点的父亲结点链接孩子结点 } free(s); } return SUCCESS;}void main(){ int i;//计数 bstptr keyptr = NULL; int a[]={62,88,58,47,35,73,51,99,37,93}; bstptr bsthead=NULL; //bitinsert(&bsthead,1); //bitinsert(&bsthead,2); //bitinsert(&bsthead,3); //bitinsert(&bsthead,4); //bitinsert(&bsthead,4); for (i=0;i<10;i++) { bitinsert(&bsthead,a[i]); } InOrderTranverse(bsthead); if(BST_Search(bsthead,99,&keyptr)) { printf("\n%p,%d\n\n",keyptr,keyptr->data); } if( BST_Delete(bsthead,30)) { InOrderTranverse(bsthead); } else { printf("key:%d is not in the tree\n"); } getchar();}
0 0
- 二叉排序树的建立、结点插入及删除
- 二叉排序树的查找、插入、删除、建立
- 二叉排序树的结点删除
- 二叉排序树的创建(结点的插入,删除等操作)
- 二叉排序树的建立,删除
- 二叉排序树的操作(建立、插入、删除和查找)
- java实现二叉排序树的建立,查找,插入,删除
- 二叉搜索树的插入建立与结点删除
- 查找(3)——二叉排序树的建立、结点的查找和删除
- 平衡二叉排序树插入结点的学习总结
- 二叉排序树的查找、删除、插入
- 二叉排序树插入删除的实现
- 二叉排序树的插入与删除
- 二叉排序树的插入和删除
- 二叉排序树的删除与插入
- 二叉排序树的插入,查找,删除
- 二叉排序树的查找-插入-删除
- 二叉排序树删除值小于value的结点
- java从0单排之<<java就业培训教程>>复习与面试题回顾——02
- 一起学spring--依赖注入---简单粗暴的例子展示
- 【机器学习】机器学习界大牛林达华推荐的书籍
- 16.OC面向对象—继承
- 17.OC面向对象—多态
- 二叉排序树的建立、结点插入及删除
- POJ 2800 Joseph's Problem(数论)
- Jackson 2.X 工具类
- 18.OC点语法和变量作用域
- ORACLE PL/SQL编程,触发器
- 验证码——python去除干扰线
- ubuntu安装flash插件
- JavaScript总结
- Android中的5种数据存储方式之——SQLite