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

求图最短路径 #144

Open
wants to merge 5 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
18 changes: 18 additions & 0 deletions 2017-1/hh-bo/第八次作业/BSTOutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
建立二叉树:
8 3 1 6 4 5 7 10 14 19 22 30
查找节点:13
该元素不存在,插入该节点,重新打印树
8 3 1 6 4 5 7 10 14 13 19 22 30
查找节点:8
该元素存在,删除该节点,重新打印树
10 3 1 6 4 5 7 14 13 19 22 30
查找节点:5
该元素存在,删除该节点,重新打印树
10 3 1 6 4 7 14 13 19 22 30
查找节点:20
该元素不存在,插入该节点,重新打印树
10 3 1 6 4 7 14 13 19 22 20 30
查找节点:6
该元素存在,删除该节点,重新打印树
10 3 1 7 4 14 13 19 22 20 30
Program ended with exit code: 0
Copy link
Collaborator

@cucyoyo cucyoyo Jun 1, 2017

Choose a reason for hiding this comment

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

注意按照作业要求打印输出,数字之间使用", "分隔

185 changes: 185 additions & 0 deletions 2017-1/hh-bo/第八次作业/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
//
// main.c
// homework8
//
// Created by Huhongbo on 2017/5/21.
// Copyright © 2017年 Huhongbo. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>

//树的结构定义
typedef struct treeNode
{
int data;
struct treeNode *left;
struct treeNode *right;

}treeNode;

//最小元素查找
treeNode* FindMin(treeNode *node)
{
if (node == NULL)
{
return NULL;
}
if (node->left)
return FindMin(node->left);
else
return node;
}

//最大元素查找
treeNode* FindMax(treeNode *node)
{
if (node == NULL)
{
return NULL;
}
if (node->right)
return FindMax(node->right);
else
return node;
}

//插入数据
treeNode * Insert(treeNode *node, int data)
{
if (node == NULL)
{
treeNode *temp;
temp = (treeNode *)malloc(sizeof(treeNode));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
if (data >(node->data))
{
node->right = Insert(node->right, data);
}
else if (data < (node->data))
{
node->left = Insert(node->left, data);
}
return node;
}

//删除数据
treeNode * Delete(treeNode *node, int data)
{
treeNode *temp;
if (node == NULL)
{
printf("Element Not Found");
}
else if (data < node->data)
{
node->left = Delete(node->left, data);
}
else if (data > node->data)
{
node->right = Delete(node->right, data);
}
else
{
if (node->right && node->left)
{
temp = FindMin(node->right);
node->data = temp->data;
node->right = Delete(node->right, temp->data);
}
else
{
temp = node;
if (node->left == NULL)
node = node->right;
else if (node->right == NULL)
node = node->left;
free(temp);
}
}
return node;

}

//查找数据
treeNode * Find(treeNode *node, int data)
{
if (node == NULL)
{
return NULL;
}
if (data > node->data)
{
return Find(node->right, data);
}
else if (data < node->data)
{
return Find(node->left, data);
}
else
{
return node;
}

}



//先序遍历
void PrintPreorder(treeNode *node)
{
if (node == NULL)
{
return;
}
printf("%d ", node->data);
PrintPreorder(node->left);
PrintPreorder(node->right);
}


int main()
{
treeNode *root = NULL;
root = Insert(root, 8);
root = Insert(root, 10);
root = Insert(root, 14);
root = Insert(root, 3);
root = Insert(root, 1);
root = Insert(root, 6);
root = Insert(root, 4);
root = Insert(root, 7);
root = Insert(root, 5);
root = Insert(root, 19);
root = Insert(root, 22);
root = Insert(root, 30);
Copy link
Collaborator

Choose a reason for hiding this comment

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

建议使用一个创建树的函数,统一进行树的初始化插入操作

printf("建立二叉树:\n");
PrintPreorder(root);
printf("\n");
//待查找关键字
int a[] = { 13,8,5,20,6 };
treeNode * temp;
for (int i = 0; i < 5; i++)
{
temp = Find(root, a[i]);
printf("查找节点:");
printf("%d\n", a[i]);
if (temp == NULL)
{
printf("该元素不存在,插入该节点,重新打印树\n");
Insert(root, a[i]);
PrintPreorder(root);
printf("\n");
}
else
{
printf("该元素存在,删除该节点,重新打印树\n");
Delete(root, a[i]);
PrintPreorder(root);
printf("\n");
}
}

}