mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-06-16 15:07:38 +08:00
更新图
This commit is contained in:
43
Code/Code/head/graph.h
Normal file
43
Code/Code/head/graph.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "head.h"
|
||||
|
||||
// 邻接矩阵图
|
||||
typedef struct {
|
||||
// 顶点表
|
||||
element_type* vex;
|
||||
// 边表
|
||||
weight_type *edge;
|
||||
// 定点数
|
||||
int vex_length;
|
||||
// 边数
|
||||
int edge_length;
|
||||
} AdjacentArrayGraph;
|
||||
|
||||
// 邻接表图
|
||||
// 边表结点
|
||||
typedef struct EdgeNode{
|
||||
// 该弧指向顶点的位置
|
||||
int vex;
|
||||
// 指向下条弧的指针
|
||||
struct EdgeNode *next;
|
||||
// 边权值
|
||||
weight_type weigh;
|
||||
} EdgeNode;
|
||||
|
||||
// 顶点表结点
|
||||
typedef struct VexNode{
|
||||
// 顶点信息
|
||||
element_type data;
|
||||
.. 指向第一条依赖该顶点的弧的指针
|
||||
EdgeNode* frist;
|
||||
} VexNode, *AdjacentList;
|
||||
|
||||
// 邻接表
|
||||
typedef struct {
|
||||
// 邻接表数据
|
||||
AdjacentList data;
|
||||
// 顶点数
|
||||
int vex_length;
|
||||
// 边数
|
||||
int edge_length;
|
||||
} AdjacentListGraph;
|
||||
|
||||
@@ -7,4 +7,11 @@
|
||||
// 定义串块链数据长度
|
||||
#define DATASIZE 4
|
||||
// 定义默认数据类型
|
||||
typedef char element_type;
|
||||
typedef char element_type;
|
||||
// 权值类型
|
||||
typedef int weight_type;
|
||||
|
||||
// 比较元素方法
|
||||
int CompareElem(element_type elem1, element_type elem2){
|
||||
return 0;
|
||||
}
|
||||
@@ -9,9 +9,9 @@ typedef struct BinaryTreeNode {
|
||||
} BinaryTreeNode, *BinaryTree;
|
||||
|
||||
// 前序遍历
|
||||
bool PreOrderBinaryTree(BinaryTree &tree, bool( *function)(BinaryTree&)){
|
||||
if(tree!= nullptr){
|
||||
if(!function(tree))
|
||||
bool PreOrderBinaryTree(BinaryTree &tree, bool( *function)(BinaryTree &)) {
|
||||
if (tree != nullptr) {
|
||||
if (!function(tree))
|
||||
return false;
|
||||
PreOrderBinaryTree(tree->left_child, function);
|
||||
PreOrderBinaryTree(tree->right_child, function);
|
||||
@@ -20,10 +20,10 @@ bool PreOrderBinaryTree(BinaryTree &tree, bool( *function)(BinaryTree&)){
|
||||
}
|
||||
|
||||
// 中序遍历
|
||||
bool InOrderBinaryTree(BinaryTree &tree, bool(* function)(BinaryTree&)){
|
||||
if(tree!= nullptr){
|
||||
bool InOrderBinaryTree(BinaryTree &tree, bool(*function)(BinaryTree &)) {
|
||||
if (tree != nullptr) {
|
||||
PreOrderBinaryTree(tree->left_child, function);
|
||||
if(!function(tree))
|
||||
if (!function(tree))
|
||||
return false;
|
||||
PreOrderBinaryTree(tree->right_child, function);
|
||||
}
|
||||
@@ -31,28 +31,28 @@ bool InOrderBinaryTree(BinaryTree &tree, bool(* function)(BinaryTree&)){
|
||||
}
|
||||
|
||||
// 后序遍历
|
||||
bool PostOrderBinaryTree(BinaryTree &tree, bool(* function)(BinaryTree&)){
|
||||
if(tree!= nullptr){
|
||||
bool PostOrderBinaryTree(BinaryTree &tree, bool(*function)(BinaryTree &)) {
|
||||
if (tree != nullptr) {
|
||||
PreOrderBinaryTree(tree->left_child, function);
|
||||
PreOrderBinaryTree(tree->right_child, function);
|
||||
if(!function(tree))
|
||||
if (!function(tree))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 非递归先序遍历
|
||||
bool PreOrderNonRecursiveBinaryTree(BinaryTree &tree, bool(* function)(BinaryTree&)){
|
||||
bool PreOrderNonRecursiveBinaryTree(BinaryTree &tree, bool(*function)(BinaryTree &)) {
|
||||
// 初始化栈,栈元素为tree指针
|
||||
std::stack<BinaryTree> stack;
|
||||
// 赋值一个新树
|
||||
BinaryTree new_tree = tree;
|
||||
// 如果栈不空或new_tree不空时循环
|
||||
while(new_tree||!stack.empty()){
|
||||
while (new_tree || !stack.empty()) {
|
||||
// 一直向左
|
||||
if(new_tree){
|
||||
if (new_tree) {
|
||||
// 访问当前结点
|
||||
if(!function(new_tree)){
|
||||
if (!function(new_tree)) {
|
||||
printf("PreOrderNonRecursive:访问结点失败!");
|
||||
return false;
|
||||
}
|
||||
@@ -61,8 +61,8 @@ bool PreOrderNonRecursiveBinaryTree(BinaryTree &tree, bool(* function)(BinaryTre
|
||||
// 左孩子不空则一直向左
|
||||
new_tree = new_tree->left_child;
|
||||
}
|
||||
// 出栈,并转向右子树
|
||||
else{
|
||||
// 出栈,并转向右子树
|
||||
else {
|
||||
// 返回栈顶的元素,但不删除该元素
|
||||
new_tree = stack.top();
|
||||
// 删除栈顶元素但不返回其值
|
||||
@@ -76,28 +76,28 @@ bool PreOrderNonRecursiveBinaryTree(BinaryTree &tree, bool(* function)(BinaryTre
|
||||
}
|
||||
|
||||
// 非递归中序遍历
|
||||
bool InOrderNonRecursiveBinaryTree(BinaryTree &tree, bool(* function)(BinaryTree&)){
|
||||
bool InOrderNonRecursiveBinaryTree(BinaryTree &tree, bool(*function)(BinaryTree &)) {
|
||||
// 初始化栈,栈元素为tree指针
|
||||
std::stack<BinaryTree> stack;
|
||||
// 赋值一个新树
|
||||
BinaryTree new_tree = tree;
|
||||
// 如果栈不空或new_tree不空时循环
|
||||
while(new_tree||!stack.empty()){
|
||||
while (new_tree || !stack.empty()) {
|
||||
// 一直向左
|
||||
if(new_tree){
|
||||
if (new_tree) {
|
||||
// 当前结点入栈
|
||||
stack.push(new_tree);
|
||||
// 左孩子不空则一直向左
|
||||
new_tree = new_tree->left_child;
|
||||
}
|
||||
// 出栈,并转向右子树
|
||||
else{
|
||||
// 出栈,并转向右子树
|
||||
else {
|
||||
// 返回栈顶的元素,但不删除该元素
|
||||
new_tree = stack.top();
|
||||
// 删除栈顶元素但不返回其值
|
||||
stack.pop();
|
||||
// 访问出栈结点
|
||||
if(!function(new_tree)){
|
||||
if (!function(new_tree)) {
|
||||
printf("InOrderNonRecursive:访问结点失败!");
|
||||
return false;
|
||||
}
|
||||
@@ -110,30 +110,84 @@ bool InOrderNonRecursiveBinaryTree(BinaryTree &tree, bool(* function)(BinaryTree
|
||||
}
|
||||
|
||||
// 层序遍历
|
||||
bool LevelOrderBinaryTree(BinaryTree &tree, bool(* function)(BinaryTree&)){
|
||||
bool LevelOrderBinaryTree(BinaryTree &tree, bool(*function)(BinaryTree &)) {
|
||||
// 初始化辅助队列
|
||||
std::queue<BinaryTree> queue;
|
||||
// 初始化树结点
|
||||
BinaryTree new_tree=tree;
|
||||
BinaryTree new_tree = tree;
|
||||
// 根结点入队
|
||||
queue.push(new_tree);
|
||||
// 若队列非空则循环
|
||||
while(!queue.empty()){
|
||||
while (!queue.empty()) {
|
||||
// 队头结点出队
|
||||
new_tree = queue.front();
|
||||
queue.pop();
|
||||
// 访问出队结点
|
||||
if(!function(new_tree)){
|
||||
if (!function(new_tree)) {
|
||||
printf("LevelOrder:访问结点失败!");
|
||||
return false;
|
||||
}
|
||||
// 如果左子树不空则其根结点入队
|
||||
if(new_tree->left_child!= nullptr){
|
||||
if (new_tree->left_child != nullptr) {
|
||||
queue.push(new_tree);
|
||||
}
|
||||
if(new_tree->right_child!= nullptr){
|
||||
if (new_tree->right_child != nullptr) {
|
||||
queue.push(new_tree);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 二叉排序树查找
|
||||
BinaryTreeNode *SearchBinarySortTree(BinaryTree tree, element_type elem) {
|
||||
BinaryTree node = tree;
|
||||
// 若树空或等于根结点值就结束循环
|
||||
while (node != nullptr && elem != node->data) {
|
||||
// 若小于则左子树查找
|
||||
if (CompareElem(elem, node->data) < 0)
|
||||
node = node->left_child;
|
||||
else
|
||||
node = node->right_child;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
// 二叉排序树插入
|
||||
bool InsertBinarySortTree(BinaryTree &tree, element_type elem) {
|
||||
if (elem == NULL){
|
||||
printf("InsertBinarySortTree:插入数据不应为空!\n");
|
||||
return false;
|
||||
}
|
||||
// 若原树为空,则将新插入的记录作为根结点
|
||||
if (tree == nullptr) {
|
||||
tree = (BinaryTree) malloc(sizeof(BinaryTreeNode));
|
||||
tree->data = elem;
|
||||
tree->left_child = tree->right_child = nullptr;
|
||||
return true;
|
||||
}
|
||||
// 若存在相同关键字的结点则插入失败
|
||||
else if (CompareElem(elem, tree->data) == 0)
|
||||
return false;
|
||||
// 若小于则插入到左子树
|
||||
else if (CompareElem(elem, tree->data) < 0)
|
||||
return InsertBinarySortTree(tree->left_child, elem);
|
||||
// 否则插入到右子树
|
||||
else
|
||||
return InsertBinarySortTree(tree->right_child, elem);
|
||||
}
|
||||
|
||||
// 构造二叉排序树
|
||||
bool CreateBinarySortTree(BinaryTree &tree, element_type* elem, int start, int length){
|
||||
// 将树初始化
|
||||
tree = nullptr;
|
||||
int i = 0;
|
||||
while(i<length){
|
||||
if(!InsertBinarySortTree(tree,elem[start+i]))
|
||||
return false;
|
||||
i++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 删除二叉排序树
|
||||
bool DeleteBinarySortTree()
|
||||
@@ -119,7 +119,7 @@ bool CreatePreOrderThreadBinaryTree(ThreadBinaryTree tree) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 先序后历线索化
|
||||
// 后序遍历线索化
|
||||
bool PostOrderThreadBinaryTree(ThreadBinaryTree &tree, ThreadBinaryTree &pre) {
|
||||
if (tree != nullptr) {
|
||||
// 递归线索化左子树
|
||||
|
||||
29
Code/Code/head/tree.h
Normal file
29
Code/Code/head/tree.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "head.h"
|
||||
|
||||
// 双亲表示法
|
||||
// 结点
|
||||
typedef struct {
|
||||
element_type data;
|
||||
// 双亲位置
|
||||
int parent;
|
||||
} ParentTreeNode;
|
||||
// 数
|
||||
typedef struct {
|
||||
// 数组
|
||||
ParentTreeNode* data;
|
||||
// 长度
|
||||
int length;
|
||||
// 最大容量
|
||||
int max_length;
|
||||
} ParentTree;
|
||||
|
||||
// 孩子兄弟表示法
|
||||
// 结点
|
||||
typedef struct ChildSiblingTreeNode {
|
||||
// 数据
|
||||
element_type data;
|
||||
// 第一个孩子
|
||||
struct ChildSiblingTreeNode *frist_child;
|
||||
// 下一个兄弟
|
||||
struct ChildSiblingTreeNode *next_sibling;
|
||||
} ChildSiblingTreeNode, *ChildSiblingTree;
|
||||
@@ -13,6 +13,8 @@
|
||||
#include "../head/link_string.h"
|
||||
#include "../head/link_tree.h"
|
||||
#include "../head/thread_tree.h"
|
||||
#include "../head/tree.h"
|
||||
#include "../head/graph.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user