二叉搜索树的插入和删除

zhang20084 2007-01-16 11:40:51
题目:2叉搜索树的插入和删除
要求
(1)任意选定一个2叉搜索树用先序遍历 中序遍历 和后序遍历显示该2叉搜索树

(2)动态显示出在2叉搜索树中插入一个元素时树的变化过程 要求演示出所有可能的情况

(3)动态显示出 在2叉搜索树中删除一个元素时的变化过程要求演示出所有的可能的情况

有谁有这样的代码啊,我100求啊。不够再加
程序比较多话,MSN:zhanglei20084@hotmail.com发给我好了,比有重谢啊
...全文
1727 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2011-07-30
  • 打赏
  • 举报
回复
不太好啊
lucky201011 2011-02-15
  • 打赏
  • 举报
回复
很精辟,学习了。
zhang20084 2007-01-20
  • 打赏
  • 举报
回复
动态显示我也没有做,不过我在网上找到了别人做的动态演示的FLASH

如果做就太复杂了,我是帮大学生做的,随便做做了,
zhang20084 2007-01-20
  • 打赏
  • 举报
回复
我也写好了,谢谢大家
如果想看我的,请到http://hi.baidu.com/zhang20084/blog/item/5f2de7cd901606510eb34513.html我写的上面看
结了
softwarewander 2007-01-19
  • 打赏
  • 举报
回复
搂主说的动态显示所有可能情况是什么意思呢?
举个例子说一下嘛,就拿删除一格节点来说说吧
jixingzhong 2007-01-18
  • 打赏
  • 举报
回复
//删除结点
struct Node *Delete(struct Node *Root,struct Node *pointer)
...{
struct Node *y=(struct Node*)malloc(sizeof(struct Node));
y->parent=NULL;
y->left=NULL;
y->right=NULL;
//找到实际要删除的结点
if(pointer->left==NULL||pointer->right==NULL)
y=pointer;
else
y=Successor(pointer);

struct Node *x=(struct Node*)malloc(sizeof(struct Node));
x->parent=NULL;
x->left=NULL;
x->right=NULL;
//要删除结点的子结点
if(y->left!=NULL)
x=y->left;
else
x=y->right;

if(x!=NULL)
x->parent=y->parent;

if(y->parent==NULL)
Root=x;//删除的是根结点
else
...{
if(y==y->parent->left)
y->parent->left=x;
else
y->parent->right=x;
}

if(y!=pointer)
pointer->key=y->key;

if(y!=NULL)
...{
free(y);
printf("Delete success! ");
}
else
printf("Delete failure! ");

return Root;
}

int main()
...{
struct Node *Root=NULL;
int value;

int flag=1;
while(flag)
...{
printf("1:Insert ");
printf("2:Delete ");
printf("3:Search ");
printf("4:Minimum ");
printf("5:Maximum ");
printf("6:Successor ");
printf("7:Predecessor ");
printf("0:Exit ");
printf("Please input your choice:");
scanf("%d",&flag);

switch(flag)
...{
case 0:
return 0;
case 1:
...{
printf("Please input the value you want to input:");
scanf("%d",&value);
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->key=value;
newNode->parent=NULL;
newNode->left=NULL;
newNode->right=NULL;
Root=Insert(Root,newNode);
break;
}
case 2:
...{
printf("Please input the value you want to delete:");
scanf("%d",&value);
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
newNode=Search(Root,value);
if(newNode!=NULL)
Root=Delete(Root,newNode);
else
printf("%d does'n exist in this tree! ",value);
break;
}
case 3:
...{
printf("Please input the value you want to input:");
scanf("%d",&value);
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
newNode=Search(Root,value);
if(newNode!=NULL)
printf("%d exist in this tree! ",newNode->key);
else
printf("%d does'n exist in this tree! ",value);
break;
}
case 4:
...{
if(Root!=NULL)
...{
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
newNode=Minimum(Root);
printf("The minimum value of this tree is %d! ",newNode->key);
}
else
printf("This tree is empty! ");
break;
}
case 5:
...{
if(Root!=NULL)
...{
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
newNode=Maximum(Root);
printf("The Maximum value of this tree is %d! ",newNode->key);
}
else
printf("This tree is empty! ");
break;
}
case 6:
...{
printf("Please input the value:");
scanf("%d",&value);
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
newNode=Search(Root,value);
if(newNode!=NULL)
...{
newNode=Successor(newNode);
if(newNode!=NULL)
printf("%d's successor is %d! ",value,newNode->key);
else
printf("%d has't successor int this tree! ",value);
}
else
printf("%d does'n exist in this tree! ",value);
break;
}
case 7:
...{
printf("Please input the value:");
scanf("%d",&value);
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
newNode=Search(Root,value);
if(newNode!=NULL)
...{
newNode=Predecessor(newNode);
if(newNode!=NULL)
printf("%d's Predecessor is %d! ",value,newNode->key);
else
printf("%d has't predecessor int this tree! ",value);
}
else
printf("%d does'n exist in this tree! ",value);
break;
}
default:
break;
}

PrintTree(Root);
}

DeleteTree(Root);
return 0;
}
jixingzhong 2007-01-18
  • 打赏
  • 举报
回复
二叉查找树又叫二叉排序树,属于二叉树的一种特例。它的左子树上所有节点的值均小于根节点的值,它的右子树上的所有节点的

