您的位置:首页 > 其它

WORD 2010无法显示图片的解决方法

2012-04-12 10:28 776 查看
二叉排序树又称为二叉查找树,它可以汗死一棵空树,也可以是具有以下性质的二叉树。

(1)若它的左子树非空,则左子树上所有结点的值都小于根节点的值。
(2)若它的右子树非空,则右子树上所有结点的值都大于根节点的值。
(3)左、右子树本身又是二叉排序树。
由二叉排序树的特点可以知道,中序遍历二叉排序树得到是一组递增的数据。
采用二叉链表作为才二叉排序树的存储结构,以下是二叉排序树的查找、插入和删除操作的实验程序代码。程序在ubuntu 12.04操作系统环境下经gcc编译通过。
#include<malloc.h>
#include<stdio.h>
typedef struct node{
int data;
struct node *lchild, *rchild;
}btree, *bitree;
//中序遍历二叉树
int inorder(bitree root)
{
if(root)
{
inorder(root->lchild);
printf("%3d", root->data);
inorder(root->rchild);
}
}
//二叉排序树查找
bitree search_bst(bitree tree, int key, bitree * parent)
{
bitree p = tree;
//如果是空树,*parent赋空值,返回
if(p == NULL)
{
*parent  = NULL;
return p;
}
/*如果查找的元素不存在,parent存放的就是查找
过程中查找的最后结点的地址,并返回空指针;
如果查找的元素存在,parent存放的就是该结点的双亲结点
的地址,并返回该结点的地址。*/
while(p && p->data != key)
{
*parent = p;
if(key < p->data)
p = p->lchild;
else
p = p->rchild;
}
return p;
}
//二叉排序树插入
int insert_bst(bitree *tree, int e)
{
bitree new_node, p, parent;

printf("insert\n");
p = search_bst(*tree, e, &parent);
if(p != NULL)
{
printf("已存在\n");
return -1;
}

new_node = (bitree)malloc(sizeof(btree));
new_node->data = e;
new_node->lchild = NULL;
new_node->rchild = NULL;

if(parent == NULL)
*tree = new_node;
else if(e < parent->data)
parent->lchild = new_node;
else
parent->rchild = new_node;

return 0;
}
int main()
{
bitree tree, p, parent;
tree = NULL;
int key, i = 0;
printf("create \n");
while(i < 6)
{
scanf("%d", &key);
insert_bst(&tree, key);
i++;
}

printf("  inorder start: ");
inorder(tree);
printf("\n");

printf("search :enter  the element:");
scanf("%d", &key);
p = search_bst(tree, key, &parent);
if(p)
printf("found %d\n", p->data);
else
printf("not found\n");

printf("\n");
return 0;
}
最近在学习过程中,发现了关于指针的一些之前没有在意的知识,指针在没有初始化或者为空时,是不能改变其所指内存单元的内容的,虽然在某些环境下能够编译通过,但是运行过程中执行到此处会出现段错误Segmentation fault (core dump)(这是在ubuntu操作系统上运行时出现的,windows上出现某指令引用的内存不能“read”),因为在编写代码是忽略了这点,运行时出现的错误让我摸不着头脑,在作为参数进行传递时,更不易察觉。

本文出自 “兵疯千里” 博客,请务必保留此出处http://slientradio.blog.51cto.com/7241495/1393651
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: