简体   繁体   English

在Java中使用迭代方法实现DFS

[英]Implementation of DFS using iterative approach in java

I have seen a lot of implementations of DFS using a boolean variable named visited, which I don't wish to use in my code. 我已经看到了许多使用名为访问的布尔变量的DFS实现,我不想在我的代码中使用它。 While considering a scene where we have a Node class that holds the reference to left and right nodes corresponding to its children and data which can be any Object, can this method be applicable to Binary Trees to calculate dfs ? 在考虑一个场景时,我们有一个Node类,该类保留对与其子节点和数据(可以是任何Object)相对应的左右节点的引用,此方法可适用于Binary Trees计算dfs吗? I have a scenario where I don't have a adjacency list or matrix. 我有一个没有邻接列表或矩阵的情况。

Is the following code a good implementation of DFS ? 以下代码是否是DFS的良好实现? Is the time complexity of the code O(n) ? 代码的时间复杂度是O(n)吗?

public void dfsForTree(BSTNode root) {
    Stack<BSTNode> s = new Stack<BSTNode>();
    BSTNode node;
    if (root == null) {
        return;
    }
    s.push(root);
    while (!s.isEmpty()) {
        node = s.pop();
        System.out.println(node.getData());
        if (node != null) {

            if (node.getRight() != null) {
                s.push(node.getRight);
            }
            if (node.getLeft != null) {
                s.push(node.getLeft);
            }

        }
    }
}

BSTNode class implementation: BSTNode类的实现:

public class BSTNode {

private BSTNode left;
private BSTNode right;
private int data;

/* Constructor */
public BSTNode(int n) {
    left = null;
    right = null;
    data = n;
}
/* Function to set left node */

public void setLeft(BSTNode n) {
    left = n;
}
/* Function to set right node */

public void setRight(BSTNode n) {
    right = n;
}
/* Function to get left node */

public BSTNode getLeft() {
    return left;
}
/* Function to get right node */

public BSTNode getRight() {
    return right;
}
/* Function to set data to node */

public void setData(int d) {
    data = d;
}
/* Function to get data from node */

public int getData() {
    return data;
}

A sure tell of an iterative tree walk is it requires an "up" link on a node (or saves them) to be able to backtrack. 可以肯定地说,迭代的树遍历是需要节点上的“向上”链接(或保存它们)才能回溯。 You do just this - only saving not "up" links but directly next links to go after backtracking. 您只需要这样做-不保存“上”链接,而是直接保存下一个要在回溯后链接的链接。 On the other hand, there are no interdependencies between steps. 另一方面,步骤之间没有相互依赖性。 See Is this function recursive even though it doesn't call itself? 请参见即使该函数自身未调用,它是否也是递归的? for how to distinguish iterative and disguised recursive. 有关如何区分迭代和变相递归的信息。

Also see Iterative tree walking for an overview of the algorithms. 另请参阅迭代树遍历以获取算法概述。

Now, for computational complexity. 现在,由于计算复杂性。 The principle can be found at Big O, how do you calculate/approximate it? 该原理可以在Big O处找到, 您如何计算/近似它? .

You do: 你做:

  • process every node 处理每个节点
    • exactly once 恰好一次
  • push & pop nodes from the stack 从堆栈中推送和弹出节点
    • each node is also pushed and popped exactly once 每个节点也被推送和弹出一次

So, indeed, it's O(N) . 因此,确实是O(N)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM