java二叉树算法详解

java二叉树算法详解

2023年6月22日发(作者:)

java二叉树算法详解

Java二叉树算法详解

二叉树是一种非常重要的数据结构,它在计算机科学中有着广泛的应用。Java作为一种流行的编程语言,也提供了丰富的二叉树算法库。本文将详细介绍Java中的二叉树算法。

1. 二叉树的定义

二叉树是一种树形结构,它的每个节点最多有两个子节点。二叉树的节点包含一个值和两个指向左右子节点的指针。如果一个节点没有子节点,则它的指针为空。

2. 二叉树的遍历

二叉树的遍历是指按照一定的顺序访问二叉树中的所有节点。Java中提供了三种遍历方式:前序遍历、中序遍历和后序遍历。

前序遍历是指先访问根节点,然后按照左子树、右子树的顺序遍历整个二叉树。Java代码如下:

```

public void preOrder(TreeNode root) {

if (root != null) {

( + " ");

preOrder(); preOrder();

}

}

```

中序遍历是指先按照左子树、根节点、右子树的顺序遍历整个二叉树。Java代码如下:

```

public void inOrder(TreeNode root) {

if (root != null) {

inOrder();

( + " ");

inOrder();

}

}

```

后序遍历是指先按照左子树、右子树的顺序遍历整个二叉树,最后访问根节点。Java代码如下:

```

public void postOrder(TreeNode root) {

if (root != null) { postOrder();

postOrder();

( + " ");

}

}

```

3. 二叉树的搜索

二叉树的搜索是指在二叉树中查找一个特定的节点。Java中提供了两种搜索方式:深度优先搜索和广度优先搜索。

深度优先搜索是指从根节点开始,按照深度优先的方式遍历整个二叉树,直到找到目标节点或遍历完整个二叉树。Java代码如下:

```

public TreeNode dfs(TreeNode root, int target) {

if (root == null) {

return null;

}

if ( == target) {

return root;

}

TreeNode left = dfs(, target); TreeNode right = dfs(, target);

return left != null ? left : right;

}

```

广度优先搜索是指从根节点开始,按照广度优先的方式遍历整个二叉树,直到找到目标节点或遍历完整个二叉树。Java代码如下:

```

public TreeNode bfs(TreeNode root, int target) {

Queue queue = new LinkedList<>();

(root);

while (!y()) {

TreeNode node = ();

if ( == target) {

return node;

}

if ( != null) {

();

}

if ( != null) {

();

} }

return null;

}

```

4. 二叉树的插入和删除

二叉树的插入是指向二叉树中添加一个新节点。Java代码如下:

```

public TreeNode insert(TreeNode root, int val) {

if (root == null) {

return new TreeNode(val);

}

if (val < ) {

= insert(, val);

} else {

= insert(, val);

}

return root;

}

```

二叉树的删除是指从二叉树中删除一个节点。Java代码如下:

```

public TreeNode delete(TreeNode root, int val) {

if (root == null) {

return null;

}

if (val < ) {

= delete(, val);

} else if (val > ) {

= delete(, val);

} else {

if ( == null) {

return ;

} else if ( == null) {

return ;

}

TreeNode minNode = findMin();

= ;

= delete(, );

}

return root;

}

private TreeNode findMin(TreeNode node) {

while ( != null) {

node = ;

}

return node;

}

```

5. 二叉树的平衡

二叉树的平衡是指保持二叉树的左右子树高度差不超过1。Java中提供了AVL树和红黑树等平衡二叉树算法。

AVL树是一种自平衡二叉搜索树,它的每个节点的左右子树高度差不超过1。Java代码如下:

```

public class AVLTree {

private TreeNode root;

public void insert(int val) {

root = insert(root, val);

}

private TreeNode insert(TreeNode node, int val) { if (node == null) {

return new TreeNode(val);

}

if (val < ) {

= insert(, val);

} else {

= insert(, val);

}

int balance = getBalance(node);

if (balance > 1 && val < ) {

return rightRotate(node);

}

if (balance < -1 && val > ) {

return leftRotate(node);

}

if (balance > 1 && val > ) {

= leftRotate();

return rightRotate(node);

}

if (balance < -1 && val < ) {

= rightRotate();

return leftRotate(node); }

return node;

}

private int getHeight(TreeNode node) {

if (node == null) {

return 0;

}

return (getHeight(), getHeight()) +

1;

}

private int getBalance(TreeNode node) {

if (node == null) {

return 0;

}

return getHeight() - getHeight();

}

private TreeNode leftRotate(TreeNode node) {

TreeNode right = ;

TreeNode left = ;

= node;

= left; return right;

}

private TreeNode rightRotate(TreeNode node) {

TreeNode left = ;

TreeNode right = ;

= node;

= right;

return left;

}

}

```

红黑树是一种自平衡二叉搜索树,它的每个节点被标记为红色或黑色,且满足以下性质:

1. 根节点是黑色的。

2. 每个叶子节点都是黑色的空节点。

3. 如果一个节点是红色的,则它的两个子节点都是黑色的。

4. 从任意节点到其每个叶子节点的所有路径都包含相同数目的黑色节点。

Java代码如下:

``` public class RedBlackTree {

private static final boolean RED = true;

private static final boolean BLACK = false;

private TreeNode root;

public void insert(int val) {

root = insert(root, val);

= BLACK;

}

private TreeNode insert(TreeNode node, int val) {

if (node == null) {

return new TreeNode(val, RED);

}

if (val < ) {

= insert(, val);

} else {

= insert(, val);

}

if (isRed() && !isRed()) {

node = rotateLeft(node);

}

if (isRed() && isRed()) { node = rotateRight(node);

}

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1687385971a6187.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信