### Summing and printing all values in a Binary Tree

• Binary Tree is also a recursive data structure

• Recursive nature of a Binary Tree:

 ``` Binary Tree = [ ] (empty Binary Tree) or: Node / \ / \ / \ / \ Bin Tree Bin Tree ```

Example:

• Fact:

 Because a Binary Tree is a recursive data structure, the recursive algorithm is ideally suited to perform certain operations on the Binary Tree

• Introductory example

• Problem description:

 Compute the sum of the values stored in a list

Example:

• Input Binary Tree:

The answer is: 3 + 6 + 9 + 1 + 4 + 8 + 2 + 7 = 40

• Developing a recursive solution

• Pretend you (= me) need to solve this problem:

This problem is:

 ``` sum( root ) // sum all values in the binary tree starting at root ```

• You make the following astute observation:

• If you can find 2 people (e.g., your friends Jack and Jill) to solve these smaller problem:

(These 2 problems are the left and right subtrees of the root node)

The solution of these smaller problems are:

 ``` leftSubTreeSum = 16 rightSubTreeSum = 21 ```

• You can solve your problem using the smaller solutions:

i.e., using this statement:

 ``` mySum = root.value + leftSubTreeSum + righSubTreeSum; ```

• The recusive solution in pseudo code:

 ``` compute sum( binary tree starting at root ): 1. leftSubTreeSum = sum( binary tree starting at root.left ); rightSubTreeSum = sum( binary tree starting at root.right ); // Solve 2 smaller problems 2. mySum = root.value + leftSubTreeSum + rightSubTreeSum; // Use the solution of // the smaller problems to // solve the original prob. ```

• The base case(s)

• The base case in the sum problem ofr a binary tree is:

• When the binary tree is empty

• How to detect this case:

 ``` if ( root == null ) ```

• The answer (sum) of this base case: 0

• The sum algorithm

• Java:

 ``` public static double sum( Node head ) { double mySum, leftSum, rightSum; if ( head == null ) { mySum = 0; // Solution for the base case return mySum; // Return solution } else { leftSum = sum( root.left ); // Solve smaller problem 1 rightSum = sum( root.right ); // Solve smaller problem 2 mySum = root.value + leftSum + rightSum; // Solve my problem using // solution of smaller problem return mySum; // Return solution } } ```

• Test program:

 ``` public static void main( String[] args ) { BST myBST = new BST(); // I use my old BST to create a Binary Tree myBST.put(3.0); myBST.put(6.0); myBST.put(9.0); myBST.put(1.0); myBST.put(4.0); myBST.put(8.0); myBST.put(2.0); myBST.put(7.0); System.out.println("myBST:"); myBST.printBST(); System.out.println(); double s; s = testProg.sum( myBST.root ); System.out.println("s = " + s); } ```

Output:

 ``` myBST: [9.0] [8.0] [7.0] [6.0] [4.0] [3.0] [2.0] [1.0] ================================ s = 40.0 ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac testProg.java To run:          java testProg

• Printing all values in a Binary Tree

• We can easily modify the sum algorithm to print the values in a list:

• Recall the sum algorithm:

 ``` public static double sum( Node head ) { double mySum, leftSum, rightSum; if ( head == null ) { mySum = 0; // Solution for the base case return mySum; // Return solution } else { leftSum = sum( root.left ); // Solve smaller problem 1 rightSum = sum( root.right ); // Solve smaller problem 2 mySum = root.value + leftSum + rightSum; // Solve my problem using // solution of smaller problem return mySum; // Return solution } } ```

• Instead of computing the sum, we just print the value root.value:

 ``` public static void print( Node head ) { double mySum, leftSum, rightSum; if ( head == null ) { return; // Done, nothing to print } else { print( root.left ); // Print the left tree print( root.right ); // Print the right tree System.out.print( root.value ); // Print the root value return; // Done } } ```

Just like printing a list, we can change the order of the 3 highlighted statements in the above algorithm.

• The 3 different ways to print a binary tree

• There are 3 different ways to print a binary tree:

• Prefix order:

 Print the root value before printing the left and right subtrees

• Infix order

 Print the root value between printing the left and right subtrees

• Postfix order

 Print the root value after printing the left and right subtrees

• The infix ordering

• I start with the infix order because this order is most commonly used in arithmetic expressions.

• Consider the following Binary Tree:

which represents the following arithmetic expression:

 ``` (3 + 4) * (1 - 2) ```

• Printing a Binary Tree in the infix order:

• We start at the root node: print its left tree first, then print the root node and finally print its right tree

• print the left tree will result in:

print its left tree first, then print the root node and finally print its right tree

• print the right tree will result in:

print its left tree first, then print the root node and finally print its right tree

• Result:

 ``` 3 + 4 * 1 - 2 ```

(No braces, but we can improve the printing....)

• The algorithm to print a Binary Tree in infix order is:

 ``` public static void print( Node head ) { double mySum, leftSum, rightSum; if ( head == null ) { return; // Done, nothing to print } else { // System.out.print( "(" ); // Improves printing print( root.left ); // Print the left tree System.out.print( root.value ); // Print the root value // BETWEEN the left and right subtrees print( root.right ); // Print the right tree // System.out.print( ")" ); // Improves printing return; // Done } } ```

Output:

 ``` Binary tree:: [2] [-] [1] [*] [4] [+] [3] ================================ Output: 3 + 4 * 1 - 2 (No braces, you can improve it) ```

You can improves the printing with braces when you uncomment the 2 additional print statements:

The output will become:

 ``` (((3 )+ (4 ))* ((1 )- (2 ))) ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac infix.java To run:          java infix

• The postfix ordering

• I discuss the postfix order next because this order is second most commonly used notation in arithmetic expressions.

• The postfix order is also known as the reverse Polish notation used to denote arithmetic expressions (see: click here)

 ``` 3 4 + means: 3 + 4 ```

Rule:

 whenever you see an operator, the operands used are the previous 2 values

Example:

 ``` 3 4 + 1 2 - * = 3 4 + 1 2 - * = (3 + 4) (1 - 2) * = (3 + 4 * (1 - 2) infix notation ```

• Printing a Binary Tree in the infix order:

• Suppose we print the following Binary Tree in postfix order:

• We start at the root node: print its left tree first, then print its right tree and finally print the root node

• print the left tree will result in:

• print its left tree first, then print its right tree and finally print the root node

• print the right tree will result in:

• print its left tree first, then print its right tree and finally print the root node

• Result:

 ``` 3 4 + 1 2 - * ```

As I said before: postfix order is the same as reverse Polish notation.

• The algorithm to print a Binary Tree in postfix order is:

 ``` public static void print( Node head ) { double mySum, leftSum, rightSum; if ( head == null ) { return; // Done, nothing to print } else { print( root.left ); // Print the left tree print( root.right ); // Print the right tree System.out.print( root.value ); // Print the root value LAST return; // Done } } ```

Output:

 ``` Binary tree:: [2] [-] [1] [*] [4] [+] [3] ================================ Output: 3 4 + 1 2 - * ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac postfix.java To run:          java postfix

• HP Calculators

• Some of the HP calculators use the reverse Polish notation as entry format !!!

• If you want to compute 3 + 4, you enter:

 3, 4 and + !!!

• These calculators did not need (have) the ( and ) keys !!!

E.g.:

• The prefix ordering

• The prefix order is also known as the Polish notation and can also be used to denote arithmetic expressions (see: click here)

 ``` + 3 4 means: 3 + 4 ```

Rule:

 whenever you see an operator, the operands used are the next 2 values

Example:

 ``` * + 3 4 - 1 2 = * + 3 4 - 1 2 = * (3 + 4) (1 - 2) = (3 + 4 * (1 - 2) infix notation ```

• Printing a Binary Tree in the prefix order:

• Suppose we print the following Binary Tree in prefix order:

• We start at the root node: print the root node first, then print its left tree and finally print its right tree

• print the left tree will result in:

• print the root node first, then print its left tree and finally print its right tree

• print the right tree will result in:

• print the root node first, then print its left tree and finally print its right tree

• Result:

 ``` * + 3 4 - 1 2 ```

The postfix order is the same as Polish notation.

• The algorithm to print a Binary Tree in postfix order is:

 ``` public static void print( Node head ) { double mySum, leftSum, rightSum; if ( head == null ) { return; // Done, nothing to print } else { System.out.print( root.value ); // Print the root value FIRST print( root.left ); // Print the left tree print( root.right ); // Print the right tree return; // Done } } ```

Output:

 ``` Binary tree:: [2] [-] [1] [*] [4] [+] [3] ================================ Output: * + 3 4 - 1 2 ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac prefix.java To run:          java prefix