From 5bdb044421ef4fe15932b64048266d97b8870d87 Mon Sep 17 00:00:00 2001 From: e2hang <2099307493@qq.com> Date: Wed, 6 Aug 2025 16:22:28 +0800 Subject: [PATCH] BinaryTree --- LinearList/BinaryTree/CMakeLists.txt | 9 ++ LinearList/BinaryTree/CMakeList的具体写法 | 0 LinearList/BinaryTree/binaryTree.h | 22 ++++ LinearList/BinaryTree/binaryTreeNode.h | 22 ++++ LinearList/BinaryTree/linkedBinaryTree.h | 117 ++++++++++++++++++++++ LinearList/BinaryTree/main.cpp | 55 ++++++++++ 6 files changed, 225 insertions(+) create mode 100644 LinearList/BinaryTree/CMakeLists.txt create mode 100644 LinearList/BinaryTree/CMakeList的具体写法 create mode 100644 LinearList/BinaryTree/binaryTree.h create mode 100644 LinearList/BinaryTree/binaryTreeNode.h create mode 100644 LinearList/BinaryTree/linkedBinaryTree.h create mode 100644 LinearList/BinaryTree/main.cpp diff --git a/LinearList/BinaryTree/CMakeLists.txt b/LinearList/BinaryTree/CMakeLists.txt new file mode 100644 index 0000000..70b3e83 --- /dev/null +++ b/LinearList/BinaryTree/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.31) +project(BTree) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(BTree main.cpp + binaryTreeNode.h + binaryTree.h + linkedBinaryTree.h) diff --git a/LinearList/BinaryTree/CMakeList的具体写法 b/LinearList/BinaryTree/CMakeList的具体写法 new file mode 100644 index 0000000..e69de29 diff --git a/LinearList/BinaryTree/binaryTree.h b/LinearList/BinaryTree/binaryTree.h new file mode 100644 index 0000000..ee436de --- /dev/null +++ b/LinearList/BinaryTree/binaryTree.h @@ -0,0 +1,22 @@ +// +// Created by PC on 25-8-6. +// + +#ifndef BINARYTREE_H +#define BINARYTREE_H +#include "binaryTreeNode.h" + +template +class binaryTree { +public: + virtual ~binaryTree() = default; + virtual bool empty() const = 0; + virtual int size() const = 0; + virtual void preOrder(void(*) (binaryTreeNode*) ) const = 0; + virtual void inOrder(void(*) (binaryTreeNode*) ) const = 0; + virtual void postOrder(void(*) (binaryTreeNode*) ) const = 0; + virtual void levelOrder(void(*) (binaryTreeNode*) ) const = 0; +}; + + +#endif //BINARYTREE_H diff --git a/LinearList/BinaryTree/binaryTreeNode.h b/LinearList/BinaryTree/binaryTreeNode.h new file mode 100644 index 0000000..05791a9 --- /dev/null +++ b/LinearList/BinaryTree/binaryTreeNode.h @@ -0,0 +1,22 @@ +// +// Created by PC on 25-8-6. +// + +#ifndef BINARYTREENODE_H +#define BINARYTREENODE_H + +template +class binaryTreeNode { +public: + T element; + binaryTreeNode* left; + binaryTreeNode* right; +public: + binaryTreeNode() {left = right = nullptr;} + explicit binaryTreeNode(const T& e) : element(e), left(nullptr), right(nullptr) {} + binaryTreeNode(const T& e, binaryTreeNode* l, binaryTreeNode* r) : element(e), left(l), right(r) {} + ~binaryTreeNode() = default; + +}; + +#endif //BINARYTREENODE_H diff --git a/LinearList/BinaryTree/linkedBinaryTree.h b/LinearList/BinaryTree/linkedBinaryTree.h new file mode 100644 index 0000000..ae24703 --- /dev/null +++ b/LinearList/BinaryTree/linkedBinaryTree.h @@ -0,0 +1,117 @@ +// +// Created by PC on 25-8-6. +// + +#ifndef LINKEDBINARYTREE_H +#define LINKEDBINARYTREE_H +#include +#include +#include + +#include "binaryTree.h" +#include "binaryTreeNode.h" +template +class linkedBinaryTree : public binaryTree { +private: + static std::deque *> nodes; + binaryTreeNode* root; + int tsize; + static void (*visit)(binaryTreeNode* node);//存储visit + static void preOrder(binaryTreeNode* node) { + if (node == nullptr) return; + + bool isLeaf = (node->left == nullptr && node->right == nullptr); + if (!isLeaf) std::cout << "("; + + visit(node); + preOrder(node->left); + preOrder(node->right); + + if (!isLeaf) std::cout << ")"; + } + + static void inOrder(binaryTreeNode* node) { + if (node == nullptr) return; + + // 对非叶子节点加括号 + bool isLeaf = (node->left == nullptr && node->right == nullptr); + if (!isLeaf) std::cout << "("; + + inOrder(node->left); + visit(node); + inOrder(node->right); + + if (!isLeaf) std::cout << ")"; + } + + static void postOrder(binaryTreeNode* node) { + if (node == nullptr) return; + + bool isLeaf = (node->left == nullptr && node->right == nullptr); + if (!isLeaf) std::cout << "("; + + postOrder(node->left); + postOrder(node->right); + visit(node); + + if (!isLeaf) std::cout << ")"; +} + + //层序遍历levelOrder,使用deque,这人好聪明! + static void levelOrder(binaryTreeNode* node) { + std::deque*> tmp; + tmp.push_back(node); + nodes.push_back(node); + //开始 + while (!tmp.empty()) { + binaryTreeNode* current = tmp.front(); + tmp.pop_front(); + + if (current->left != nullptr) { + nodes.push_back(current->left); + tmp.push_back(current->left); + } + if (current->right != nullptr) { + nodes.push_back(current->right); + tmp.push_back(current->right); + } + } + for (binaryTreeNode* x : nodes) { + std::cout << x->element; + } + } + + static void dispose(binaryTreeNode* node) { + if (node == nullptr) return; + dispose(node->left); + dispose(node->right); + std::cout << "disposing " << node->element << std::endl; + delete node; + } +public: + linkedBinaryTree() {root = nullptr;tsize = 0;} + explicit linkedBinaryTree(binaryTreeNode* x ) {root = x;tsize = 1;} + ~linkedBinaryTree() override {erase();} + bool empty() const override {return tsize == 0;} + void preOrder(void(*Ivisit) (binaryTreeNode*)) const override {visit = Ivisit; preOrder(root);} + //传入的是一个函数,在private里有void (*visit)来储存函数入口,从而在遍历的时候进行操作(输出,计算等) + //操作的函数是visit,即访问到该node之后进行什么操作 + void inOrder(void(*Ivisit) (binaryTreeNode*)) const override{visit = Ivisit; inOrder(root);} + void postOrder(void(*Ivisit) (binaryTreeNode*)) const override {visit = Ivisit; postOrder(root);} + void levelOrder(void(*Ivisit) (binaryTreeNode*)) const override {visit = Ivisit; levelOrder(root);} + int size() const override {return tsize;} + void erase() { + //postOrder(dispose); + dispose(root); + root = nullptr; + tsize = 0; + } +}; + +template +void (*linkedBinaryTree::visit)(binaryTreeNode*) = nullptr; + +template +std::deque *> linkedBinaryTree::nodes; + +#endif //LINKEDBINARYTREE_H diff --git a/LinearList/BinaryTree/main.cpp b/LinearList/BinaryTree/main.cpp new file mode 100644 index 0000000..f91033f --- /dev/null +++ b/LinearList/BinaryTree/main.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include "linkedBinaryTree.h" +using namespace std; +vector m; +void func(binaryTreeNode* node) { + cout << node->element; +} + +int main() { + /* + * x + * + - + * a b c d + */ + //构建要从下往上构建,注意如果是 new了一个[],必须delete []而不能直接delete t[i] + auto t0 = new binaryTreeNode("a"); + auto t1 = new binaryTreeNode("b"); + auto t2 = new binaryTreeNode("c"); + auto t3 = new binaryTreeNode("d"); + auto t4 = new binaryTreeNode("+", t0, t1); + auto t5 = new binaryTreeNode("-", t2, t3); + auto t6 = new binaryTreeNode("x", t4, t5); + linkedBinaryTree tree(t6); + + cout << endl; + tree.inOrder(func); + cout << endl; + + tree.preOrder(func); + cout << endl; + + tree.postOrder(func); + cout << endl; + + tree.levelOrder(func); + cout << endl; + return 0; +} + +/* +Output: +((a+b)x(c-d)) +(x(+ab)(-cd)) +((ab+)(cd-)x) +x+-abcd +disposing a +disposing b +disposing + +disposing c +disposing d +disposing - +disposing x + */ \ No newline at end of file