数据结构:二叉树

文章目录


二叉树

一,概述

二叉树是一种非线性数据结构,它由一系列节点组成,每个节点最多有两个子节点,通常称为左子节点和右子节点。二叉树的每个元素都有一个键,通过这个键可以从树中找到该元素。

二叉树具有以下性质:

  1. 每个节点最多有两个子节点,即左子节点和右子节点。
  2. 每个节点可以拥有任意数量的子节点,也可以没有子节点。
  3. 每个节点都有一个键,通过这个键可以识别节点。
  4. 除了根节点外,每个节点都有一个父节点。

二叉树的基本操作包括:

  1. 插入:向二叉树中插入一个新节点。
  2. 删除:从二叉树中删除一个节点。
  3. 查找:在二叉树中查找一个节点。
  4. 遍历:遍历二叉树中的所有节点。

二叉树的遍历方式有三种:前序遍历、中序遍历和后序遍历。

  1. 前序遍历:先访问根节点,然后遍历左子树,最后遍历右子树。
  2. 中序遍历:先遍历左子树,然后访问根节点,最后遍历右子树。
  3. 后序遍历:先遍历左子树,然后遍历右子树,最后访问根节点。

二叉树的应用非常广泛,例如在计算机科学中的搜索树、排序树、编码理论、数据压缩等领域都有应用。

简介

  • 二叉树是一种非线性数据结构,由一些称为节点的对象组成,其中每个节点最多有两个子节点,通常称为左子节点和右子节点。
  • 二叉树的根是没有父节点的节点,叶子是没有子节点的节点。
  • 二叉树的高度是从根到所有叶子节点的最长路径上的节点数。
  • 二叉树有多种类型,如满二叉树、完全二叉树、平衡二叉树等。

图示

一个简单的二叉树如下:

lua 复制代码
    1
   / \
  2   3
 / \
4   5

在这个二叉树中,1是根节点,2和3是1的子节点,4和5是2的子节点。高度为3。

示例

在Java中,可以创建一个简单的二叉树类:

java 复制代码
class Node {
    int key;
    Node left, right;
 
    public Node(int item) {
        key = item;
        left = right = null;
    }
}
 
class BinaryTree {
    Node root;
 
    BinaryTree(int key) {
        root = new Node(key);
    }
 
    BinaryTree() {
        root = null;
    }
 
    // 更多的方法(如插入、搜索、删除等)可以在这里添加
}

在这个示例中,创建了一个Node类来表示二叉树的节点,每个节点都有一个键和两个子节点。还创建了一个BinaryTree类,它有一个根节点,并可以添加更多的方法来操作二叉树(例如插入、搜索和删除节点等)。

二,添加数据

在Java中,二叉树是一种常见的数据结构,它可以有效地组织和搜索数据。下面是一个示例,展示如何在二叉树中添加数据:

首先,需要创建一个表示二叉树节点的类:

java 复制代码
public class TreeNode {
    int value;
    TreeNode left;
    TreeNode right;

    public TreeNode(int value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

然后,我们可以创建一个表示二叉树的类,并添加一个方法来添加数据:

java 复制代码
public class BinaryTree {
    TreeNode root;

    public BinaryTree() {
        root = null;
    }

    public void add(int value) {
        root = addRecursive(root, value);
    }

    private TreeNode addRecursive(TreeNode current, int value) {
        if (current == null) {
            return new TreeNode(value);
        }

        if (value < current.value) {
            current.left = addRecursive(current.left, value);
        } else if (value > current.value) {
            current.right = addRecursive(current.right, value);
        } else {
            // value already exists in the tree, do nothing
            return current;
        }

        return current;
    }
}

这里,使用了递归方法addRecursive来找到应该插入新节点的位置。如果树为空,就在根节点插入新值。如果新值小于当前节点的值,将其插入到左子树;如果新值大于当前节点的值,将其插入到右子树。如果新值已经存在于树中,什么也不做。

现在,我们可以创建一个二叉树并向其中添加数据:

java 复制代码
public class Main {
    public static void main(String[] args) {
        BinaryTree bt = new BinaryTree();
        bt.add(6);
        bt.add(4);
        bt.add(8);
        bt.add(3);
        bt.add(5);
        bt.add(7);
        bt.add(9);
    }
}

这是一个基本的二叉搜索树的实现,新添加的元素总是被放在正确的位置以保持树的排序属性。

三,删除数据

在Java中,二叉树是一种常见的数据结构,可以有效地组织和搜索数据。下面是一个示例,展示如何在二叉树中删除数据:

首先,需要创建一个表示二叉树节点的类:

java 复制代码
public class TreeNode {
    int value;
    TreeNode left;
    TreeNode right;

    public TreeNode(int value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

然后,可以创建一个表示二叉树的类,并添加一个方法来删除数据:

java 复制代码
public class BinaryTree {
    TreeNode root;

    public BinaryTree() {
        root = null;
    }

    public void remove(int value) {
        root = removeRecursive(root, value);
    }

    private TreeNode removeRecursive(TreeNode current, int value) {
        if (current == null) {
            return null;
        }

        if (value == current.value) {
            // Node with the given value found, remove it from the tree.
            if (current.left == null && current.right == null) {
                return null;
            } else if (current.left == null) {
                return current.right;
            } else if (current.right == null) {
                return current.left;
            } else {
                // Find the minimum value in the right subtree and replace it with the current node's value.
                TreeNode minNode = findMin(current.right);
                current.value = minNode.value;
                current.right = removeRecursive(current.right, minNode.value);
                return current;
            }
        } else if (value < current.value) {
            current.left = removeRecursive(current.left, value);
            return current;
        } else {
            current.right = removeRecursive(current.right, value);
            return current;
        }
    }

    private TreeNode findMin(TreeNode node) {
        while (node.left != null) {
            node = node.left;
        }
        return node;
    }
}

这里,使用了递归方法removeRecursive来找到应该删除节点的位置。如果要删除的节点没有子节点,直接返回null。如果要删除的节点只有一个子节点,将这个子节点返回作为新的节点。如果要删除的节点有两个子节点,找到右子树中的最小节点,用它替换要删除的节点,然后在右子树中递归删除这个最小节点。

相关推荐
曳渔17 分钟前
Java-数据结构-二叉树-习题(三)  ̄へ ̄
java·开发语言·数据结构·算法·链表
shark-chili27 分钟前
数据结构与算法-Trie树添加与搜索
java·数据结构·算法·leetcode
Crossoads2 小时前
【数据结构】排序算法---快速排序
c语言·开发语言·数据结构·算法·排序算法
眰恦3742 小时前
数据结构--第五章树与二叉树
数据结构·算法
未 顾2 小时前
JavaSE--集合总览02:单列集合Collection的体系之一:List
数据结构·list
wx200411022 小时前
Codeforces Round 973 (Div. 2) - D题
数据结构·c++·算法
Crossoads3 小时前
【数据结构】排序算法---基数排序
c语言·开发语言·数据结构·算法·排序算法
爱敲代码的憨仔3 小时前
第二讲 数据结构
数据结构·算法
问道飞鱼3 小时前
每日学习一个数据结构-布隆过滤器Bloom Filter
数据结构·学习·哈希算法
龙ze3 小时前
数据结构—(java)反射,枚举,lambda表达式
java·开发语言·数据结构