茅草屋的搭建视频:用C++实现二叉排序树的各种算法

来源:百度文库 编辑:中科新闻网 时间:2024/04/29 22:00:19
用函数实现如下算法:
(1) 建立二叉排序树
(2) 插入、删除结点
(3) 前序、中序、后序、层次遍历二叉树
(4) 中序遍历的非递归算法
(5) 在二叉树中查找给定关键字
(6) 交换各结点的左右子树
(7) 求二叉树的深度、叶子结点树
(8) 输出树型结构

在下求C++的源程序,急用!!!

#include<stdio.h>
#include<stdlib.h>

#define D 2
#define R 10
#define N 11
typedef int KeyType;
typedef int DataType;

struct Node;
typedef struct Node radixNode;
struct Node
{
KeyType key[D];
/* DataType info; */
radixNode *next;
};

typedef radixNode * radixList;
typedef struct QueueNode
{
radixNode *f;
radixNode *e;
} Queue;
Queue queue[R];

void display(radixNode *plist)
{
int i;
radixNode *p;
p=plist->next;

while(p!=NULL)
{
printf("->");
for(i=0;i<D;i++)
printf("%1d",p->key[i]);
p=p->next;
}
printf("\n");
}

void radixSort(radixNode *plist,int d,int r)
{
int i,j,k;
radixNode *p,*head;
head=plist->next;
for(j=d-1;j>=0;j--)
{
p=head;
for(i=0;i<r;i++)
{
queue[i].f=NULL;
queue[i].e=NULL;
}
while(p!=NULL)
{
k=p->key[j];
if(queue[k].f==NULL)
queue[k].f=p;
else
(queue[k].e)->next=p;
queue[k].e=p;
p=p->next;
}
i=0;
while(queue[i].f==NULL)
i++;
p=queue[i].e; head=queue[i].f;
for(i++;i<r; i++)
if(queue[i].f!=NULL)
{
p->next=queue[i].f;
p=queue[i].e;
}
p->next=NULL;
printf("j=%d",j);
}
plist->next=head;
}

void main()
{
radixNode *p,*q,*head;
int a[]={30,12,20,17,40,60,34,42,29,35,47};
int i;

p=(radixNode *) malloc(sizeof(struct Node));
head=p;

p->next=NULL;
printf("test...\n");
for(i=0;i<11;i++)
{
q=(radixNode *) malloc(sizeof(struct Node));

q->key[0]=a[i]/10;
q->key[1]=a[i]%10;
q->next=NULL;
p->next=q;
p=p->next;

}
printf("before:\n");
display(head);
printf("\n");
radixSort(head,D,R);
printf("after:\n");
display(head);
}

这个不难啊,自己跟着书写吧