Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, you are to take the existing example for a binary tree and implement tree traversal: pre-order, in-order, post-order with an example. Node

For this assignment, you are to take the existing example for a binary tree and implement tree traversal: pre-order, in-order, post-order with an example.

Node Class

private class Node { Integer value; Node left = null; Node right = null; public Node(Integer obj) { value = obj; } public Node(Integer obj, Node leftChild, Node rightChild) { value = obj; left = leftChild; right = rightChild; } }

Binary Tree Class

public class BinaryTree { public Node root = null; public BinaryTree() { } public Integer getMin() { if (root == null) { return null; } else { return leftMost(root).value; } } private Node leftMost(Node n) { if (n.left != null) { leftMost(n.left); } return n; } public void add(Integer obj) { Node n = new Node(obj); if (root == null) { root = n; } else { this.add(root, n); } } private void add(Node r, Node n) { if (r.value > n.value) { if (r.left == null) { r.left = n; } else { add(r.left, n); } } else { if (r.right == null) { r.right = n; } else { add(r.right, n); } } } }

Main method

public static void main(String[] args) { Random rand = new Random(); BinaryTree bintree = new BinaryTree(); for (int i = 0; i < 5; i++) { bintree.add(rand.nextInt()); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions