Write a program to read in as set of numbers and build an ordered binary tree. F
ID: 3690436 • Letter: W
Question
Write a program to read in as set of numbers and build an ordered binary tree. For the given the tree compute and print the following
a. Print out the tree in inorder
b. Print out the tree in preorder.
c. Print out the tree in postorder.
d. Print out the number of nodes in the tree. (Traverse the tree and count the nodes)
e. Sum up all the values in the tree and compute the average. Print Sum and Average
f. Print out the average of all the values that are leafs.
g. Print the value that is the deepest down in the tree.
h. Print out all the nodes with one child.
Label all output.
Input file is: treeData.txt
Explanation / Answer
solution:
d)
Output:
e)
f)
// Java program to print all the node to leaf path
// A binary tree node
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
}
class BinaryTree {
static Node root;
/*Given a binary tree, print out all of its root-to-leaf
paths, one per line. Uses a recursive helper to do the work.*/
void printPaths(Node node) {
int path[] = new int[1000];
printPathsRecur(node, path, 0);
}
/* Recursive helper function -- given a node, and an array containing
the path from the root node up to but not including this node,
print out all the root-leaf paths.*/
void printPathsRecur(Node node, int path[], int pathLen) {
if (node == null) {
return;
}
/* append this node to the path array */
path[pathLen] = node.data;
pathLen++;
/* it's a leaf, so print the path that led to here */
if (node.left == null && node.right == null) {
printArray(path, pathLen);
} else {
/* otherwise try both subtrees */
printPathsRecur(node.left, path, pathLen);
printPathsRecur(node.right, path, pathLen);
}
}
/* Utility function that prints out an array on a line. */
void printArray(int ints[], int len) {
int i;
for (i = 0; i < len; i++) {
System.out.print(ints[i] + " ");
}
System.out.println("");
}
// driver program to test above functions
public static void main(String args[]) {
BinaryTree tree = new BinaryTree();
tree.root = new Node(10);
tree.root.left = new Node(8);
tree.root.right = new Node(2);
tree.root.left.left = new Node(3);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(2);
tree.printPaths(root);
}
}
g)
}
h)
// Check if each internal node of BST has only one child
class BinaryTree {
boolean hasOnlyOneChild(int pre[], int size) {
int nextDiff, lastDiff;
for (int i = 0; i < size - 1; i++) {
nextDiff = pre[i] - pre[i + 1];
lastDiff = pre[i] - pre[size - 1];
if (nextDiff * lastDiff < 0) {
return false;
};
}
return true;
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
int pre[] = new int[]{8, 3, 5, 7, 6};
int size = pre.length;
if (tree.hasOnlyOneChild(pre, size) == true) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
// Java program to print all the node to leaf path
// A binary tree node
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
}
class BinaryTree {
static Node root;
/*Given a binary tree, print out all of its root-to-leaf
paths, one per line. Uses a recursive helper to do the work.*/
void printPaths(Node node) {
int path[] = new int[1000];
printPathsRecur(node, path, 0);
}
/* Recursive helper function -- given a node, and an array containing
the path from the root node up to but not including this node,
print out all the root-leaf paths.*/
void printPathsRecur(Node node, int path[], int pathLen) {
if (node == null) {
return;
}
/* append this node to the path array */
path[pathLen] = node.data;
pathLen++;
/* it's a leaf, so print the path that led to here */
if (node.left == null && node.right == null) {
printArray(path, pathLen);
} else {
/* otherwise try both subtrees */
printPathsRecur(node.left, path, pathLen);
printPathsRecur(node.right, path, pathLen);
}
}
/* Utility function that prints out an array on a line. */
void printArray(int ints[], int len) {
int i;
for (i = 0; i < len; i++) {
System.out.print(ints[i] + " ");
}
System.out.println("");
}
// driver program to test above functions
public static void main(String args[]) {
BinaryTree tree = new BinaryTree();
tree.root = new Node(10);
tree.root.left = new Node(8);
tree.root.right = new Node(2);
tree.root.left.left = new Node(3);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(2);
tree.printPaths(root);
}
}
g)
public class DeepestNode { int deepestlevel; int value; public int Deep(Node root) { find(root, 0); return value; } public void find(Node root, int level) { if (root != null) { find(root.left, ++level); if (level > deepestlevel) { value = root.data; deepestlevel = level; } find(root.right, level); } } public static void main(String args[]) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.left = new Node(6); root.right.right = new Node(7); root.right.right.right = new Node(8); DeepestNode dp = new DeepestNode(); System.out.println("Deepest child is: " + dp.Deep(root)); } } class Node { int data; Node left; Node right; public Node(int data) { this.data = data; this.left = null; this.right = null; }}
h)
// Check if each internal node of BST has only one child
class BinaryTree {
boolean hasOnlyOneChild(int pre[], int size) {
int nextDiff, lastDiff;
for (int i = 0; i < size - 1; i++) {
nextDiff = pre[i] - pre[i + 1];
lastDiff = pre[i] - pre[size - 1];
if (nextDiff * lastDiff < 0) {
return false;
};
}
return true;
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
int pre[] = new int[]{8, 3, 5, 7, 6};
int size = pre.length;
if (tree.hasOnlyOneChild(pre, size) == true) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.