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

提交图的最短路径 #162

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file added 2017-1/Micylt/hw8/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions 2017-1/Micylt/hw8/BSTOutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • 程序中没有看到C语言文件流相关操作,这个结果输出是手工复制的吗? 建议使用C语言文件操作,自动把查找结果输出到文件中

Copy link
Contributor Author

Choose a reason for hiding this comment

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

= =,不好意思理解错了,结果确实是自己复制的.... 修改后会重新提交


8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30

7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30

7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30

7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30

7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30
130 changes: 130 additions & 0 deletions 2017-1/Micylt/hw8/BSTree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include"BSTree.h"
int max = 0;

bool EQ(KeyType kval, KeyType data) {
if (kval == data) {
return true;
}
else {
return false;
}
}

bool LT(KeyType kval, KeyType data) {
if (kval != data && kval < data) {
return true;
}
else {
return false;
}
}

Status preOrderTraverse(BiTree T) {
if (T) {
printf("%d", T->data);
if (T->data != max) {
printf(", ");
}
preOrderTraverse(T->lchild);
preOrderTraverse(T->rchild);
return OK;
}
else {
return ERROR;
}
}

bool SearchBST(BiTree T, KeyType kval, BiTree f, BiTree *p) {
if (!T) {
*p = f;
return false;
}
else if (EQ(kval, T->data)) {
*p = T;
return true;
}
else if (LT(kval, T->data)) {
return SearchBST(T->lchild, kval, T, p);
}
else {
return SearchBST(T->rchild, kval, T, p);
}
}

bool InsertBST(BiTree *T, KeyType e, BiTree p) {
BiTree s = NULL;
if (max < e)
{
max = e;
}
if (!(s = (BiTNode *)malloc(sizeof(BiTNode)))) {
exit(OVERFLOW);
}
if (!(p = (BiTNode *)malloc(sizeof(BiTNode)))) {
exit(OVERFLOW);
}
if (!SearchBST(*T, e, NULL, &p)) {
s->data = e;
s->lchild = s->rchild = NULL;
if (!p) {
*T = s;
}
else if (LT(e, p->data)) {
p->lchild = s;
}
else {
p->rchild = s;
}
return true;
}
else {
return false;
}
}

Status Delete(BiTree *p) {
BiTree q = NULL;
BiTree s = NULL;
if (!(*p)->rchild) {
q = *p;
(*p) = (*p)->lchild;
}
else if (!(*p)->lchild) {
q = *p;
(*p) = (*p)->rchild;
}
else {
q = *p;
s = (*p)->lchild;
while (s->rchild) {
q = s;
s = s->rchild;
}
(*p)->data = s->data;
if (q != (*p)) {
q->rchild = s->lchild;
}
else {
q->rchild = s->lchild;
}
}
return OK;
}

bool DeleteBST(BiTree *T, KeyType kval) {
if (!T) {
return false;
}
else {
if (EQ(kval, (*T)->data)) {
Delete(T);
return true;
}
else if (LT(kval, (*T)->data)) {
return DeleteBST(&(*T)->lchild, kval);
}
else {
return DeleteBST(&(*T)->rchild, kval);
}
}
}
28 changes: 28 additions & 0 deletions 2017-1/Micylt/hw8/BSTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>

typedef int KeyType;

typedef enum
{
false,
true,
}bool;
typedef enum {
OK,
OVERFLOW,
ERROR,
} Status;
typedef struct BiTNode
{
int data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

bool EQ(KeyType kval, KeyType data);
bool LT(KeyType kval, KeyType data);
Status preOrderTraverse(BiTree T);
bool SearchBST(BiTree T, KeyType kval, BiTree f, BiTree *p);
bool InsertBST(BiTree *T, KeyType e, BiTree p);
Status Delete(BiTree *p);
bool DeleteBST(BiTree *T, KeyType kval);
32 changes: 32 additions & 0 deletions 2017-1/Micylt/hw8/main1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include"BSTree.h"

int main() {
char c[30] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 };
char c1[10] = { 13, 8, 5, 20, 6 };
int i;
bool flag = 0;
BiTree T = NULL;
BiTree p = NULL;

for (i = 0; i<12; i++)
{
InsertBST(&T, c[i], p);
}

preOrderTraverse(T);
printf("\n\n");

for (i = 0; i<5; i++) //5����������ij���
{
flag = SearchBST(T, c1[i], NULL, &p);
if (flag == true) {
DeleteBST(&T, c1[i]);
}
else {
InsertBST(&T, c1[i], p);
}
preOrderTraverse(T);
printf("\n\n");
}
return 0;
}