如何在漂亮图中打印二进制搜索树?

发布于 2025-02-07 13:40:56 字数 2909 浏览 0 评论 0原文

我必须用这样的方法来实现二进制搜索树,该方法以这样的连接打印出漂亮的图表:

“在此处输入映像”

现在我设法打印了以下内容:

​更好:/

您是否有任何提示如何解决?

这是我实施它的实例守则:

public interface PrintableTree {

    class Node {
        int data;
        Node left, right;

        Node(int data) {
            this.data = data;
            this.left = null;
            this.right = null;
        }
    }

    class Trunk {
        Trunk prev;
        String str;

        Trunk(Trunk prev, String str) {
            this.prev = prev;
            this.str = str;
        }
    }

    Node insert_Recursive(Node root, int key);

    void add(int i);

    String prettyPrint();

    static PrintableTree getInstance() {
        return new PrintableTree() {
            String stringOfTree = "";
            static final int COUNT = 2;
            Node root;

            @Override
            public void add(int i) {
                root = insert_Recursive(root, i);
            }

            @Override
            public Node insert_Recursive(Node root, int key) {
                if (root == null) {
                    root = new Node(key);
                    return root;
                }

                if (key < root.data)
                    root.left = insert_Recursive(root.left, key);
                else if (key > root.data)
                    root.right = insert_Recursive(root.right, key);

                return root;
            }

            @Override
            public String prettyPrint() {
                printTree(root, null, false);
                return "";
            }

            public void showTrunks(Trunk p) {
                if (p == null) {
                    return;
                }

                showTrunks(p.prev);
                System.out.print(p.str);
            }

            public void printTree(Node root, Trunk prev, boolean isLeft) {
                if (root == null) {
                    return;
                }

                String prev_str = "    ";
                Trunk trunk = new Trunk(prev, prev_str);

                printTree(root.left, trunk, true);

                if (prev == null) {
                    trunk.str = "";
                } else if (isLeft) {
                    trunk.str = "┌";
                    prev_str = "    │";
                } else {
                    trunk.str = "└";
                    prev.str = prev_str;
                }

                showTrunks(trunk);
                System.out.println(" " + root.data);

                if (prev != null) {
                    prev.str = prev_str;
                }
                trunk.str = "   │";

                printTree(root.right, trunk, false);
            }

        };
    }
}

I have to implement binary search tree with method that prints nice diagram with connections like this:

enter image description here

For now I managed to print this:

enter image description here

However I'm struggling to make it better :/

Do you have any hints how to fix that?

It's my code of instance implementing it:

public interface PrintableTree {

    class Node {
        int data;
        Node left, right;

        Node(int data) {
            this.data = data;
            this.left = null;
            this.right = null;
        }
    }

    class Trunk {
        Trunk prev;
        String str;

        Trunk(Trunk prev, String str) {
            this.prev = prev;
            this.str = str;
        }
    }

    Node insert_Recursive(Node root, int key);

    void add(int i);

    String prettyPrint();

    static PrintableTree getInstance() {
        return new PrintableTree() {
            String stringOfTree = "";
            static final int COUNT = 2;
            Node root;

            @Override
            public void add(int i) {
                root = insert_Recursive(root, i);
            }

            @Override
            public Node insert_Recursive(Node root, int key) {
                if (root == null) {
                    root = new Node(key);
                    return root;
                }

                if (key < root.data)
                    root.left = insert_Recursive(root.left, key);
                else if (key > root.data)
                    root.right = insert_Recursive(root.right, key);

                return root;
            }

            @Override
            public String prettyPrint() {
                printTree(root, null, false);
                return "";
            }

            public void showTrunks(Trunk p) {
                if (p == null) {
                    return;
                }

                showTrunks(p.prev);
                System.out.print(p.str);
            }

            public void printTree(Node root, Trunk prev, boolean isLeft) {
                if (root == null) {
                    return;
                }

                String prev_str = "    ";
                Trunk trunk = new Trunk(prev, prev_str);

                printTree(root.left, trunk, true);

                if (prev == null) {
                    trunk.str = "";
                } else if (isLeft) {
                    trunk.str = "┌";
                    prev_str = "    │";
                } else {
                    trunk.str = "└";
                    prev.str = prev_str;
                }

                showTrunks(trunk);
                System.out.println(" " + root.data);

                if (prev != null) {
                    prev.str = prev_str;
                }
                trunk.str = "   │";

                printTree(root.right, trunk, false);
            }

        };
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一萌ing 2025-02-14 13:40:56

您可以使用这些功能。他们返回一个字符串,因此由呼叫者打印。

当向上打印右子树和向下的左子树时,我也发现它会更好。这样,这棵树与通常描绘的方式仅旋转90° - 顶部的根。

这是相关代码:

    public String pretty() {
        return pretty(root, "", 1);
    }

    private String pretty(Node root, String prefix, int dir) {
        if (root == null) {
            return "";
        }

        String space = " ".repeat(("" + root.data).length());
        return pretty(root.right, prefix + "│  ".charAt(dir) + space, 2)
            + prefix + "└ ┌".charAt(dir) + root.data
              + " ┘┐┤".charAt((root.left  != null ? 2 : 0) 
                            + (root.right != null ? 1 : 0)) + "\n"
            + pretty(root.left, prefix + "  │".charAt(dir) + space, 0);
    }

You could use these functions. They return a string, so it is up to the caller to print it.

I also find it nicer when the right subtree is printed upwards, and the left subtree downwards. That way, the tree is just rotated 90° from how it is usually depicted -- with the root at the top.

Here is the relevant code:

    public String pretty() {
        return pretty(root, "", 1);
    }

    private String pretty(Node root, String prefix, int dir) {
        if (root == null) {
            return "";
        }

        String space = " ".repeat(("" + root.data).length());
        return pretty(root.right, prefix + "│  ".charAt(dir) + space, 2)
            + prefix + "└ ┌".charAt(dir) + root.data
              + " ┘┐┤".charAt((root.left  != null ? 2 : 0) 
                            + (root.right != null ? 1 : 0)) + "\n"
            + pretty(root.left, prefix + "  │".charAt(dir) + space, 0);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文