Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

sampleInput: 2 1 public class BinaryTree { private Node root; /** Constructs an empty tree. */ public BinaryTree() { root = null; } /** Constructs

image text in transcribed

image text in transcribed

image text in transcribed

sampleInput: 2 1

public class BinaryTree { private Node root;

/** Constructs an empty tree. */ public BinaryTree() { root = null; }

/** Constructs a tree with one node and no children. @param rootData the data for the root */ public BinaryTree(Object rootData) { root = new Node(); root.data = rootData; root.left = null; root.right = null; }

/** Constructs a binary tree. @param rootData the data for the root @param left the left subtree @param right the right subtree */ public BinaryTree(Object rootData, BinaryTree left, BinaryTree right) { root = new Node(); root.data = rootData; root.left = null; root.right = null; if (left != null) { root.left = left.root; } if (right != null) { root.right = right.root; } }

class Node { public Object data; public Node left; public Node right; }

/** Returns the height of the subtree whose root is the given node. @param n a node or null @return the height of the subtree, or 0 if n is null */ private static int height(Node n) { if (n == null) { return 0; } else { return 1 + Math.max(height(n.left), height(n.right)); } }

/** Returns the height of this tree. @return the height */ public int height() { return height(root); }

/** Checks whether this tree is empty. @return true if this tree is empty */ public boolean isEmpty() { return root == null; }

/** Gets the data at the root of this tree. @return the root data */ public Object data() { return root.data; }

/** Gets the left subtree of this tree. @return the left child of the root */ public BinaryTree left() { BinaryTree result = new BinaryTree(); result.root = root.left; return result; }

/** Gets the right subtree of this tree. @return the right child of the root */ public BinaryTree right() { BinaryTree result = new BinaryTree(); result.root = root.right; return result; } }

=================================================================

import java.util.*;

public class PoD {

//============================================================================= /** * Returns true if the binary tree is a strict binary tree, in which * each node in the tree has either zero or two children. * @param bTree BinaryTree of interest * @return boolean true if strict binary tree, false otherwise */

public static boolean isStrictBinaryTree(BinaryTree bTree) {

}

//=============================================================================

public static void main( String [] args ) {

Scanner in = new Scanner( System.in );

int i = in.nextInt(); int j = in.nextInt();

BinaryTree newBT = makeBT(i,j);

boolean isStrictBT = isStrictBinaryTree(newBT);

if (isStrictBT) { System.out.println("Strict binary tree"); } else { System.out.println("NOT a strict binary tree"); }

in.close();

System.out.print("END OF OUTPUT"); }

public static BinaryTree makeBT(int n, int k) { BinaryTree b; if ((n>0) && (n%2==0)) { b = new BinaryTree(n,makeBT(n-1,k),makeBT(n-2,k)); } else if (n>0 & n%2==k) { b = new BinaryTree(n, null, makeBT(n - 2,k)); } else { b = new BinaryTree(0,null,null); }

return b;

} }

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

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions