Binary Tree

#include <iostream>
using namespace std;

class BinaryTree {
    private:
        struct treenode{
            int key;
            treenode *left, *right;
            treenode (int k, treenode *l = 0, treenode *r = 0)
                : key(k), left(l), right(r) {}
        };  //  treenode
        treenode * head;
    public:
        BinaryTree() : head(NULL) {}
        //  destructor
        ~BinaryTree() {
            clear(head);
        }
        const treenode * getHead() const {
            return head;
        }
        void clear(treenode * t) {
            if (t) {
                clear(t->left);
                clear(t->right);
                delete t;
            }
        }

        //  insert a new node with some key into the tree
        void insert(int k);
        //  remove a node with some key
        void remove(int k);
        //  return max key
        int max() const;
        //  return min key
        int min() const;
        //  search wheather there is some key existing
        bool search(int k) const;
        //  print the key in preorder
        void preorder(const treenode * t) const;
        //  print the key in inorder
        void inorder(const treenode * t) const;
        //  print the key in postorder
        void postorder(const treenode * t) const;
};

void BinaryTree::insert(int k) {
    if (!head) {
        head = new treenode(k);
        return;
    }  //  if head is null, creat a head with the key
    treenode * temp = head;
    while (1) {  //  find a proper position that is empty where we place k
        if (k < temp->key) {
            if (temp->left) {
                temp = temp->left;
            } else {
                temp->left = new treenode(k, 0, 0);
                break;
            }
        } else {
            if (temp->right) {
                temp = temp->right;
            } else {
                temp->right = new treenode(k, 0, 0);
                break;
            }
        }
    }
}

void BinaryTree::remove(int k) {
    //  temp is the node to be delete
    treenode *temp = head, *parent = head;
    while (temp && temp->key != k) {
        parent = temp;
        if (k < temp->key)
            temp = temp->left;
        else
            temp = temp->right;
    }  //  find the treenode to be delete, name it temp

    if (!temp->left) {  //  if temp has no left treenode
        if (parent == head) {
            head = temp->right;
            delete temp;
        } else if (parent->left == temp) {
            parent->left = temp->right;
        } else {
            parent->right = temp->right;
        }
    } else if (!temp->right) {  //  if temp has no right treenode
        if (parent == head) {
            head = temp->left;
            delete temp;
        } else if (parent->left == temp) {
            parent->left = temp->left;
        } else {
            parent->right = temp->left;
        }
    } else {
        //  find successor and its parent
        treenode *successor = temp, *p = temp;
        while (successor->left) {
            p = successor;
            successor = successor->left;
        }
        //  replace temp with successor
        temp->key = successor->key;
        //  let successor's right become successor's parent's child
        if (p->left == successor) {
            p->left = successor->right;
        } else {
            p->right = successor->right;
        }
        delete successor;
    }
}

int BinaryTree::max() const {  //  the rightest element is the minimum
    if (head) {
        treenode * temp = head;
        while (temp->right) {
            temp = temp->right;
        }
        return temp->key;
    }
}

int BinaryTree::min() const {  //  the leftest element is the minimum
    if (head) {
        treenode * temp = head;
        while (temp->left) {
            temp = temp->left;
        }
        return temp->key;
    }
}

bool BinaryTree::search(int k) const {
    treenode * temp = head;
    while (temp) {
        if (k == temp->key) return true;  //  find it and return true
        if (k < temp->key)
            temp = temp->left;
        else
            temp = temp->right;
    }
    return false;  //  if k not exists, return false
}

void BinaryTree::preorder(const treenode * t) const {
    if (t) {
        cout << t->key << " ";
        preorder(t->left);
        preorder(t->right);
    }
}

void BinaryTree::inorder(const treenode * t) const {
    if (t) {
        inorder(t->left);
        cout << t->key << " ";
        inorder(t->right);
    }
}

void BinaryTree::postorder(const treenode * t) const {
    if (t) {
        postorder(t->left);
        postorder(t->right);
        cout << t->key << " ";
    }
}


//main function for test
int main() {
    BinaryTree test;
    int arr[10] = {9, -2, 3, 10, 4, 26, -9, 0, 5, 11};
    for (int i = 0; i < 10; i++)
        test.insert(arr[i]); 

    cout << arr[2] << " is in the tree?"
         << (test.search(arr[2]) ? " Yes." : " No.") << endl;

    test.preorder(test.getHead());
    cout << endl; 

    test.inorder(test.getHead());
    cout << endl;

    test.postorder(test.getHead());
    cout << endl;
    /*       9                      9
       -2        10           -2         10
     -9   3    11  26  ->  -9    4     11  26
         0 4                    0 5
            5                                */
    test.remove(arr[2]);  //  arr[2] == 3
    cout << arr[2] << " is in the tree?"
         << (test.search(arr[2]) ? " Yes." : " No.") << endl;

    test.inorder(test.getHead());
    cout << endl;

    test.remove(arr[7]);  //  arr[7] == 0
    test.inorder(test.getHead());
    cout << endl;

    return 0;
}

Reasult :
3 is in the tree? Yes.
9 -2 -9 3 0 4 5 10 26 11
-9 -2 0 3 4 5 9 10 11 26
-9 0 5 4 3 -2 11 26 10 9
3 is in the tree? No.
-9 -2 0 4 5 9 10 11 26
-9 -2 4 5 9 10 11 26


Process exited after 0.01723 seconds with return value 0
请按任意键继续…

