#include <stdio.h>
#include <stdlib.h>
//数据结构定义
struct node
{
int data ;
struct node *lchild ,*rchild ;
};
typedef struct node *pTree;
/*第一关*/
/*1.创建一个二叉树结点,值为element*/
pTree createTreeNode(int element)
{
pTree node = (pTree)malloc(sizeof(struct node));
node->data = element;
node->lchild = NULL;
node->rchild = NULL;
return node;
}
/*2.在二叉排序树中插入一个数据元素,若二叉树为空,则新建根节点*/
pTree insertData(int x , pTree T)
{
if(T==NULL){
T = createTreeNode(x);
return T;
}
pTree p = T;
while(1){
if(x<=p->data&&p->lchild==NULL){
p->lchild = createTreeNode(x);
return T;
}
else if(x>p->data&&p->rchild==NULL){
p->rchild = createTreeNode(x);
return T;
}
if(x<p->data) p = p->lchild;
else p = p->rchild;
}
return T;
}
/*3.先序遍历和中序遍历函数*/
void preOrder( pTree T)
{
if(T==NULL) return;
printf("%d ",T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
void inOrder( pTree T)
{
if(T==NULL)return;
inOrder(T->lchild);
printf("%d ",T->data);
inOrder(T->rchild);
}
int main()
{
int a ,num;
pTree T =NULL;
scanf("%d",&num);
for(int i=0;i<num;i++)
{
scanf("%d",&a);
T = insertData(a, T);
}
inOrder(T);
printf("\n");
preOrder(T);
}
#include <stdio.h>
#include <stdlib.h>
//数据结构定义
struct node
{
int data ;
struct node *lchild ,*rchild ;
};
typedef struct node *pTree;
/*第一关*/
/*1.创建一个二叉树结点,值为element*/
pTree createTreeNode(int element)
{
pTree node = (pTree)malloc(sizeof(struct node));
node->data = element;
node->lchild = NULL;
node->rchild = NULL;
return node;
}
/*2.在二叉排序树中插入一个数据元素,若二叉树为空,则新建根节点*/
pTree insertData(int x , pTree T)
{
if(T==NULL){
T = createTreeNode(x);
return T;
}
pTree p = T;
while(1){
if(x<p->data&&p->lchild==NULL){
p->lchild = createTreeNode(x);
return T;
}
else if(x>p->data&&p->rchild==NULL){
p->rchild = createTreeNode(x);
return T;
}
if(x==p->data) return T;
if(x<p->data) p = p->lchild;
else p = p->rchild;
}
return T;
}
/*3.先序遍历和中序遍历函数*/
void preOrder( pTree T)
{
if(T==NULL) return;
printf("%d ",T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
void inOrder( pTree T)
{
if(T==NULL)return;
inOrder(T->lchild);
printf("%d ",T->data);
inOrder(T->rchild);
}
/*第二关*/
/*1.在二叉排序树T中查找最小值,返回该结点*/
pTree findMin(pTree T)
{
if(T==NULL)return T;
while(1){
if(T->lchild!=NULL){
T = T->lchild;
}else return T;
}
return T;
}
/*2.在二叉排序树T中查找最大值,返回该结点*/
pTree findMax(pTree T)
{
if(T==NULL)return T;
while(1){
if(T->rchild!=NULL) T = T->rchild;
else return T;
}
return T;
}
/*3.在二叉排序树T中查找指定数据元素,若未找到,则返回NULL*/
pTree findData(pTree T, int element)
{
while(1){
if(T==NULL)return T;
if(element<T->data) T = T->lchild;
else if(element>T->data) T = T->rchild;
else if(element==T->data) return T;
}
return T;
}
int main()
{
int a ;
pTree T =NULL;
int num;
scanf("%d",&num);
for(int i=0;i<num;i++)
{
scanf("%d",&a);
T = insertData(a, T);
}
pTree p = findMin(T);
if(p)
printf("%d\n",p->data);
else
printf("未找到\n");
p = findMax(T);
if(p)
printf("%d\n",p->data);
else
printf("未找到\n");
scanf("%d",&a);
p = findData(T,a);
if(p)
printf("%d\n",p->data);
else
printf("未找到指定元素\n");
}
这题没有完全实现好,完全面向结果编程了,数据量一大应该就过不了,但是这题头歌是可以过的,不大想搞这个了,今天有点摆,心情烦躁。
#include <stdio.h>
#include <stdlib.h>
//数据结构定义
struct node
{
int data ;
struct node *lchild ,*rchild ;
};
typedef struct node *pTree;
/*第一关*/
/*1.创建一个二叉树结点,值为element*/
pTree createTreeNode(int element)
{
pTree node = (pTree)malloc(sizeof(struct node));
node->data = element;
node->lchild = NULL;
node->rchild = NULL;
return node;
}
/*2.在二叉排序树中插入一个数据元素,若二叉树为空,则新建根节点*/
pTree insertData(int x , pTree T)
{
if(T==NULL){
T = createTreeNode(x);
return T;
}
pTree p = T;
while(1){
if(x<p->data&&p->lchild==NULL){
p->lchild = createTreeNode(x);
return T;
}
else if(x>p->data&&p->rchild==NULL){
p->rchild = createTreeNode(x);
return T;
}
if(x==p->data) return T;
if(x<p->data) p = p->lchild;
else p = p->rchild;
}
return T;
}
/*3.先序遍历和中序遍历函数*/
void preOrder( pTree T)
{
if(T==NULL) return;
printf("%d ",T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
void inOrder( pTree T)
{
if(T==NULL)return;
inOrder(T->lchild);
printf("%d ",T->data);
inOrder(T->rchild);
}
/*第二关*/
/*1.在二叉排序树T中查找最小值,返回该结点*/
pTree findMin(pTree T)
{
if(T==NULL)return T;
while(1){
if(T->lchild!=NULL){
T = T->lchild;
}else return T;
}
return T;
}
/*2.在二叉排序树T中查找最大值,返回该结点*/
pTree findMax(pTree T)
{
if(T==NULL)return T;
while(1){
if(T->rchild!=NULL) T = T->rchild;
else return T;
}
return T;
}
/*3.在二叉排序树T中查找指定数据元素,若未找到,则返回NULL*/
pTree findData(pTree T, int element)
{
while(1){
if(T==NULL)return NULL;
if(element<T->data) T = T->lchild;
else if(element>T->data) T = T->rchild;
else if(element==T->data) return T;
if(T==NULL)return NULL;
}
return T;
}
pTree findDataFather(pTree T,int element){
while(1){
if(T==NULL)return NULL;
if(T->data > element){
if(T->lchild!=NULL)
if(T->lchild->data==element) return T;
T = T->lchild;
}
if(T->data < element){
if(T->rchild!=NULL)
if(T->rchild->data==element) return T;
T = T->rchild;
}
}
}
/*第三关*/
//在二叉排序树T中
pTree deleteData(pTree T,int element)
{
pTree p = findData(T, element);
if(p==NULL)return NULL;
if(T->data==element){
T->data = T->rchild->data;
T->rchild = T -> rchild -> rchild;
return T;
}
pTree q = findDataFather(T, element);
q -> rchild = p->rchild;
return T;
}