Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Students should add three methods in the binaryTree.java: public void inorder(); public void postorder(); public int findHeight(btNode root); Then test the three methods. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- For

Students should add three methods in the binaryTree.java: public void inorder(); public void postorder(); public int findHeight(btNode root); Then test the three methods.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

For the in-order and post-order scanning through the binary tree, you just have to switch the order of the commands.

image text in transcribed

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Count the height of the binary tree

Definition: The height of a node is the number of edges from the node to the deepest leaf node.

int findHeight(btNode r) {

if r = null

return 0;

/* This is to deal with the leaf node situation*/

int lh = findHeight(left child)

int rh = findHeight(right child);

/* Check for length of left and right subtree height */

return max(lh, rh)+1

}

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

binaryTree.java:

package binaryTreeLAB;

public class binaryTree { protected btNode root; /* Constructor */ public binaryTree() { root = null; } /* Function to check if tree is empty */ public boolean isEmpty() { return root == null; } /* Functions to insert data */ public void insert(int data) { root = insert(root, data); } /* Function to insert data recursively */ private btNode insert(btNode node, int data) { if (node == null) node = new btNode(data); else { if (data

/* Function for preorder traversal */ public void preorder() { preorder(root); } private void preorder(btNode r) { if (r != null) { System.out.print(r.getData() +" "); preorder(r.getLeft()); preorder(r.getRight()); } } /* * Students in the LAB should complete three methods as follows */ /* Function for inorder traversal *////////////////////////////////////////////////// /* public void inorder() { //TO DO by students } */ /* Function for postorder traversal *////////////////////////////////////////////////// /* public void postorder() { //TO DO by students }*/ /* Recursive approach to find height of binary tree *////////////////////////////////////////////////// /* public int findHeight(btNode root) { // TO DO by students } */

}

public void preorder preorder (root); private void preorder (btNode r) if (r null) System.out. print (r. getData() preorder (r.getLeft (r. getRight preorder(

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

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

Students also viewed these Databases questions

Question

Can you figure out who will read your application initially?

Answered: 1 week ago