值均大于根节点的值,并且其左右子树也都是二叉查找树。搜索,插入,删除的复杂度等于树高,O(log(n)).

#include "stdio.h"
#include "stdlib.h"

struct Node
...{
int key;//结点值
struct Node *parent;//父结点
struct Node *left;//左结点
struct Node *right;//右结点
};

//打印二叉树
void PrintTree(struct Node *Root)
...{
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
pointer=Root;
if(pointer!=NULL)
...{
PrintTree(pointer->left);
printf("%d ",pointer->key);
PrintTree(pointer->right);
}
}

//删除树,递归释放树的结点
void DeleteTree(struct Node *Root)
...{
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
pointer=Root;

if(pointer!=NULL)
...{
DeleteTree(pointer->left);
free(pointer);
DeleteTree(pointer->right);
}
}

//最小值
struct Node *Minimum(struct Node *Root)
...{
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
pointer=Root;

while(pointer->left!=NULL)
pointer=pointer->left;

return pointer;
}

//最大值
struct Node *Maximum(struct Node *Root)
...{
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
pointer=Root;

while(pointer->right!=NULL)
pointer=pointer->right;

return pointer;
}

//查找
struct Node *Search(struct Node *Root,int value)
...{
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
pointer=Root;

while(pointer!=NULL&&pointer->key!=value)
...{
//如果查找值比结点值小,查找它的左子树
if(value<pointer->key)
pointer=pointer->left;
else
pointer=pointer->right;//如果查找值比结点值大,查找它的右子树
}

return pointer;
}

//后继
struct Node *Successor(struct Node *pointer)
...{
if(pointer->right!=NULL)
return Minimum(pointer->right);

struct Node *y=(struct Node*)malloc(sizeof(struct Node));
y=pointer->parent;
while(y!=NULL&&y->right==pointer)
...{
pointer=y;
y=pointer->parent;
}

return y;
}

//前驱
struct Node *Predecessor(struct Node *pointer)
...{
if(pointer->left!=NULL)
return Maximum(pointer->left);

struct Node *y=(struct Node*)malloc(sizeof(struct Node));
y=pointer->parent;
while(y!=NULL&&y->left==pointer)
...{
pointer=y;
y=pointer->parent;
}

return y;
}

//插入新结点
struct Node *Insert(struct Node *Root,struct Node *newNode)
...{
struct Node *back=(struct Node*)malloc(sizeof(struct Node));
struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
back=NULL;
pointer=Root;

//找到要插入的位置
while(pointer!=NULL)
...{
back=pointer;
if(newNode->key<pointer->key)
pointer=pointer->left;
else
pointer=pointer->right;
}

//新结点的Parent指针先指向父结点
newNode->parent=back;

if(back==NULL)
Root=newNode;//树为空,新结点成为树根
else
...{
if(newNode->key<back->key)
back->left=newNode;
else
back->right=newNode;
}

return Root;
}
jixingzhong 2007-01-18
  • 打赏
  • 举报
回复
二、二叉搜索树的插入与删除

树形结构的一个重要应用是用来组织索引,二叉搜索树是适用于内存储器的一种重要的树形索引。二叉搜索树里每个结点的左子树中所有结点的关键码值都小于该结点的关键码值,而右子树中所有结点的关键码值都大于该结点的关键码值。二叉树的插入和删除需要保证插入和删除以后仍符合二叉搜索树的定义。

1、插入是这样进行的:将待插入结点的关键码值与树根的关键码值比较,若待插入的关键码值小于树根的关键码值,则进入左子树,否则进入右子树。在子树里又与子树根比较,如此进行下去,直到把新结点插入到二叉树里作为一个新的树叶。
对于给定的关键码集合,为建立二叉搜索树,可以从一个空的二叉搜索树开始,将关键码一个个插进去。
将关键码集合组织成二叉搜索树,实际上起了对集合里的关键码进行排序的作用,按中序周游二叉搜索树,就能得到排好的关键码序列。

2、从二叉搜索树里删除一个结点时,不能把以这个结点为根的子树都删除掉,只能删除掉这一个结点,并且还要保持二叉搜索树原来的性质。
设p,p1,r是指针变量,p↑表示s要删除的结点,p1↑表示p↑的父母结点,则删除可以按如下规定进行:若结点p↑没有左子树,则用右子树的根代替被删除的结点p↑。若结点p↑有左子树,则在左子树里找按中序周游的最后一个结点r↑,将r↑的右指针置成指向p↑的右子树的根,然后用结点p↑的左子树的根去代替被删除的结点p↑。
改进的删除算法:设p,p1,r是指针变量,p↑表示s要删除的结点, p1↑表示p↑的父母结点,则删除可以按如下规定进行。若结点p↑没有左子树,则用右子树的根代替被删除的结点p↑。若结点p↑有左子树,则在左子树里找按中序周游的最后一个结点r↑,将r↑的右指针置成指向p↑的右子树的根,然后用结点r↑去代替被删除的结点p↑。
zhang20084 2007-01-18
  • 打赏
  • 举报
回复
汗,郁闷的,还要钱,那我自己作吧

有谁可以提供一点资料啊
a_b_c_abc5 2007-01-17
  • 打赏
  • 举报
回复
$50.0
愿意出我就给你做了.

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