Big Java, Binder Ready Version: Early Objects
Big Java, Binder Ready Version: Early Objects
6th Edition
ISBN: 9781119056447
Author: Cay S. Horstmann
Publisher: WILEY
Question
Book Icon
Chapter 17, Problem 8PE
Program Plan Intro

Computing average value of the elements in the binary tree

Program plan:

BinaryTree.java:

  • Define the class “BinaryTree.java”.
  • Declare the private variable root from class “Node”.
  • Defining the constructor “BinaryTree” to generate an empty tree.
    • Assign root to null.
  • Define the parameterized constructor “BinaryTree(object rootData)” to generate a tree one root element.
    • Generate root node.
  • Define the parameterized constructor “public BinaryTree(Object rootData, BinaryTree left, BinaryTree right)”.
    • Generates root and children nodes.
  • Define the “preorder()” method for both tree with many nodes and a tree with one node.
    • For a tree with one node and no children,
      • “preorder(root,v)” is called.
        • For a tree with multiple nodes,
          • If (node == null) returns v.visit(node.data);
          • If (node.left != null) then, preorder(node.left, v);
          • If (node.right != null) then, preorder(node.right, v);
  • Define the “inorder()” method for both tree with many nodes and a tree with one node.
    • For a tree with one node and no children,
      • “inorder(root,v)” is called.
        • For a tree with multiple nodes,
          • If (node.left != null) then,  inorder(node.left, v) and v.visit(node.data);
          • If (node.right != null) then, inorder(node.right, v);
  • Define the “postorder()” method for both tree with many nodes and a tree with one node.
    • For a tree with one node and no children,
      • “postorder(root,v)” is called.
        • For a tree with multiple nodes,
          • If (node.left != null) then, postorder(v, node.left);
          • If (node.right != null) then, postorder(v, node.right) and v.visit(node.data);
  • Define the class “Node”.
    • Declare “object data”.
    • Declare “Node left”.
    • Declare “Node right”.
  • Define the function static “height()”
    • If (n == null) returns the null value.
    • Otherwise returns the value 1 + Math.max(height(n.left), height(n.right)).
  • Define the function “height()”
    • Returns the height of this tree.
  • Define “isEmpty()” function:
    • It checks whether this tree is empty.
    • Return true value if this tree is empty.
  • Define the “data()” function.
    • Gets the data at the root of this tree.
    • Returns the root data.
  • Define “left()” and “right()” function.
    • “left()” function return the left child of the root.
    • “right()” function return the right child of the root.

Visitor.java:

  • Define interface “Visitor”
    • Calls the “visit(object data)”

BinaryTreeTester.java:

  • Import the required header files.
  • Define “BinaryTreeTester” class.
    • Define the “main()” function.
      • Create an object “questionTree” for “BinaryTree” with tree values.
      • Define a class “Averager” which implements the “Visitor” class.
        • Define “visit()” method which compute the sum of elements of the binary tree.
        • Define “average()” method which computes the average value using “sum” value .
      • Create object for “Averager” class and call the “inorder()” function by passing the “Averager” class object.
      • Print the value of average.

Blurred answer

Chapter 17 Solutions

Big Java, Binder Ready Version: Early Objects

Ch. 17.2 - Prob. 11SCCh. 17.2 - Prob. 12SCCh. 17.3 - Prob. 13SCCh. 17.3 - Prob. 14SCCh. 17.3 - Prob. 15SCCh. 17.3 - Prob. 16SCCh. 17.3 - Prob. 17SCCh. 17.3 - Prob. 18SCCh. 17.4 - Prob. 19SCCh. 17.4 - Prob. 20SCCh. 17.4 - Prob. 21SCCh. 17.4 - Prob. 22SCCh. 17.4 - Prob. 23SCCh. 17.4 - Prob. 24SCCh. 17.5 - Prob. 25SCCh. 17.5 - Prob. 26SCCh. 17.5 - Prob. 27SCCh. 17.5 - Prob. 28SCCh. 17.5 - Prob. 29SCCh. 17.5 - Prob. 30SCCh. 17.6 - Prob. 31SCCh. 17.6 - Prob. 32SCCh. 17.6 - Prob. 33SCCh. 17.6 - Prob. 34SCCh. 17.6 - Prob. 35SCCh. 17.7 - Prob. 36SCCh. 17.7 - Prob. 37SCCh. 17.7 - Prob. 38SCCh. 17.7 - Prob. 39SCCh. 17.7 - Prob. 40SCCh. 17 - Prob. 1RECh. 17 - Prob. 2RECh. 17 - Prob. 3RECh. 17 - Prob. 4RECh. 17 - Prob. 5RECh. 17 - Prob. 6RECh. 17 - Prob. 7RECh. 17 - Prob. 8RECh. 17 - Prob. 9RECh. 17 - Prob. 10RECh. 17 - Prob. 11RECh. 17 - Prob. 12RECh. 17 - Prob. 13RECh. 17 - Prob. 14RECh. 17 - Prob. 16RECh. 17 - Prob. 18RECh. 17 - Prob. 19RECh. 17 - Prob. 20RECh. 17 - Prob. 21RECh. 17 - Prob. 22RECh. 17 - Prob. 23RECh. 17 - Prob. 24RECh. 17 - Prob. 25RECh. 17 - Prob. 26RECh. 17 - Prob. 27RECh. 17 - Prob. 28RECh. 17 - Prob. 1PECh. 17 - Prob. 2PECh. 17 - Prob. 3PECh. 17 - Prob. 4PECh. 17 - Prob. 5PECh. 17 - Prob. 6PECh. 17 - Prob. 7PECh. 17 - Prob. 8PECh. 17 - Prob. 9PECh. 17 - Prob. 10PECh. 17 - Prob. 11PECh. 17 - Prob. 12PECh. 17 - Prob. 13PECh. 17 - Prob. 1PPCh. 17 - Prob. 2PPCh. 17 - Prob. 3PPCh. 17 - Prob. 4PPCh. 17 - Prob. 5PPCh. 17 - Prob. 6PPCh. 17 - Prob. 7PPCh. 17 - Prob. 8PPCh. 17 - Prob. 9PPCh. 17 - Prob. 10PPCh. 17 - Prob. 11PP
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education