简体   繁体   中英

How to print out value as a long string in java binary search tree?

So I'm trying to print out the value as a long string that all my nodes have in BST, the problem is I can't find a way to preserve those value within the method since they are recursive methods( inOrder preOrder postOrder), how do I do that? my codes work when I try to print out each value line by line. Thanks in advance!

say, I have these names and I want to print those out in alphabetic order using BST: Jerry, Elaine, Ralph, Alice, George, Susan, Norton, Trixie. The result I want: [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();
    }

You can use generic type . Something like BinarySearchTree<T> , where T can be any type of object. Then when you use it, you can initialize as such BinarySearchTree<String> in case you need to deal with String

You can have a static String variable in the TreeNode class. Since there is one copy of the variable available for all instances, you can append the value as you traverse.

Eg: myPath += "["+value+"]"; where your temp = "["+value+"]"; is

Once the traversal is complete you can print TreeNode.myPath .

Just concatenate the results from calling inOrder of the left and the right subtree.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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