COS 312 DAY 25 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 7 Not corrected – 3 MIA Quiz 3 May 4 – Chaps 12-19 – Available 9 AM - 5 PM – 20 M/C 10.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Fundamentals of Python: From First Programs Through Data Structures
Trees. 2 Tree Concepts Previous data organizations place data in linear order Some data organizations require categorizing data into groups, subgroups.
Chapter 13 Linked Structures - Stacks. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked implementation.
Chapter 19 Trees. Chapter Scope Trees as data structures Tree terminology Tree implementations Analyzing tree efficiency Tree traversals Expression trees.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
Marc Smith and Jim Ten Eyck
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Chapter 12 Introduction to Collections - Stacks
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 9: Trees Java Software Structures: Designing and Using Data.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 19 Trees. Chapter Scope Trees as data structures Tree terminology Tree implementations Analyzing tree efficiency Tree traversals Expression trees.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
Chapter 10 Trees – part B.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 10: Binary Search Trees Java Software Structures: Designing.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 10-A Trees Modified
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
1 Trees and Binary Search Trees Using binary trees Definition of a binary search tree Implementing binary search trees –Add Element –Remove Element Using.
Chapter 20 Binary Search Trees
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
COS 312 DAY 19 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 6 Posted – Due April 16 Layers using non-opaque panels.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Collections Chapter 12 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
1 Trees and Binary Search Trees Using binary trees Definition of a binary search tree Implementing binary search trees –Add Element –Remove Element Using.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
2/11/ IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees Chapter 19, 20 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Collections & Stacks Collections A collection is an object that holds and organizes other objects It provides operations for accessing and managing.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
The Tree ADT.
Trees Chapter 15.
Trees and Binary Search Trees
Trees Another Abstract Data Type (ADT)
Trees Tree nomenclature Implementation strategies Traversals
Chapter 21 Heaps and Priority Queues
Recall: Nodes and Linked Lists
Java Software Structures: John Lewis & Joseph Chase
Trees Another Abstract Data Type (ADT)
Trees Another Abstract Data Type (ADT)
Chapter 20 Binary Search Trees
Chapter 21 Heaps and Priority Queues
Presentation transcript:

COS 312 DAY 25 Tony Gauvin

Ch 1 -2 Agenda Questions? Assignment 7 Not corrected – 3 MIA Quiz 3 May 4 – Chaps – Available 9 AM - 5 PM – 20 M/C 10 Short Answer – One Two bonus question Capstone Presentation / demonstration – Send me all your capstone material…code, notes, presentations etc.

Final Countdown Today – Trees – Assignment 7 due Monday, May 1 PM – Capstone Presentations – Quiz 3 available from 9AM-5PM Java Foundations, 3rd Edition, Lewis/DePasquale/Chase5 - 3

Chapter 19 Trees

Chapter Scope Trees as data structures Tree terminology Tree implementations Analyzing tree efficiency Tree traversals Expression trees Java Foundations, 3rd Edition, Lewis/DePasquale/Chase19 - 5

Trees A tree is a non-linear structure in which elements are organized into a hierarchy A tree is comprised of a set of nodes in which elements are stored and edges connect one node to another Each node is located on a particular level There is only one root node in the tree Java Foundations, 3rd Edition, Lewis/DePasquale/Chase19 - 6

Trees Nodes at the lower level of a tree are the children of nodes at the previous level A node can have only one parent, but may have multiple children Nodes that have the same parent are siblings The root is the only node which has no parent Java Foundations, 3rd Edition, Lewis/DePasquale/Chase19 - 7

Trees A node that has no children is a leaf node A node that is not the root and has at least one child is an internal node A subtree is a tree structure that makes up part of another tree We can follow a path through a tree from parent to child, starting at the root A node is an ancestor of another node if it is above it on the path from the root. Java Foundations, 3rd Edition, Lewis/DePasquale/Chase19 - 8

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase5 - 9 root This leaf is a Child of 54 This internal Node is the Parent of 67 Siblings Path from root to leaf Node 17 is ancestor of Node 9

Trees Nodes that can be reached by following a path from a particular node are the descendants of that node The level of a node is the length of the path from the root to the node The path length is the number of edges followed to get from the root to the node The height of a tree is the length of the longest path from the root to a leaf Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Trees Java Foundations, 3rd Edition, Lewis/DePasquale/Chase Height of A is 3 Level of B is 1 Path length of G is 3

Classifying Trees Trees can be classified in many ways One important criterion is the maximum number of children any node in the tree may have This may be referred to as the order of the tree General trees have no limit to the number of children a node may have A tree that limits each node to no more than n children is referred to as an n-ary tree – A linked list is a unary tree Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Balanced Trees Trees in which nodes may have at most two children are called binary trees A tree is balanced if all of the leaves of the tree are on the same level or within one level of each other Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Full and Complete Trees A balanced n-ary tree with m elements will have a height of log n m A balanced binary tree with n nodes has a height of log 2 n An n-ary tree is full if all leaves of the tree are at the same height and every non-leaf node has exactly n children (cannot add a leaf without creating an new level) A tree is complete if it is full, or full to the next-to- last level with all leaves at the bottom level on the left side of the tree Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Full and Complete Trees Three complete trees: Only tree c is full All three are balanced Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Implementing Trees An obvious choice for implementing trees is a linked structure Array-based implementations are the less obvious choice, but are sometimes useful Let's first look at the strategy behind two array- based implementations Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Computed Child Links For full or complete binary trees, we can use an array to represent a tree For any element stored in position n, (top to bottom, left to right) – the element’s left child is stored in array position (2n+1) – the element’s right child is stored in array position (2*(n+1)) If the represented tree is not complete or relatively complete, this approach can waste large amounts of array space Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Computed Child Links Java Foundations, 3rd Edition, Lewis/DePasquale/Chase Left = 2N + 1 Right = 2(N+1) Root = 0

Simulated Child Links Each element of the array is an object that stores a reference to the tree element and the array index of each child This approach is modeled after the way operating systems manage memory Array positions are allocated on a first-come, first-served basis Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Simulated Child Links Java Foundations, 3rd Edition, Lewis/DePasquale/Chase element Index left Index right Elements Added in following order A, C, B, E, D, F

Tree Traversals For linear structures, the process of iterating through the elements is fairly obvious (forwards or backwards) For non-linear structures like a tree, the possibilities are more interesting Let's look at four classic ways of traversing the nodes of a tree All traversals start at the root of the tree Each node can be thought of as the root of a subtree Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Tree Traversals Preorder: visit the root, then traverse the subtrees from left to right – Root  Left  Right Inorder: traverse the left subtree, then visit the root, then traverse the right subtree – Left  Root  Right Postorder: traverse the subtrees from left to right, then visit the root – Left  Right  Root Level-order: visit each node at each level of the tree from top (root) to bottom and left to right Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Tree Traversals Preorder:A B D E C Inorder:D B E A C Postorder:D E B C A Level-Order:A B C D E Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Tree Traversals Recursion simplifies the implementation of tree traversals Preorder (pseudocode): Visit node Traverse (left child) Traverse (right child) Inorder: Traverse (left child) Visit node Traverse (right child) Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Tree Traversals Postorder: Traverse (left child) Traverse (right child) Visit node A level-order traversal is more complicated It requires the use of extra structures (such as queues and/or lists) to create the necessary order Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

A Binary Tree ADT Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

A Binary Tree ADT Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

package jsjf; import java.util.Iterator; /** * BinaryTreeADT defines the interface to a binary tree data structure. * Java Foundations 4.0 */ public interface BinaryTreeADT { /** * Returns a reference to the root element * a reference to the root */ public T getRootElement(); /** * Returns true if this binary tree is empty and false otherwise. * true if this binary tree is empty, false otherwise */ public boolean isEmpty(); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Returns the number of elements in this binary tree. * the number of elements in the tree */ public int size(); /** * Returns true if the binary tree contains an element that matches * the specified element and false otherwise. * targetElement the element being sought in the tree true if the tree contains the target element */ public boolean contains(T targetElement); /** * Returns a reference to the specified element if it is found in * this binary tree. Throws an exception if the specified element * is not found. * targetElement the element being sought in the tree a reference to the specified element */ public T find(T targetElement); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Returns the string representation of this binary tree. * a string representation of the binary tree */ public String toString(); /** * Returns an iterator over the elements of this tree. * an iterator over the elements of this binary tree */ public Iterator iterator(); /** * Returns an iterator that represents an inorder traversal on this binary tree. * an iterator over the elements of this binary tree */ public Iterator iteratorInOrder(); /** * Returns an iterator that represents a preorder traversal on this binary tree. * an iterator over the elements of this binary tree */ public Iterator iteratorPreOrder(); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Returns an iterator that represents a postorder traversal on this binary tree. * an iterator over the elements of this binary tree */ public Iterator iteratorPostOrder(); /** * Returns an iterator that represents a levelorder traversal on the binary tree. * an iterator over the elements of this binary tree */ public Iterator iteratorLevelOrder(); } Code\chap19\jsjf\BinaryTreeADT.java Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Expression Trees An expression tree is a tree that shows the relationships among operators and operands in an expression An expression tree is evaluated from the bottom up Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

