【问题标题】:How to print out value as a long string in java binary search tree?java - 如何在Java二叉搜索树中将值打印为长字符串?
【发布时间】:2018-11-02 12:51:54
【问题描述】:

所以我试图将值作为我所有节点在 BST 中的长字符串打印出来,问题是我找不到在方法中保留这些值的方法,因为它们是递归方法(inOrder preOrder postOrder),我该怎么做?当我尝试逐行打印每个值时,我的代码有效。提前致谢!

说,我有这些名字,我想使用 BST 按字母顺序打印出来: 杰瑞、伊莱恩、拉尔夫、爱丽丝、乔治、苏珊、诺顿、崔克茜。 我想要的结果: [Alice][Elaine][George][Jerry][Norton][Ralph][Susan][Trixie]

import java.util.Random;

public class TreeNode {

    private String value;
    private TreeNode left;
    private TreeNode right;

    public TreeNode(String n) {
        value = n;
        left = right = null;
    }

    public void insert(String n) {
        if (n.compareTo(value) <= 0) {
            if (left == null) {
                left = new TreeNode(n);
            } else {
                left.insert(n);
            }
        } else {
            if (right == null) {
                right = new TreeNode(n);
            } else {
                right.insert(n);
            }
        }
    }

    public boolean contains(String n) {
        if (n == value) {
            return true;
        } else if (n.compareTo(value) <= 0) {
            if (left == null) {
                return false;
            } else {
                return left.contains(n);
            }
        } else {
            if (right == null) {
                return false;
            } else {
                return right.contains(n);
            }
        }
    }

    public TreeNode remove(String n) {
        if (n.compareTo(value) < 0) {
            if (left != null) {
                left = left.remove(n);
            }
        } else if (n.compareTo(value) > 0) {
            if (right != null) {
                right = right.remove(n);
            }
        } else {
            if (left == null && right == null) {
                return null;
            } else if (left != null && right == null) {
                return left;
            } else if (left == null && right != null) {
                return right;
            } else {
                Random r = new Random();
                if (r.nextBoolean()) {
                    value = left.rightMost();
                    left = left.remove(value);
                } else {
                    value = right.leftMost();
                    right = right.remove(value);
                }
            }
        }
        return this;
    }

    public String leftMost() {
        if (left == null) {
            return value;
        } else {
            return left.leftMost();
        }
    }

    public String rightMost() {
        if (right == null) {
            return value;
        } else {
            return right.rightMost();
        }
    }

    public String inOrder() {

        String temp = null;
        if (left != null) {
            left.inOrder();
        }

        temp = "["+value+"]";

        if (right != null) {
            right.inOrder();
        }

        return temp;

    }
public class BinarySearchTree {

    private TreeNode root;

    public BinarySearchTree() {
        root = null;
    }

    public void insert(String n) {
        if (root == null) {
            root = new TreeNode(n);
        } else {
            root.insert(n);
        }
    }

    public boolean contains(String n) {
        if (root == null) {
            return false;
        } else {
            return root.contains(n);
        }
    }

    public void remove(String n) {
        if (root != null) {
            root = root.remove(n);
        }
    }

    public String inOrder() {

        if (root != null) {
            root.inOrder();
        }
        return root.inOrder();
    }

【问题讨论】:

    标签: java binary-search-tree inorder


    【解决方案1】:

    您可以使用generic type。类似于BinarySearchTree&lt;T&gt;,其中T 可以是任何类型的对象。然后在使用的时候可以初始化为BinarySearchTree&lt;String&gt;,以防需要处理String

    【讨论】:

      【解决方案2】:

      您可以在TreeNode 类中拥有一个静态字符串变量。由于所有实例都有一个可用的变量副本,因此您可以在遍历时附加该值。

      例如: myPath += "["+value+"]"; 你的temp = "["+value+"]"; 在哪里

      遍历完成后,您可以打印TreeNode.myPath

      【讨论】:

        【解决方案3】:

        只需连接左右子树调用inOrder的结果即可。

        public String inOrder() {
            String temp = "[" + value +"]";
            if (left != null) {
                temp = left.inOrder() + temp;
            }
        
            if (right != null) {
                temp = temp + right.inOrder();
            }
            return temp;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-11
          • 1970-01-01
          • 1970-01-01
          • 2019-03-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多