/ An IntTree represents a binary tree of integers public class IntTree { private IntTreeNode overallRoot; public int evenBranches() { // TODO: Your code here } public static void main(String[] args) { System.out.println(new IntTree("[2 [8 [0] null] [1 [7 [4] null] [6 null [9]]]]").evenBranches()); } // Constructs a tree with default numbers public IntTree() { overallRoot = null; } // Constructs a tree from the given text representation public IntTree(String s) { overallRoot = fromString(s); } // post: Prints the numbers in this tree in a pre-order fashion. public void print() { print(overallRoot); } private void print(IntTreeNode root) { if (root != null) { System.out.print(root.data + " "); print(root.left); print(root.right); } } // post: Returns true if o is an IntTree with the same values public boolean equals(Object o) { if (this == o) { return true; } else if (!(o instanceof IntTree)) { return false; } else { IntTree other = (IntTree) o; return toString().equals(other.toString()); } } // post: Returns a text representation of the tree public String toString() { return toString(overallRoot); } private String toString(IntTreeNode root) { if (root == null) { return "null"; } else if (root.left == null && root.right == null) { return "[" + root.data + "]"; } else { return "[" + root.data + " " + toString(root.left) + " " + toString(root.right) + "]"; } } // An IntTreeNode represents a single node in a binary tree private static class IntTreeNode { public int data; public IntTreeNode left; public IntTreeNode right; // post: Constructs a leaf node with given data public IntTreeNode(int data) { this(data, null, null); } // post: Constructs a leaf or branch node with given data and links public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) { this.data = data; this.left = left; this.right = right; } } private static IntTreeNode fromString(String s) { s = s.trim(); if (s.isEmpty() || s.equals("null")) { return null; } s = s.substring(1, s.length() - 1); try { return new IntTreeNode(Integer.parseInt(s.trim())); } catch (NumberFormatException e) { String[] pair = s.trim().split(" +", 2); int data = Integer.parseInt(pair[0]); int index = splitIndex(pair[1]); String left = pair[1].substring(0, index); String right = pair[1].substring(index); return new IntTreeNode(data, fromString(left), fromString(right)); } } private static int splitIndex(String s) { if (s.startsWith("null")) { return 4; } int brackets = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '[') { brackets += 1; } else if (c == ']') { brackets -= 1; } if (brackets == 0) { return i + 1; } else if (brackets < 0) { throw new IllegalArgumentException("bad brackets: " + s); } } throw new IllegalArgumentException("bad brackets: " + s); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

// An IntTree represents a binary tree of integers

public class IntTree {
private IntTreeNode overallRoot;

public int evenBranches() {
// TODO: Your code here
}

public static void main(String[] args) {
System.out.println(new IntTree("[2 [8 [0] null] [1 [7 [4] null] [6 null [9]]]]").evenBranches());
}

// Constructs a tree with default numbers
public IntTree() {
overallRoot = null;
}

// Constructs a tree from the given text representation
public IntTree(String s) {
overallRoot = fromString(s);
}

// post: Prints the numbers in this tree in a pre-order fashion.
public void print() {
print(overallRoot);
}

private void print(IntTreeNode root) {
if (root != null) {
System.out.print(root.data + " ");
print(root.left);
print(root.right);
}
}

// post: Returns true if o is an IntTree with the same values
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof IntTree)) {
return false;
} else {
IntTree other = (IntTree) o;
return toString().equals(other.toString());
}
}

// post: Returns a text representation of the tree
public String toString() {
return toString(overallRoot);
}

private String toString(IntTreeNode root) {
if (root == null) {
return "null";
} else if (root.left == null && root.right == null) {
return "[" + root.data + "]";
} else {
return "[" + root.data + " "
+ toString(root.left) + " "
+ toString(root.right) + "]";
}
}

// An IntTreeNode represents a single node in a binary tree
private static class IntTreeNode {
public int data;
public IntTreeNode left;
public IntTreeNode right;

// post: Constructs a leaf node with given data
public IntTreeNode(int data) {
this(data, null, null);
}

// post: Constructs a leaf or branch node with given data and links
public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) {
this.data = data;
this.left = left;
this.right = right;
}
}

private static IntTreeNode fromString(String s) {
s = s.trim();
if (s.isEmpty() || s.equals("null")) {
return null;
}
s = s.substring(1, s.length() - 1);
try {
return new IntTreeNode(Integer.parseInt(s.trim()));
} catch (NumberFormatException e) {
String[] pair = s.trim().split(" +", 2);
int data = Integer.parseInt(pair[0]);
int index = splitIndex(pair[1]);
String left = pair[1].substring(0, index);
String right = pair[1].substring(index);
return new IntTreeNode(data, fromString(left), fromString(right));
}
}

private static int splitIndex(String s) {
if (s.startsWith("null")) {
return 4;
}
int brackets = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '[') {
brackets += 1;
} else if (c == ']') {
brackets -= 1;
}
if (brackets == 0) {
return i + 1;
} else if (brackets < 0) {
throw new IllegalArgumentException("bad brackets: " + s);
}
}
throw new IllegalArgumentException("bad brackets: " + s);
}
}

evenBranches
This checkpoint assessment is to be completed individually.
Write a method evenBranches that returns the number of branch nodes in a binary tree that
contain even numbers. A branch node is one that has one or two children (i.e., not a leaf node). For
example, if a variable tree stores a reference to the following tree:
+---+
2 |
/
| 8 |
| 1 |
| 6 |
| 9 |
Then the call tree.evenBranches (); should return 3 because there are three branch nodes with
even values ( 2, 8, and 6). Notice that the leaf nodes with even values are not included (the nodes
storing o and 4).
Transcribed Image Text:evenBranches This checkpoint assessment is to be completed individually. Write a method evenBranches that returns the number of branch nodes in a binary tree that contain even numbers. A branch node is one that has one or two children (i.e., not a leaf node). For example, if a variable tree stores a reference to the following tree: +---+ 2 | / | 8 | | 1 | | 6 | | 9 | Then the call tree.evenBranches (); should return 3 because there are three branch nodes with even values ( 2, 8, and 6). Notice that the leaf nodes with even values are not included (the nodes storing o and 4).
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY