Question
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.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started