Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The attached code provides a program related to binary trees. Analyze the code to determine what it does. In a text box response, describe what

The attached code provides a program related to binary trees. Analyze the code to determine what it does. In a text box response, describe what the code does and write out both the trees that it creates.

Are "tree" and "tree2" both valid Binary Search Trees? Why is this code a poor way to build a binary tree?

image

class Node int data; Node left, right; public Node(int item) { data = item; left = right = null; } } public class binaryTree { Node root; int getNum() } return getNum(root); int getNum(Node node) { } if (node == null) return 0; if (node.left == null && node.right == null) return 1; else return getNum(node.left) + getNum(node.right); public static void main(String args[]) { binaryTree tree = new binaryTree(); tree.root = new Node(32); tree.root.left = new Node(17); tree.root.left.right = new Node(28); tree.root.right = new Node(37); tree.root.right.right = new Node(54); tree.root.left.right.left = new Node (21); tree.root.left.left = new Node (8); tree.root.right.right.left = new Node(40); tree.root.left.right.right = new Node(29); tree.root.left.left.right = new Node(13); System.out.println(tree.getNum()); binaryTree tree2 = new binaryTree(); tree2.root = new Node(11); tree2.root.right = new Node (24); tree2.root.left = new Node(9); tree2.root.right.left = new Node(27); tree2.root.right.right = new Node (32); tree2.root.left.left = new Node(2); System.out.println(tree2.getNum()); } }

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

Step: 3

blur-text-image

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

Introduction To Health Care Management

Authors: Sharon B. Buchbinder, Nancy H. Shanks

3rd Edition

128408101X, 9781284081015

More Books

Students also viewed these Programming questions