diff --git a/DataStructure/StaticSequenceList.h b/DataStructure/StaticSequenceList.h index 9e4dc76..f743d14 100644 --- a/DataStructure/StaticSequenceList.h +++ b/DataStructure/StaticSequenceList.h @@ -8,6 +8,8 @@ #ifndef StaticSequenceList_h #define StaticSequenceList_h +#include + // 静态顺序表 typedef struct { int data[10]; diff --git a/DataStructure/Tree.h b/DataStructure/Tree.h new file mode 100644 index 0000000..2d4961a --- /dev/null +++ b/DataStructure/Tree.h @@ -0,0 +1,167 @@ +// +// Tree.h +// DataStructure +// 树与二叉树 +// Created by Noth Amor on 2023/5/29. +// + +#ifndef Tree_h +#define Tree_h + +#include +#include + +using namespace std; + +typedef struct BiTNode { + char data; + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree; + +// 二叉树初始化 +void InitTree(BiTree &T, char value) { + // 申请根节点空间 + T = (BiTree) malloc(sizeof(BiTree)); + + // 赋默认值 + T->data = value; + + // 默认🈚️左右子节点 + T->lchild = NULL; + T->rchild = NULL; +} + +// 左插入子节点 +bool InsertLeftTreeNode(BiTNode* &T, char data) { + // 申请左子节点空间 + T->lchild = (BiTNode *) malloc(sizeof(BiTNode)); + // 申请空间失败处理 + if (T->lchild == NULL) { + return false; + } + + // 赋值 + T->lchild->data = data; + + // 默认无子节点 + T->lchild->lchild = NULL; + T->lchild->rchild = NULL; + + return true; +} + +// 右插入子节点 +bool InsertRightTreeNode(BiTNode* &T, char data) { + // 申请右子节点空间 + T->rchild = (BiTNode *) malloc(sizeof(BiTNode)); + if (T->rchild == NULL) { + return false; + } + + // 赋值 + T->rchild->data = data; + + // 子节点为空 + T->rchild->lchild = NULL; + T->rchild->rchild = NULL; + + return true; +} + +// 访问节点并输出节点值 +void Visit(BiTree T) { + cout << T->data << endl; +} + +// 递归先序遍历 +void PreOrder(BiTree T) { + // 如果节点非空 + if (T != NULL) { + // 访问顺序祖左右 + Visit(T); + PreOrder(T->lchild); + PreOrder(T->rchild); + } +} + +// 递归中序遍历 +void InOrder(BiTree T) { + // 如果节点非空 + if (T != NULL) { + // 访问顺序左祖右 + InOrder(T->lchild); + Visit(T); + InOrder(T->rchild); + } +} + +// 递归后序遍历 +void PostOrder(BiTree T) { + // 如果节点非空 + if (T != NULL) { + // 访问顺序左右祖 + PostOrder(T->lchild); + PostOrder(T->rchild); + Visit(T); + } +} + +// 递归求树深度 +int TreeDepth(BiTree T) { + if (T == NULL) { + return 0; + } else { + // 求左子树深度 + int l = TreeDepth(T->lchild); + + // 求右子树深度 + int r = TreeDepth(T->rchild); + + // 如果左子树深度大于右子树,就返回左子树深度,反之亦然 + // 深度+1是为了算上根节点的深度1 + return l > r ? l + 1 : r + 1; + } +} + +void TreeTest() { + // 创建二叉树 + BiTree T; + + // 初始化二叉树 + InitTree(T, 'A'); + + // 插入左子节点 + InsertLeftTreeNode(T, 'B'); + + // 插入右子节点 + InsertRightTreeNode(T, 'C'); + + // 输出三个节点的值 + // Visit(T); + // Visit(T->lchild); + // Visit(T->rchild); + + // 先序遍历 + cout << "先序遍历" << endl; + PreOrder(T); + + cout << endl; + + // 中序遍历 + cout << "中序遍历" << endl; + InOrder(T); + + cout << endl; + + // 后序遍历 + cout << "后序遍历" << endl; + PostOrder(T); + + cout << endl; + + // 树深度 + int depth = TreeDepth(T); + cout << "当前二叉树的深度为: " << depth << endl; +} + +#endif /* Tree_h */ diff --git a/DataStructure/main.cpp b/DataStructure/main.cpp index 93d941b..f9838e0 100644 --- a/DataStructure/main.cpp +++ b/DataStructure/main.cpp @@ -1,6 +1,7 @@ #include #include -#include "SString.h" +#include "Tree.h" +// #include "SString.h" // #include "StackPostFixExpression.h" // #include "LinkList.h" // #include "Stack.h" @@ -15,7 +16,8 @@ int main() { // bool state = StackBracketTest(); // printf("%d\n", state); // Test(); + // StringTest(); - StringTest(); + TreeTest(); return 0; } diff --git a/DataStructure/run b/DataStructure/run index 47b38a6..735135a 100755 Binary files a/DataStructure/run and b/DataStructure/run differ