Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第七次作业提交 #148

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions 2017-1/HuanWoWeiLan/第九次作业/第九次作业.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int key;
struct node *lchild, *rchild;
}BSTNode, *BSTree;

//����
int InsertBST(BSTree *bst, int k)
{
BSTree r, s, pre;
r = (BSTree)malloc(sizeof(BSTNode));
r->key = k;
r->lchild = NULL;
r->rchild = NULL;
if(*bst == NULL)
{
*bst = r;
return 1;
}
pre = NULL;
s = *bst;
while(s)
{
if(k == s->key)
return 0;
else if(k < s->key)
{
pre = s;
s = s->lchild;
}
else
{
pre = s;
s = s->rchild;
}
}
if(k < pre->key)
pre->lchild = r;
else
pre->rchild = r;
return 1;
}
BSTree DeleteBST(BSTree T, int k)
{
BSTree p, q, s, f;
p = T;
f = NULL;
while (p)
{
if(p->key == k)
break;
f = p;
if(p->key > k)
p =p->lchild;
else if(p->key< k)
p =p->rchild;

}

q = p;

if ((p->lchild)&& (p->rchild))
{
s =p->lchild;
while(s->rchild)
{
q =s;
s =s->rchild;
}
p->key =s->key;
if (p != q)

q->rchild= s->lchild;

else if (p == q)
q->lchild= s->lchild;
free(s);
return T;
}
else if (!p->lchild)

p =p->rchild;

else if (!p->rchild)
p =p->lchild;
if (!f)

T = p;

else if (q == f->lchild)
f->lchild= p;
else
f->rchild= p;

free(q);
return T;

}

void CreateBST(BSTree *bst)
{
int key;
*bst = NULL;
scanf("%d", &key);
while(key != -1)
{
InsertBST(bst, key);
scanf("%d", &key);
}
}
/*��ӡ���������������*/
void InOrder(BSTree root)
{
if(root != NULL)
{
InOrder(root->lchild);
printf( "%d ", root->key);
InOrder(root->rchild);
}
}

/*����*/
BSTree SearchBST(BSTree bst, int key)
{
BSTree q;
q = bst;
//�ݹ�
while(q)
{
if(q->key == key)
return q;
if(q->key > key)
q=q->lchild;
else
q=q->rchild;
}
return NULL; //����ʧ��
}

int main()
{
BSTree T;
int tag = 1;
int m, n;
printf("����������������������������-1������");
CreateBST(&T);
printf("���������������������");
InOrder(T);
while(tag != 0)
{
printf("��������Ҫ���ҵ�Ԫ��:");
scanf("%d", &n);
Copy link
Collaborator

@chenfucaijun chenfucaijun Jun 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 建议自动输入二叉树的数据,测试输入数据序列:{ 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }, 待查找关键字依次为: { 13, 8, 5, 20, 6 }
  • 你的源程序名又变成中文了,要时刻注意细节哦!

if(SearchBST(T, n) == NULL)
{
printf("��Ǹ����ʧ��!");
InsertBST(&T, n);
}
else
{
printf("���ҳɹ������ҵ�����Ϊ%d\n", SearchBST(T,n)->key);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 请看作业要求: 在本次PR中,应包含采用纯文本文件的方式保存的查找表使用上述测试用例产生的程序输出结果。

DeleteBST(SearchBST(T,n),n);
}
InOrder(T);
printf("\n�Ƿ�������� �� ���밴 1 ���� 0:");
scanf("%d", &tag);
}
return 0;
}