二叉排序树的实现:分别以顺序表和二叉链表作为存储结构,实现二叉排序树。基本操作有查找、插入、删除。
我写了个用二叉链表作为存储结构的,不知对不对,希望高手帮忙改一改,再帮我转换一个用顺序表作为存储结构的,急用!拜托了!#include<stdio.h>#include<...
我写了个用二叉链表作为存储结构的,不知对不对,希望高手帮忙改一改,再帮我转换一个用顺序表作为存储结构的,急用!拜托了!
#include <stdio.h>
#include <malloc.h>
int main()
{
}
typedef struct BiTnode
{
int data;
struct BiTnode *lchild;
struct BiTnode *rchild;
}BiTnode,*BiTree;
BiTree search_tree(BiTree T,int keyword,BiTree *father)
{
BiTree p;
*father = NULL;
p = T;
while (p && p->data!=keyword)
{
*father = p;
if (keyword < p->data)
p = p->lchild;
else
p = p->rchild;
}
return p;
}
int Insert_tree(BiTree *T,int elem)
{
BiTree c,p,father;
c = (BiTnode *)malloc(sizeof(BiTnode));
if (!c)
return -1;
c->data = elem;
c->lchild = c->rchild = NULL;
p = search_tree(*T,elem,&father);
if (!p)
return -1;
if (father == NULL)
*T = c;
else if (elem < father->data)
father->lchild = c;
else
father->rchild = c;
return 0;
} 展开
#include <stdio.h>
#include <malloc.h>
int main()
{
}
typedef struct BiTnode
{
int data;
struct BiTnode *lchild;
struct BiTnode *rchild;
}BiTnode,*BiTree;
BiTree search_tree(BiTree T,int keyword,BiTree *father)
{
BiTree p;
*father = NULL;
p = T;
while (p && p->data!=keyword)
{
*father = p;
if (keyword < p->data)
p = p->lchild;
else
p = p->rchild;
}
return p;
}
int Insert_tree(BiTree *T,int elem)
{
BiTree c,p,father;
c = (BiTnode *)malloc(sizeof(BiTnode));
if (!c)
return -1;
c->data = elem;
c->lchild = c->rchild = NULL;
p = search_tree(*T,elem,&father);
if (!p)
return -1;
if (father == NULL)
*T = c;
else if (elem < father->data)
father->lchild = c;
else
father->rchild = c;
return 0;
} 展开
2个回答
展开全部
楼主注意用顺序表作二叉树的存储结构的结点的结构, 结点的地址是顺序表的索引值
时间复杂度是 n
C/C++ code#include <stdio.h>
typedef struct Nod
{
char d;
int left, right;
}Node;
void inorder(Node t[], int root)
{
if(root!=-1)
{
inorder(t, t[root].left);
printf("%c ", t[root].d);
inorder(t, t[root].right);
}
}
void main()
{
Node t[3] ={{'r', 1, 2} , {'1', -1, -1}, {'2', -1, -1}};
inorder(t, 0);
时间复杂度是 n
C/C++ code#include <stdio.h>
typedef struct Nod
{
char d;
int left, right;
}Node;
void inorder(Node t[], int root)
{
if(root!=-1)
{
inorder(t, t[root].left);
printf("%c ", t[root].d);
inorder(t, t[root].right);
}
}
void main()
{
Node t[3] ={{'r', 1, 2} , {'1', -1, -1}, {'2', -1, -1}};
inorder(t, 0);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询