Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Finish this binary tree code in Java 4. Tree Traversal. Complete the static meth

ID: 3570189 • Letter: F

Question

Finish this binary tree code in Java

4. Tree Traversal.

    Complete the static method treeEqual() that is part of the BTNode class below. treeEqual() should return true if the two binary trees passed to it are identical, and false otherwise.   (Two trees are considered equal if their structure is identical, and the data in corresponding nodes is the same.) You may assume that the equals(Object anotherObject) method is implemented for the data in each BTNode, and you may use it to compare data items.

  

public class BTNode {

    public BTNode getLeft()   { return left; }

    public BTNode getRight() { return right; }

    public Object getData()   { return data; }

    public void setLeft(BTNode l)   { left = l; }

    public void setRight(BTNode r) { right = r; }

    public void setData(Object d)   { data = d; }

   public void treeEqual(BTNode t1, BTNode t2) {} // you will write this below

    private Object data;

    private BTNode left;

    private BTNode right;

    } // BTNode

    public static boolean treeEqual(BTNode t1, BTNode t2) {

_______________________________________________________________________________

b. If you know that a plain binary tree of integers is full, compare the complexity of finding the number of elements in the binary tree with finding the number of elements in a sorted array. Explain.

d. If a binary search tree is full, which implementation would be more efficient, an array or a linked node representation? Explain.

Explanation / Answer

import java.util.Queue; import java.util.LinkedList; public class TreeTraverse { private static class Node { public Node left; public Node right; public T data; public Node(T data) { this.data = data; } public Node getLeft() { return this.left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return this.right; } public void setRight(Node right) { this.right = right; } } public static void preorder(Node n) { if (n != null) { System.out.print(n.data + " "); preorder(n.getLeft()); preorder(n.getRight()); } } public static void inorder(Node n) { if (n != null) { inorder(n.getLeft()); System.out.print(n.data + " "); inorder(n.getRight()); } } public static void postorder(Node n) { if (n != null) { postorder(n.getLeft()); postorder(n.getRight()); System.out.print(n.data + " "); } } public static void levelorder(Node n) { Queue>(); if (n != null) nodequeue.add(n); while (!nodequeue.isEmpty()) { Node next = nodequeue.remove(); System.out.print(next.data + " "); if (next.getLeft() != null) { nodequeue.add(next.getLeft()); } if (next.getRight() != null) { nodequeue.add(next.getRight()); } } } public static void main(final String[] args) { Node Node(1); Node two = new Node(2); Node three = new Node(3); Node four = new Node(4); Node five = new Node(5); Node six = new Node(6); Node seven = new Node(7); Node eight = new Node(8); Node nine = new Node(9); one.setLeft(two); one.setRight(three); two.setLeft(four); two.setRight(five); three.setLeft(six); four.setLeft(seven); six.setLeft(eight); six.setRight(nine); preorder(one); System.out.println(); inorder(one); System.out.println(); postorder(one); System.out.println(); levelorder(one); System.out.println(); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote