数据结构:二叉树

文章目录


二叉树

一,概述

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

二叉树具有以下性质:

  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。如果要删除的节点只有一个子节点,将这个子节点返回作为新的节点。如果要删除的节点有两个子节点,找到右子树中的最小节点,用它替换要删除的节点,然后在右子树中递归删除这个最小节点。

相关推荐
nice_lcj5201 天前
LRU 缓存入门:它是什么、怎么用、面试怎么手写
java·数据结构·缓存·面试·lru
꧁细听勿语情꧂1 天前
直接选择排序,堆排序,冒泡排序,递归快速排序、hoare版本、挖坑法、lomuto前后指针,非递归快速排序
c语言·开发语言·数据结构·算法
Pniubi2 天前
关于算法用现成数据结构和手撕
数据结构
老师我太想进步了20262 天前
【数据结构-6】算法美学之单链表(1)
数据结构·算法
希望我万事顺遂2 天前
力扣hot100-滑动窗口最大值
数据结构·算法·leetcode
꧁细听勿语情꧂2 天前
二叉树余题,排序、直接插入排序、希尔排序
c语言·开发语言·数据结构·算法
是main不是漫2 天前
【双指针|LeetCode1089 复写零】巧用逆向思路,破解原地覆盖陷阱
数据结构·算法·leetcode
饮水桶_T3 天前
JAVA IO流笔记
开发语言·数据结构·算法
wo_jack3 天前
C++ 入门笔记(十七):list 入门 —— 为什么 list 不能 sort?
数据结构·c++·面试·stl·list
zaizai10073 天前
算法(左程云)1
数据结构