当前,全球经济格局深刻调整,数字化浪潮席卷各行各业,智能物流作为现代物流发展的必然趋势和关键支撑,正迎来前所未有的发展机遇。以人工智能、物联网、大数据、云计算、区块链等前沿信息技术的快速迭代与深度融合为驱动,智能物流不再是传统物流的简单技术叠加,而是正在经历一场从自动化向智能化、从被动响应向主动预测、从信息孤岛向全面互联的深刻变革。展望2025年,智能物流系统将不再局限于提升效率、降低成本的基本目标,而是要构建一个感知更全面、决策更精准、执行更高效、协同更顺畅的智慧运行体系。这要求我们必须超越传统思维定式,以系统化、前瞻性的视角,全面规划和实施智能物流系统的建设。本实施方案正是基于对行业发展趋势的深刻洞察和对未来需求的精准把握而制定。我们的核心目标在于:通过构建一个集成了先进感知技术、大数据分析引擎、智能决策算法和高效协同平台的综合智能物流系统,实现物流全链路的可视化、透明化和智能化管理。这不仅是技术层面的革新,更是管理模式和服务能力的全面提升。本方案旨在明确系统建设的战略方向、关键任务、技术路径和实施步骤,确保通过系统化部署,有效应对日益复杂的供应链环境,提升整体物流韧性,优化资源配置效率,降低运营成本,并最终为客户创造更卓越的价值体验。我们致力于通过本方案的实施,引领智能物流迈向更高水平,为构建现代化经济体系、推动高质量发展提供强有力的物流保障。
<think>我们被要求详细介绍二叉树(binary tree)。结合用户提供的引用内容,我们可以从以下几个方面展开: 1. 二叉树的定义和基本概念(包括节点、根、子树等) 2. 二叉树的性质(如第i层最多有2^(i-1)个节点,深度为k的二叉树最多有2^k-1个节点等) 3. 特殊类型的二叉树(满二叉树、完全二叉树、完美二叉树等,注意引用[3]中提到的术语区别) 4. 二叉树的存储结构(通常用链式存储,如引用[1][2][4]中的节点结构) 5. 二叉树的遍历(深度优先:前序、中序、后序;广度优先:层次遍历) 6. 二叉树的构建(如引用[1]中的手动构建和引用[4]中的插入构建) 7. 二叉树的应用 由于用户要求详细,我们将每个部分都进行适当的展开。 注意:用户提供的引用中,引用[3]提到了满二叉树和完全二叉树的区别,并引入了完美二叉树的概念,我们需要明确这些术语。 下面开始组织内容: </think>### 二叉树(Binary Tree)详解 二叉树是一种重要的非线性数据结构,其特点是每个节点最多有两个子节点:**左子节点**和**右子节点**。这种结构天然支持递归定义,在算法设计和数据处理中具有广泛应用。 --- #### 一、二叉树核心概念 1. **节点结构** 每个节点包含三个部分: ```java class TreeNode { int data; // 节点存储的数据 TreeNode left; // 左子节点指针 TreeNode right; // 右子节点指针 } ``` 引用示例代码:[^4] 2. **基本术语** - **根节点(Root)**:树的起始节点(如`tree.root`)[^1] - **叶子节点(Leaf)**:左右子节点均为空的节点 - **深度(Depth)**:从根节点到当前节点的路径长度 - **高度(Height)**:从当前节点到最深叶子节点的路径长度 - **度(Degree)**:节点的子节点数量(二叉树中最大为2) 3. **特殊二叉树类型** | 类型 | 定义 | 示例 | |------|------|------| | **满二叉树(Full Binary Tree)** | 每个节点有0或2个子节点 | `A(B,C)` | | **完全二叉树(Complete Binary Tree)** | 除最后一层外全满,最后一层从左向右填充 | `A(B(D,E),C(F))` | | **完美二叉树(Perfect Binary Tree)** | 所有叶子节点在同一层,且每层都被填满 | `A(B(D,E),C(F,G))` [^3] | --- #### 二、二叉树存储结构 1. **链式存储(主流方式)** - 节点通过指针链接(如上文Java代码实现)[^4] - 优点:动态内存分配,插入删除灵活 - 缺点:指针占用额外空间 2. **顺序存储(数组实现)** - 根节点索引:`i=0` - 左子节点索引:`2i+1` - 右子节点索引:`2i+2` - 适用场景:完全二叉树存储优化 --- #### 三、二叉树遍历算法 1. **深度优先遍历(DFS)** ```java // 前序遍历:根 → 左 → 右 void preOrder(TreeNode node) { if (node == null) return; System.out.print(node.data + " "); // 访问根 preOrder(node.left); // 遍历左子树 preOrder(node.right); // 遍历右子树 } ``` - **中序遍历**:左 → 根 → 右(二叉搜索树可输出有序序列)[^1] - **后序遍历**:左 → 右 → 根(常用于表达式树求值) 2. **广度优先遍历(BFS)** 使用队列实现层次遍历: ```java void levelOrder(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { TreeNode node = queue.poll(); System.out.print(node.data + " "); if (node.left != null) queue.add(node.left); if (node.right != null) queue.add(node.right); } } ``` --- #### 四、二叉树构建方法 1. **手动构建(静态)** 直接连接节点(适用于固定结构): ```java BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); // 引用[1]示例 ``` 2. **插入构建(动态)** 根据规则递归插入(如二叉搜索树): ```java void insert(TreeNode node) { if (root == null) root = node; else if (node.data < current.data) current.left = insert(current.left, node); else current.right = insert(current.right, node); } // 引用[4]逻辑简化版 ``` --- #### 五、二叉树关键性质 1. **节点数量关系** - 第 $i$ 层最多有 $2^{i-1}$ 个节点 - 深度为 $k$ 的树最多有 $2^k - 1$ 个节点 2. **高度与节点关系** 对于 $n$ 个节点的二叉树: - 最小高度:$\lfloor \log_2n \rfloor + 1$ - 最大高度:$n$(退化为链表) 3. **特殊性质** 在二叉搜索树中(BST): - 左子树所有节点值 $<$ 根节点值 - 右子树所有节点值 $>$ 根节点值 --- #### 六、二叉树应用场景 1. **数据检索**:二叉搜索树实现$O(\log n)$查找 2. **表达式解析**:算术表达式的语法树表示 3. **文件系统**:目录树结构建模 4. **游戏AI**:决策树实现行为选择 5. **压缩算法**:哈夫曼编码构建最优前缀树 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值