简体   繁体   中英

How can I modify the traverse of binary search trees?

I successfully made the binary tree but I can't traverse it right. So this is my binary tree program, and my traverse method.

import java.util.*;
public class BinaryTreeUtube {

    Node root;
    public void addNode(int key) {
        Node newNode = new Node(key);

        if (root == null) {
            root = newNode;

        }
        else {
            Node focusNode = root; 
            Node parent;
            while (true) {
                parent = focusNode;
                if (key < focusNode.key) {
                    focusNode = focusNode.leftChild;
                    if (focusNode == null) {
                        parent.leftChild = newNode;
                        return;
                    }
                } else {
                    focusNode = focusNode.rightChild;
                    if (focusNode == null) {
                        parent.rightChild = newNode;
                        return; 
                    }
                }
            }
        }
    }

    public void inOrderTraverseTree(Node focusNode) {

        if (focusNode != null) {


            inOrderTraverseTree(focusNode.leftChild);
            System.out.print(focusNode + ",");
            inOrderTraverseTree(focusNode.rightChild);

        }


    }




    public Node findNode(int key) {

        Node focusNode = root;

        while(focusNode.key != key) {
            if (key < focusNode.key) {
                focusNode = focusNode.leftChild;
            }
            else {
                focusNode = focusNode.rightChild;
            }

            if (focusNode == null) {
                return null;
            }
        }
        return focusNode;
    }

    public static void main(String[] args){


        BinaryTreeUtube theTree = new BinaryTreeUtube();
        Scanner sc = new Scanner(System.in);
        int times = sc.nextInt();
        for (int t = 0; t < times; t++) {
            theTree.addNode(sc.nextInt());
        }




        theTree.inOrderTraverseTree(theTree.root);


    }

}



class Node {

    int key;

    Node leftChild;
    Node rightChild;

    Node(int key) {
        this.key = key;
    }

    public String toString() {
        if (leftChild == null) {
            return "(-," + Integer.toString(key) + ",-)";
        }
        return Integer.toString(key);
    }
}

I input

5
3 5 4 2 8

and it returns

(-,2,-),3,(-,4,-),5,(-,8,-), 

rather than

(-,2,-),3,((-,4,-),5,(-,8,-)),

I tried lots of ways to modify the code to let it do what I want, but all failed... How can I give my program the ability to detect the hierarchy between nodes? what modification should I make? Thx!

You could change to toString method in Node :

public String toString() {
    String l = Objects.toString(leftChild, "-");
    String r = Objects.toString(rightChild, "-");
    return "(" + l + "," + key + "," + r + ")";
}

Then you can just call System.out.println(theTree.root) to see the structure.

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