• 欢迎使用千万蜘蛛池,网站外链优化,蜘蛛池引蜘蛛快速提高网站收录,收藏快捷键 CTRL + D

1. Java树形结构查询方法大全:Recursion、Stack、Queue等 2. 如何快速实现Java树形结构查询?实例详解


在Java编程语言中,树形结构查询通常用递归遍历和迭代遍历(栈)实现。递归方法是通过调用自身函数来解决问题的方法,在Java树形查询中,我们可以使用递归方法来遍历树形结构数据。

使用递归方法实现Java树形查询

以下是一个简单的Java递归方法实现树形查询的示例:

Java树形结构查询

<pre>
class TreeNode {    
    int val;    
    TreeNode left;    
    TreeNode right;    
    TreeNode(int x) {        
        val = x;    
    }
}

public class TreeTraversal {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(7);
        System.out.println("前序遍历:");
        preOrderTraversal(root);
        System.out.println("");
        System.out.println("中序遍历:");
        inOrderTraversal(root);
        System.out.println("");
        System.out.println("后序遍历:");
        postOrderTraversal(root);
        System.out.println("");
    }

    // 前序遍历    
    public static void preOrderTraversal(TreeNode node) {
        if (node == null) {            
            return;        
        }        
        System.out.print(node.val + " ");        
        preOrderTraversal(node.left);        
        preOrderTraversal(node.right);    
    }

    // 中序遍历    
    public static void inOrderTraversal(TreeNode node) {        
        if (node == null) {            
            return;        
        }        
        inOrderTraversal(node.left);        
        System.out.print(node.val + " ");        
        inOrderTraversal(node.right);    
    }    

    // 后序遍历    
    public static void postOrderTraversal(TreeNode node) {        
        if (node == null) {            
            return;        
        }        
        postOrderTraversal(node.left);        
        postOrderTraversal(node.right);        
        System.out.print(node.val + " ");    
    }
</pre>

递归方法的基本思想是以一种逻辑方式处理数据,将问题分解为子问题,直到问题变得足够简单,以便可以被最终处理。

使用迭代方法实现Java树形查询

另一种处理Java树形查询的方法是迭代遍历树形结构(如栈)。迭代方法是一种使用循环结构来解决问题的方法,在Java树形查询中,我们可以使用迭代方法来实现树形结构的遍历。

Java树形结构查询

<pre>
import java.util.*;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

public class IterativeTreeTraversal {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(7);
        System.out.println("前序遍历:");
        preOrderIterativeTraversal(root);
        System.out.println("");
        System.out.println("中序遍历:");
        inOrderIterativeTraversal(root);
        System.out.println("");
        System.out.println("后序遍历:");
        postOrderIterativeTraversal(root);
        System.out.println("");
    }

    // 前序遍历(迭代)
    public static void preOrderIterativeTraversal(TreeNode node) {
        Stack<TreeNode> stack = new Stack<>();
        if (node != null) {  // 如果根节点不为空,将其压入栈中并访问根节点的值和左子节点、右子节点(如果存在)的值,否则返回空栈。
            stack.push(node); // 将根节点压入栈中
            while (!stack.isEmpty()) { // 当栈非空时,执行以下操作
                TreeNode currentNode = stack.pop();
                System.out.print(currentNode.val + " ");
                if (currentNode.right != null) {
                    stack.push(currentNode.right);
                }
                if (currentNode.left != null) {
                    stack.push(currentNode.left);
                }
            }
        }
    }

    // 中序遍历(迭代)
    public static void inOrderIterativeTraversal(TreeNode node) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode currentNode = node;
        while (currentNode != null || !stack.isEmpty()) {
            while (currentNode != null) {
                stack.push(currentNode);
                currentNode = currentNode.left;
            }
            currentNode = stack.pop();
            System.out.print(currentNode.val + " ");
            currentNode = currentNode.right;
        }
    }

    // 后序遍历(迭代)
    public static void postOrderIterativeTraversal(TreeNode node) {
        Stack<TreeNode> stack = new Stack<>();
        if (node != null) {
            stack.push(node);
            TreeNode prev = null;
            while (!stack.isEmpty()) {
                TreeNode current = stack.peek();
                if (prev == null || prev.left == current || prev.right == current) {
                    if (current.left != null) {
                        stack.push(current.left);
                    } else if (current.right != null) {
                        stack.push(current.right);
                    } else {
                        System.out.print(current.val + " ");
                        stack.pop();
                    }
                } else if (current.left == prev) {
                    if (current.right != null) {
                        stack.push(current.right);
                    } else {
                        System.out.print(current.val + " ");
                        stack.pop();
                    }
                } else if (current.right == prev) {
                    System.out.print(current.val + " ");
                    stack.pop();
                }
                prev = current;
            }
        }
    }
</pre>

引导读者评论、关注、点赞和感谢观看

希望这篇文章能够帮助您学习Java树形结构查询的方法。如果您有任何疑问或建议,请在下面的评论栏中留言。同时,请关注我的博客,点赞并分享这篇文章,感谢您的观看。

本文链接:https://www.24zzc.com/news/171692847575284.html