xxx Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

import jsjf.*; /** * ExpressionTreeOp represents an element in an expression tree. * Java Foundations 4.0 */ public class ExpressionTreeOp { private int termType; private char operator; private int value; /** * Creates a new expression tree object with the specified data. * type the integer type of the expression op the operand for the expression val the value for the expression */ public ExpressionTreeOp(int type, char op, int val) { termType = type; operator = op; value = val; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Returns true if this object is an operator and false otherwise. * true if this object is an operator, false otherwise */ public boolean isOperator() { return (termType == 1); } /** *Returns the operator of this expression tree object. * the character representation of the operator */ public char getOperator() { return operator; } /** * Returns the value of this expression tree object. * the value of this expression tree object */ public int getValue() { return value; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

public String toString() { if (termType == 1) return operator + ""; else return value + ""; } Code\chap19\ExpressionTreeOp.java Code\chap19\jsjf\BinaryTreeNode.java Code\chap19\jsjf\LinkedBinaryTree.java Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

import jsjf.*; /** * ExpressionTree represents an expression tree of operators and operands. * Java Foundations 4.0 */ public class ExpressionTree extends LinkedBinaryTree { /** * Creates an empty expression tree. */ public ExpressionTree() { super(); } Code\chap19\ExpressionTree.java Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Constructs a expression tree from the two specified expression * trees. * element the expression tree for the center leftSubtree the expression tree for the left subtree rightSubtree the expression tree for the right subtree */ public ExpressionTree(ExpressionTreeOp element, ExpressionTree leftSubtree, ExpressionTree rightSubtree) { root = new BinaryTreeNode (element, leftSubtree, rightSubtree); } /** * Evaluates the expression tree by calling the recursive * evaluateNode method. * the integer evaluation of the tree */ public int evaluateTree() { return evaluateNode(root); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Recursively evaluates each node of the tree. * root the root of the tree to be evaluated the integer evaluation of the tree */ public int evaluateNode(BinaryTreeNode root) { int result, operand1, operand2; ExpressionTreeOp temp; if (root==null) result = 0; else { temp = (ExpressionTreeOp)root.getElement(); if (temp.isOperator()) { operand1 = evaluateNode(root.getLeft()); operand2 = evaluateNode(root.getRight()); result = computeTerm(temp.getOperator(), operand1, operand2); } else result = temp.getValue(); } return result; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Evaluates a term consisting of an operator and two operands. * operator the operator for the expression operand1 the first operand for the expression operand2 the second operand for the expression */ private static int computeTerm(char operator, int operand1, int operand2) { int result=0; if (operator == '+') result = operand1 + operand2; else if (operator == '-') result = operand1 - operand2; else if (operator == '*') result = operand1 * operand2; else result = operand1 / operand2; return result; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Generates a structured string version of the tree by performing * a levelorder traversal. * a string representation of this binary tree */ public String printTree() { UnorderedListADT > nodes = new ArrayUnorderedList >(); UnorderedListADT levelList = new ArrayUnorderedList (); BinaryTreeNode current; String result = ""; int printDepth = this.getHeight(); int possibleNodes = (int)Math.pow(2, printDepth + 1); int countNodes = 0; nodes.addToRear(root); Integer currentLevel = 0; Integer previousLevel = -1; levelList.addToRear(currentLevel); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

while (countNodes < possibleNodes) { countNodes = countNodes + 1; current = nodes.removeFirst(); currentLevel = levelList.removeFirst(); if (currentLevel > previousLevel) { result = result + "\n\n"; previousLevel = currentLevel; for (int j = 0; j < ((Math.pow(2, (printDepth - currentLevel))) - 1); j++) result = result + " "; } else { for (int i = 0; i < ((Math.pow(2, (printDepth - currentLevel + 1)) - 1)) ; i++) { result = result + " "; } if (current != null) { result = result + (current.getElement()).toString(); nodes.addToRear(current.getLeft()); levelList.addToRear(currentLevel + 1); nodes.addToRear(current.getRight()); levelList.addToRear(currentLevel + 1); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

else { nodes.addToRear(null); levelList.addToRear(currentLevel + 1); nodes.addToRear(null); levelList.addToRear(currentLevel + 1); result = result + " "; } return result; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Expression Tree Evaluation Let's look at an example that creates an expression tree from a postfix expression and then evaluates it This is a modification of an earlier solution It uses a stack of expression trees to build the complete expression tree Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

xxx Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

import java.util.Scanner; /** * Demonstrates the use of an expression tree to evaluate postfix expressions. * Java Foundations 4.0 */ public class PostfixTester { /** * Reads and evaluates multiple postfix expressions. */ public static void main(String[] args) { String expression, again; int result; Scanner in = new Scanner(System.in); do { PostfixEvaluator evaluator = new PostfixEvaluator(); System.out.println("Enter a valid post-fix expression one token " + "at a time with a space between each token (e.g *)"); System.out.println("Each token must be an integer or an operator (+,-,*,/)"); expression = in.nextLine(); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

result = evaluator.evaluate(expression); System.out.println(); System.out.println("That expression equals " + result); System.out.println("The Expression Tree for that expression is: "); System.out.println(evaluator.getTree()); System.out.print("Evaluate another expression [Y/N]? "); again = in.nextLine(); System.out.println(); } while (again.equalsIgnoreCase("y")); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

import jsjf.*; import jsjf.exceptions.*; import java.util.*; import java.io.*; /** * PostfixEvaluator this modification of our stack example uses a * stack to create an expression tree from a VALID integer postfix expression * and then uses a recursive method from the ExpressionTree class to * evaluate the tree. * Java Foundations 4.0 */ public class PostfixEvaluator { private String expression; private Stack treeStack; /** * Sets up this evalutor by creating a new stack. */ public PostfixEvaluator() { treeStack = new Stack (); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Retrieves and returns the next operand off of this tree stack. * treeStack the tree stack from which the operand will be returned the next operand off of this tree stack */ private ExpressionTree getOperand(Stack treeStack) { ExpressionTree temp; temp = treeStack.pop(); return temp; } /** * Evaluates the specified postfix expression by building and evaluating * an expression tree. * expression string representation of a postfix expression value of the given expression */ public int evaluate(String expression) { ExpressionTree operand1, operand2; char operator; String tempToken; Scanner parser = new Scanner(expression); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

while (parser.hasNext()) { tempToken = parser.next(); operator = tempToken.charAt(0); if ((operator == '+') || (operator == '-') || (operator == '*') || (operator == '/')) { operand1 = getOperand(treeStack); operand2 = getOperand(treeStack); treeStack.push(new ExpressionTree (new ExpressionTreeOp(1,operator,0), operand2, operand1)); } else { treeStack.push(new ExpressionTree(new ExpressionTreeOp (2,' ',Integer.parseInt(tempToken)), null, null)); } } return (treeStack.peek()).evaluateTree(); } /** * Returns the expression tree associated with this postfix evaluator. * string representing the expression tree */ public String getTree() { return (treeStack.peek()).printTree(); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

xxx Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Decision Trees A decision tree is a tree whose nodes represent decision points, and whose children represent the options available The leaves of a decision tree represent the possible conclusions that might be drawn A simple decision tree, with yes/no questions, can be modeled by a binary tree Decision trees are useful in diagnostic situations (medical, car repair, etc.) Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Decision Trees A simplified decision tree for diagnosing back pain: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

import java.io.*; /** * BackPainAnaylyzer demonstrates the use of a binary decision tree to * diagnose back pain. */ public class BackPainAnalyzer { /** * Asks questions of the user to diagnose a medical problem. */ public static void main (String[] args) throws FileNotFoundException { System.out.println ("So, you're having back pain."); DecisionTree expert = new DecisionTree("input.txt"); expert.evaluate(); } Code\chap19\BackPainAnalyzer.java Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

import jsjf.*; import java.util.*; import java.io.*; /** * The DecisionTree class uses the LinkedBinaryTree class to implement * a binary decision tree. Tree elements are read from a given file and * then the decision tree can be evaluated based on user input using the * evaluate method. * Java Foundations 4.0 */ public class DecisionTree { private LinkedBinaryTree tree; /** * Builds the decision tree based on the contents of the given file * filename the name of the input file FileNotFoundException if the input file is not found */ public DecisionTree(String filename) throws FileNotFoundException { File inputFile = new File(filename); Scanner scan = new Scanner(inputFile); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

int numberNodes = scan.nextInt(); scan.nextLine(); int root = 0, left, right; List > nodes = new java.util.ArrayList >(); for (int i = 0; i < numberNodes; i++) nodes.add(i,new LinkedBinaryTree (scan.nextLine())); while (scan.hasNext()) { root = scan.nextInt(); left = scan.nextInt(); right = scan.nextInt(); scan.nextLine(); nodes.set(root, new LinkedBinaryTree ((nodes.get(root)).getRootElement(), nodes.get(left), nodes.get(right))); } tree = nodes.get(root); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Follows the decision tree based on user responses. */ public void evaluate() { LinkedBinaryTree current = tree; Scanner scan = new Scanner(System.in); while (current.size() > 1) { System.out.println (current.getRootElement()); if (scan.nextLine().equalsIgnoreCase("N")) current = current.getLeft(); else current = current.getRight(); } System.out.println (current.getRootElement()); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Implementing Binary Trees with Links Now let's explore an implementation of a binary tree using links The LinkedBinaryTree class holds a reference to the root node The BinaryTreeNode class represents each node, with links to a possible left and/or right child Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

package jsjf; /** * BinaryTreeNode represents a node in a binary tree with a left and * right child. * Java Foundations 4.0 */ public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; /** * Creates a new tree node with the specified data. * obj the element that will become a part of the new tree node */ public BinaryTreeNode(T obj) { element = obj; left = null; right = null; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Creates a new tree node with the specified data. * obj the element that will become a part of the new tree node left the tree that will be the left subtree of this node right the tree that will be the right subtree of this node */ public BinaryTreeNode(T obj, LinkedBinaryTree left, LinkedBinaryTree right) { element = obj; if (left == null) this.left = null; else this.left = left.getRootNode(); if (right == null) this.right = null; else this.right = right.getRootNode(); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Returns the number of non-null children of this node. * the integer number of non-null children of this node */ public int numChildren() { int children = 0; if (left != null) children = 1 + left.numChildren(); if (right != null) children = children right.numChildren(); return children; } /** * Return the element at this node. * the element stored at this node */ public T getElement() { return element; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Return the right child of this node. * the right child of this node */ public BinaryTreeNode getRight() { return right; } /** * Sets the right child of this node. * node the right child of this node */ public void setRight(BinaryTreeNode node) { right = node; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Return the left child of this node. * the left child of the node */ public BinaryTreeNode getLeft() { return left; } /** * Sets the left child of this node. * node the left child of this node */ public void setLeft(BinaryTreeNode node) { left = node; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

package jsjf; import java.util.*; import jsjf.exceptions.*; /** * LinkedBinaryTree implements the BinaryTreeADT interface * Java Foundations 4.0 */ public class LinkedBinaryTree implements BinaryTreeADT, Iterable { protected BinaryTreeNode root; protected int modCount; /** * Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Creates a binary tree with the specified element as its root. * element the element that will become the root of the binary tree */ public LinkedBinaryTree(T element) { root = new BinaryTreeNode (element); } /** * Creates a binary tree with the specified element as its root and the * given trees as its left child and right child * element the element that will become the root of the binary tree left the left subtree of this tree right the right subtree of this tree */ public LinkedBinaryTree(T element, LinkedBinaryTree left, LinkedBinaryTree right) { root = new BinaryTreeNode (element); root.setLeft(left.root); root.setRight(right.root); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Returns a reference to the specified target element if it is * found in this binary tree. Throws a ElementNotFoundException if * the specified target element is not found in the binary tree. * targetElement the element being sought in this tree a reference to the specified target ElementNotFoundException if the element is not in the tree */ public T find(T targetElement) throws ElementNotFoundException { BinaryTreeNode current = findNode(targetElement, root); if (current == null) throw new ElementNotFoundException("LinkedBinaryTree"); return (current.getElement()); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Returns a reference to the specified target element if it is * found in this binary tree. * targetElement the element being sought in this tree next the element to begin searching from */ private BinaryTreeNode findNode(T targetElement, BinaryTreeNode next) { if (next == null) return null; if (next.getElement().equals(targetElement)) return next; BinaryTreeNode temp = findNode(targetElement, next.getLeft()); if (temp == null) temp = findNode(targetElement, next.getRight()); return temp; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Performs an inorder traversal on this binary tree by calling an * overloaded, recursive inorder method that starts with * the root. * an in order iterator over this binary tree */ public Iterator iteratorInOrder() { ArrayUnorderedList tempList = new ArrayUnorderedList (); inOrder(root, tempList); return new TreeIterator(tempList.iterator()); } /** * Performs a recursive inorder traversal. * node the node to be used as the root for this traversal tempList the temporary list for use in this traversal */ protected void inOrder(BinaryTreeNode node, ArrayUnorderedList tempList) { if (node != null) { inOrder(node.getLeft(), tempList); tempList.addToRear(node.getElement()); inOrder(node.getRight(), tempList); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase