打印二叉树

问题描述 投票:0回答:1

我正在尝试用 Java 直观地打印出一个简单的二叉树。 这是完整的代码:

class WordPoint {
        private final String word;
        private final float pointValue;

        public WordPoint(String w, float pw) {
            this.word = w;
            this.pointValue = pw;
        }

        public String getWord() {
            return word;
        }

        public float getPointValue() {
            return pointValue;
        }

        @Override
        public String toString() {
            return "(" + word + ", " + pointValue + ")";
        }
    }
class BinarySearchTree {
        private Node root;

        static class Node {
            WordPoint data;
            Node left;
            Node right;

            Node(WordPoint data) {
                this.data = data;
            }
        }

        // Method to insert a new WortPoint into the tree
        public void insert(WordPoint wp) {
            root = insertRec(root, wp);
        }
  
        private Node insertRec(Node root, WordPoint wp) {
            if (root == null) {
                return new Node(wp);
            }

            if(wp.getWord().charAt(0) < root.data.getWord().charAt(0)) {
                root.left = insertRec(root.left, wp);
            } else if (wp.getWord().charAt(0) > root.data.getWord().charAt(0)) {
                root.right = insertRec(root.right, wp);
            }

            return root;
        }

         public void traversePreOrder(StringBuilder sb, String padding, String     pointer, Node node) {
            if (node != null) {
                sb.append(padding);
                sb.append(pointer);
                sb.append(node.data.getWord());
                sb.append("(").append(node.data.getPointValue()).append(")");
                sb.append("\n");

                StringBuilder paddingBuilder = new StringBuilder(padding);
                paddingBuilder.append("|    ");

                String paddingForBoth = paddingBuilder.toString();
                String pointerForRight = "+--";
                String pointerForLeft = (node.right != null) ? "|--" : "+--";

                traversePreOrder(sb, paddingForBoth, pointerForLeft, node.left);
                traversePreOrder(sb, paddingForBoth, pointerForRight, node.right);
            }
        } 

         public void print(PrintStream os) {
             StringBuilder sb = new StringBuilder();
             traversePreOrder(sb, "", "", this.root);
             os.print(sb.toString());
        }
    }


public class binaryTreeTest{
        public static void main(String[] args) {
            BinarySearchTree wordTree = new BinarySearchTree();

            // Example words with their points
            wordTree.insert(new WordPoint("copy", 5.2f));
            wordTree.insert(new WordPoint("friend", 7.76f));
            wordTree.insert(new WordPoint("end", 4.94f));
            wordTree.insert(new WordPoint("apple", 6.34f));
            wordTree.insert(new WordPoint("baseball", 5.64f));
            wordTree.insert(new WordPoint("happy", 7.7f));
            wordTree.insert(new WordPoint("fine", 3.46f));
            wordTree.insert(new WordPoint("spam", 2.94f));
            wordTree.insert(new WordPoint("laugh", 7.84f));
            wordTree.insert(new WordPoint("new", 3.24f));
            wordTree.insert(new WordPoint("school", 7.76f));
            wordTree.insert(new WordPoint("unicorn", 7.82f));

            System.out.println();
            wortBaum.print(System.out);
            System.out.println();
        }
   }

这是我的输出:

copy(5.2)
|    |--friend(7.76)
|    |    |--end(4.94)
|    |    |    +--apple(6.34)        
|    |    |    |    +--baseball(5.64)
|    |    +--happy(7.7)
|    +--spam(2.94)
|    |    |--laugh(7.84)
|    |    |    +--new(3.24)        
|    |    +--unicorn(7.82)

预期输出:

copy(5.2)
|    |--friend(7.76)
|    |    |--end(4.94)
|    |    |    |--apple(6.34) //here the "|" character is incorrect in my output        
|    |    |    |    +--baseball(5.64)
|    |    |    +--fine(3.46) // this one is missing in my output
|    |    +--happy(7.7)
|    +--spam(2.94)
|    |    |--laugh(7.84)
|    |    |    +--new(3.24)
|    |    |    |    +--school(7.76) // this one is missing in my output
|    |    |            
|    |    +--unicorn(7.82)

整体结构有效。只是树中的最后一个节点丢失了。有人知道为什么吗?

我尝试以常规行的形式定期打印这些值,并且它有效。但视觉上这样做根本行不通。

java tree binary-tree
1个回答
0
投票

我认为问题在于,当您 InsertRec 时,您只检查第一个字母是否更大或更小,因此当第一个字母相同但单词不同时,它不会创建新的单词,我建议像这样检查完整的单词:

    private Node insertRec(Node root, WordPoint wp) {

    if (root == null) {

        return new Node(wp);

    }
    int comparison = wp.getWord().compareTo(root.data.getWord());

    if (comparison < 0) {

        root.left = insertRec(root.left, wp);

    } else if (comparison > 0) {

        root.right = insertRec(root.right, wp);
    }

    return root;
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.