Question

Create graph binary tree with java code Then the output - post order - in order...

Create graph binary tree with java code
Then the output
- post order
- in order
- pre order
- depth
- breath



0 0
Add a comment Improve this question Transcribed image text
Answer #1

Sol: I implemented a binary tree for my asignment and I have attached the code from the same:

class Node {

    int value;

    Node left;

    Node right;

    Node(int value) {

        this.value = value;

        right = null;

        left = null;

    }

}

public class BinaryTree {

    Node root;

private Node addRecursive(Node current, int value) {

    if (current == null) {

        return new Node(value);

    }

    if (value < current.value) {

        current.left = addRecursive(current.left, value);

    } else if (value > current.value) {

        current.right = addRecursive(current.right, value);

    } else {

        // value already exists

        return current;

    }

    return current;

}

public void add(int value) {

    root = addRecursive(root, value);

}

private BinaryTree createBinaryTree() {

    BinaryTree bt = new BinaryTree();

    bt.add(6);

    bt.add(4);

    bt.add(8);

    bt.add(3);

    bt.add(5);

    bt.add(7);

    bt.add(9);

    return bt;

}

private boolean containsNodeRecursive(Node current, int value) {

    if (current == null) {

        return false;

    }

    if (value == current.value) {

        return true;

    }

    return value < current.value

      ? containsNodeRecursive(current.left, value)

      : containsNodeRecursive(current.right, value);

}

public boolean containsNode(int value) {

    return containsNodeRecursive(root, value);

}

//1. Post order traversal

public void traversePostOrder(Node node) {

    if (node != null) {

        traversePostOrder(node.left);

        traversePostOrder(node.right);

        System.out.print(" " + node.value);

    }

}

// The output will be : 3 5 4 7 9 8 6

//2. In order

public void traverseInOrder(Node node) {

    if (node != null) {

        traverseInOrder(node.left);

        System.out.print(" " + node.value);

        traverseInOrder(node.right);

    }

}

//Output will be 3 4 5 6 7 8 9

//3. Pre order

public void traversePreOrder(Node node) {

    if (node != null) {

        System.out.print(" " + node.value);

        traversePreOrder(node.left);

        traversePreOrder(node.right);

    }

}

//Output will be 6 4 3 5 8 7 9

//4. depth

int mDepth(Node node)

    {

        if (node == null)

            return 0;

        else

        {

            int lDepth = mDepth(node.left);

            int rDepth = mDepth(node.right);

            if (lDepth > rDepth)

                return (lDepth + 1);

             else

                return (rDepth + 1);

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Create graph binary tree with java code Then the output - post order - in order...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT