Trees CSCI 162. 1-2 Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Chapter 4: Trees Part II - AVL Tree
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
CS 171: Introduction to Computer Science II
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
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.
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.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 9: Trees Java Software Structures: Designing and Using Data.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 10 Trees – part B.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
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,
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 10: Binary Search Trees Java Software Structures: Designing.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Starting at Binary Trees
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.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chapter 20 Binary Search Trees
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
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.
1 CMSC 341 Introduction to Trees Textbook sections:
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.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
The Tree ADT.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Recursive Objects (Part 4)
Trees Tree nomenclature Implementation strategies Traversals
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
TREES General trees Binary trees Binary search trees AVL trees
Java Software Structures: John Lewis & Joseph Chase
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Lecture 12 CS203 1.
Binary Trees, Binary Search Trees
CHAPTER 11: Priority Queues and Heaps
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees CSCI 162

1-2 Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze tree implementations of collections Discuss methods for traversing trees Examine a binary tree example

1-3 Trees A Tree is a non-linear structure Consists of set of nodes and edges –Distinguished node called root –All nodes except root have exactly one parent For trees, the operations are dependent upon the type of tree and its use

1-4 Definitions We have already introduced a couple of terms: –Node -- refers to a location in the tree where an element is stored –Root -- the node at the base of the tree (the one that does not have a parent) –Edge – connects two nodes

1-5 Definitions Edges connect nodes Nodes directly subordinate to other node are referred to as children A child of a child is then called a grandchild, a child of a grandchild called a great-grandchild, etc. A node that has no children is a leaf A node that is not the root and has at least one child is called an internal node

1-6 Tree terminology

1-7 Definitions Any node below another node and on a path from that node is called a descendant of that node Any node above another node on a connecting path from the root to that node is called an ancestor of that node All children of the same node are called siblings A tree that limits each node to no more than n children is called an n-ary tree (degree of tree)

1-8 Definitions Each node of the tree is at a specific level or depth within the tree The level of a node is the length of the path from the root to the node This path length is determined by counting the number of edges that must be followed to get from the root to the node The root is considered to be level 0, the children of the root are at level 1, the grandchildren of the root are at level 2, and so on

1-9 Definitions The height or order of a tree is the length of the longest path from the root to a leaf Thus the height or order of the tree in the next slide is 3 The path from the root (A) to leaf (F) is of length 3 The path from the root (A) to leaf (C) is of length 1

1-10 Path length and level

1-11 Definitions A tree is considered to be balanced if all of the leaves of the tree are at roughly the same depth While the use of the term “roughly” may not be rigorous, the actual definition is dependent upon the algorithm being used Some algorithms define balanced as all of the leaves being at level h or h-1 where h is the height of the tree and where h = log N n for an N-ary tree

1-12 Balanced and unbalanced trees

1-13 Definitions The concept of a complete tree is related to the balance of a tree A tree is considered complete if it is balanced and all of the leaves at level h are on the left side of the tree (all levels full except possibly last) Complete trees can be stored level-wise in an array without gaps Trees a, b, and c on the next slide are complete –If the child in tree ‘b’ on level 2 is a middle child then it is not complete

1-14 Some example trees

1-15 Implementing Trees with Links The most obvious implementation of tree is a linked structure Each node could be defined as a TreeNode class, as we did with the Node class for linked lists

1-16 Implementing Trees with Links Each node would contain a reference to the element to be stored in that node as well as pointers for each of the possible children of the node Depending on the implementation, it may also be useful to store a pointer in each node to its parent

1-17 Implementing Trees with Arrays For certain types of trees, specifically binary trees, a computational strategy can be used for storing a tree using an array For any element stored in position n of the array –leftChild (n) = 2*n + 1 –rightChild (n) = 2*n + 2 –parent (n) = (n – 1) / 2

1-18 Implementing Trees with Arrays Avoid dynamic allocation of nodes Use formula to get to parent and left and right children But can waste space if tree is not largely complete

1-19 Computational strategy for array implementation of trees

1-20 Implementing Trees with Arrays Array of nodes: Node[] array; Each element of the array will be a node class similar to the TreeNode class that we discussed earlier For left and right instead of references to Nodes we use array subscripts

1-21 Implementing Trees with Arrays This approach allows elements to be stored contiguously in the array so that space is not wasted Deleting a node is expensive –Shift (not good idea) –Maintain free list (good idea)

1-22 Simulated link strategy for array implementation of trees

1-23 Analysis of Trees Trees are a useful and efficient way to implement other collections In our analysis of a linked list implementation, we described the find operation as expected case n/2 or O(n) Using a binary search tree (BST) we can make find O(log n) BST Property: For each node v, key (v) > keys (v.left) and key (v) < keys (v.right) (No dups allowed)

1-24 Analysis of Trees Height of complete BST is lg n With the added ordering property of a binary search tree, you are guaranteed to at worst search one path from the root to a leaf

1-25 Tree Traversals There are four basic algorithms for traversing a tree: –Preorder traversal –Inorder traversal –Postorder traversal –Levelorder traversal

1-26 Preorder traversal Preorder traversal is accomplished by visiting each node, followed by its children, starting with the root Given the complete binary tree on the next slide, a preorder traversal would produce the order: A B D E C

1-27 A complete tree

1-28 Preorder traversal Stated in pseudocode, the algorithm for a preorder traversal of a binary tree is: Visit node Traverse(left child) Traverse(right child)

1-29 Inorder traversal Inorder traversal is accomplished by visiting the left child of the node, then the node, then any remaining child nodes starting with the root An inorder traversal of the previous tree produces the order: D B E A C

1-30 Inorder traversal Stated in pseudocode, the algorithm for an inorder traversal of a binary tree is: Traverse(left child) Visit node Traverse(right child)

1-31 Postorder traversal Postorder traversal is accomplished by visiting the children, then the node starting with the root Given the same tree, a postorder traversal produces the following order: D E B C A

1-32 Postorder traversal Stated in pseudocode, the algorithm for a postorder traversal of a binary tree is: Traverse(left child) Traverse(right child) Visit node

1-33 Level Order traversal Level order traversal is accomplished by visiting all of the nodes at each level, one level at at time, starting with the root Given the same tree, a level order traversal produces the order: A B C D E

1-34 Level Order traversal Stated in pseudocode, the algorithm for a level order traversal of a binary tree is:

1-35 Implementing Binary Trees As an example of possible implementations of trees, lets explore a simple implementation of a binary tree Having specified that we are implementing a binary tree, we can identify a set of possible operations that would be common for all binary trees Notice however, that other than the constructors, none of these operations add any elements to the tree It is not possible to define an operation to add an element to the tree until we know more about how the tree is to be used

1-36 The operations on a binary tree

1-37 UML description of the BinaryTreeADT interface

1-38 BinaryTreeADT /** * BinaryTreeADT defines the interface to a binary tree data structure. * Dr. Lewis Dr. Chase 1.0, 8/19/08 */ package jss2; import java.util.Iterator; public interface BinaryTreeADT { /** * Returns a reference to the root element * a reference to the root */ public T getRoot (); /** * BinaryTreeADT defines the interface to a binary tree data structure. * Dr. Lewis Dr. Chase 1.0, 8/19/08 */ package jss2; import java.util.Iterator; public interface BinaryTreeADT { /** * Returns a reference to the root element * a reference to the root */ public T getRoot ();

1-39 BinaryTreeADT (continued) /** * Returns true if this binary tree is empty and false otherwise. * true if this binary tree is empty */ public boolean isEmpty(); /** * Returns the number of elements in this binary tree. * the integer number of elements in this 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 true if this binary tree is empty and false otherwise. * true if this binary tree is empty */ public boolean isEmpty(); /** * Returns the number of elements in this binary tree. * the integer number of elements in this 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);

1-40 BinaryTreeADT (continued) /** * 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); /** * Returns the string representation of the binary tree. * a string representation of the binary tree */ public String toString(); /** * Performs an inorder traversal on this binary tree by calling an * overloaded, recursive inorder method that starts with the root. * an iterator over the elements of this binary tree */ public Iterator iteratorInOrder(); /** * 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); /** * Returns the string representation of the binary tree. * a string representation of the binary tree */ public String toString(); /** * Performs an inorder traversal on this binary tree by calling an * overloaded, recursive inorder method that starts with the root. * an iterator over the elements of this binary tree */ public Iterator iteratorInOrder();

1-41 BinaryTreeADT (continued) /** * Performs a preorder traversal on this binary tree by calling an * overloaded, recursive preorder method that starts with the root. * an iterator over the elements of this binary tree */ public Iterator iteratorPreOrder(); /** * Performs a postorder traversal on this binary tree by calling an * overloaded, recursive postorder method that starts with the root. * an iterator over the elements of this binary tree */ public Iterator iteratorPostOrder(); /** * Performs a levelorder traversal on the binary tree, using a queue. * an iterator over the elements of this binary tree */ public Iterator iteratorLevelOrder(); } /** * Performs a preorder traversal on this binary tree by calling an * overloaded, recursive preorder method that starts with the root. * an iterator over the elements of this binary tree */ public Iterator iteratorPreOrder(); /** * Performs a postorder traversal on this binary tree by calling an * overloaded, recursive postorder method that starts with the root. * an iterator over the elements of this binary tree */ public Iterator iteratorPostOrder(); /** * Performs a levelorder traversal on the binary tree, using a queue. * an iterator over the elements of this binary tree */ public Iterator iteratorLevelOrder(); }

1-42 Implementing Binary Trees with Links Lets examine a linked implementation of a binary tree Our implementation will need to keep track of the node that is the root of the tree as well as the count of elements in the tree protected int count; protected BinaryTreeNode root;

1-43 LinkedBinaryTree We will provide two constructors –One to create a null tree –One to create a tree containing a single element

1-44 The LinkedBinaryTree class /** * LinkedBinaryTree implements the BinaryTreeADT interface * Dr. Lewis Dr. Chase 1.0, 8/19/08 */ package jss2; import java.util.Iterator; import jss2.exceptions.*; public class LinkedBinaryTree implements BinaryTreeADT { protected int count; protected BinaryTreeNode root; /** * LinkedBinaryTree implements the BinaryTreeADT interface * Dr. Lewis Dr. Chase 1.0, 8/19/08 */ package jss2; import java.util.Iterator; import jss2.exceptions.*; public class LinkedBinaryTree implements BinaryTreeADT { protected int count; protected BinaryTreeNode root;

1-45 The LinkedBinaryTree class (cont.) /** * Creates an empty binary tree. */ public LinkedBinaryTree() { count = 0; root = null; } /** * Creates a binary tree with the specified element as its root. * element the element that will become the root of the new binary * tree */ public LinkedBinaryTree (T element) { count = 1; root = new BinaryTreeNode (element); } /** * Creates an empty binary tree. */ public LinkedBinaryTree() { count = 0; root = null; } /** * Creates a binary tree with the specified element as its root. * element the element that will become the root of the new binary * tree */ public LinkedBinaryTree (T element) { count = 1; root = new BinaryTreeNode (element); }

1-46 The BinaryTreeNode Class We will also need a class to represent each node in the tree Since this is a binary tree, we will create a BinaryTreeNode class that contain a reference to the element stored in the node as well as references for each of the children

1-47 The BinaryTreeNode class /** * BinaryTreeNode represents a node in a binary tree with a left and * right child. * Dr. Lewis Dr. Chase 1.0, 8/19/08 */ package jss2; public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; /** * BinaryTreeNode represents a node in a binary tree with a left and * right child. * Dr. Lewis Dr. Chase 1.0, 8/19/08 */ package jss2; public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right;

1-48 The BinaryTreeNode class (cont.) /** * Creates a new tree node with the specified data. * obj the element that will become a part of the new tree node */ BinaryTreeNode (T obj) { element = obj; left = null; right = null; } /** * Creates a new tree node with the specified data. * obj the element that will become a part of the new tree node */ BinaryTreeNode (T obj) { element = obj; left = null; right = null; }

1-49 The BinaryTreeNode class (cont.) /** * Returns the number of non-null children of this node. * This method may be able to be written more efficiently. * 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; } /** * Returns the number of non-null children of this node. * This method may be able to be written more efficiently. * 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; }

1-50 The LinkedBinaryTree Class Lets examine some of the methods of the LinkedBinaryTree Class Keep in mind that each node of a tree represents a sub-tree

1-51 The find and findagain Methods The find method provides an excellent example of the recursion that is possible given the nature of a tree We use the private method findAgain to support the public find method This allows us to distinguish between the original invocation of the method and the subsequent recursive calls

1-52 LinkedBinaryTree - Find /** * Returns a reference to the specified target element if it is * found in this binary tree. Throws a NoSuchElementException 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 an element not found exception occurs */ public T find(T targetElement) throws ElementNotFoundException { BinaryTreeNode current = findAgain( targetElement, root ); if( current == null ) throw new ElementNotFoundException("binary tree"); return (current.element); } /** * Returns a reference to the specified target element if it is * found in this binary tree. Throws a NoSuchElementException 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 an element not found exception occurs */ public T find(T targetElement) throws ElementNotFoundException { BinaryTreeNode current = findAgain( targetElement, root ); if( current == null ) throw new ElementNotFoundException("binary tree"); return (current.element); }

1-53 LinkedBinaryTree - findAgain /** * 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 findAgain(T targetElement, BinaryTreeNode next) { if (next == null) return null; if (next.element.equals(targetElement)) return next; BinaryTreeNode temp = findAgain(targetElement, next.left); if (temp == null) temp = findAgain(targetElement, next.right); return temp; } /** * 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 findAgain(T targetElement, BinaryTreeNode next) { if (next == null) return null; if (next.element.equals(targetElement)) return next; BinaryTreeNode temp = findAgain(targetElement, next.left); if (temp == null) temp = findAgain(targetElement, next.right); return temp; }

1-54 The iteratorInOrder Method Like the find method, the iteratorInOrder method uses a private method, inorder, to support recursion The traversals for a tree might be implemented as toString methods or iterators or both

1-55 LinkedBinaryTree - iteratorInOrder /** * 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 tempList.iterator(); } /** * 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 tempList.iterator(); }

1-56 LinkedBinaryTree - inorder /** * 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.left, tempList); tempList.addToRear(node.element); inorder (node.right, tempList); } /** * 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.left, tempList); tempList.addToRear(node.element); inorder (node.right, tempList); }

1-57 Implementing Binary Trees with Arrays Lets examine an array implementation of a binary tree Our implementation will need to keep track of the array containing the tree as well as the count of elements in the tree protected int count; protected T[ ] tree;

1-58 ArrayBinaryTree We will provide two constructors –One to create a null tree –One to create a tree containing a single element

1-59 The ArrayBinaryTree class /** * ArrayBinaryTree implements the BinaryTreeADT interface using an array * Dr. Lewis Dr. Chase 1.0, 8/19/08 */ package jss2; import java.util.Iterator; import jss2.exceptions.*; public class ArrayBinaryTree implements BinaryTreeADT { protected int count; protected T[] tree; private final int capacity = 50; /** * ArrayBinaryTree implements the BinaryTreeADT interface using an array * Dr. Lewis Dr. Chase 1.0, 8/19/08 */ package jss2; import java.util.Iterator; import jss2.exceptions.*; public class ArrayBinaryTree implements BinaryTreeADT { protected int count; protected T[] tree; private final int capacity = 50;

1-60 The ArrayBinaryTree class (cont.) /** * Creates an empty binary tree. */ public ArrayBinaryTree() { count = 0; tree = (T[]) new Object[capacity]; } /** * Creates a binary tree with the specified element as its root. * element the element which will become the root of the new tree */ public ArrayBinaryTree (T element) { count = 1; tree = (T[]) new Object[capacity]; tree[0] = element; } /** * Creates an empty binary tree. */ public ArrayBinaryTree() { count = 0; tree = (T[]) new Object[capacity]; } /** * Creates a binary tree with the specified element as its root. * element the element which will become the root of the new tree */ public ArrayBinaryTree (T element) { count = 1; tree = (T[]) new Object[capacity]; tree[0] = element; }

1-61 The ArrayBinaryTree Class Lets examine some of the methods of the ArrayBinaryTree Class Like our linked implementation, keep in mind that each node of a tree represents a sub-tree

1-62 ArrayBinaryTree - Find /** * Returns a reference to the specified target element if it is * found in this binary tree. Throws a NoSuchElementException if * the specified target element is not found in the binary tree. * targetElement the element being sought in the tree true if the element is in the tree ElementNotFoundException if an element not found exception occurs */ public T find (T targetElement) throws ElementNotFoundException { T temp=null; boolean found = false; for (int ct=0; ct<count && !found; ct++) if (targetElement.equals(tree[ct])) { found = true; temp = tree[ct]; } if (!found) throw new ElementNotFoundException("binary tree"); return temp; } /** * Returns a reference to the specified target element if it is * found in this binary tree. Throws a NoSuchElementException if * the specified target element is not found in the binary tree. * targetElement the element being sought in the tree true if the element is in the tree ElementNotFoundException if an element not found exception occurs */ public T find (T targetElement) throws ElementNotFoundException { T temp=null; boolean found = false; for (int ct=0; ct<count && !found; ct++) if (targetElement.equals(tree[ct])) { found = true; temp = tree[ct]; } if (!found) throw new ElementNotFoundException("binary tree"); return temp; }

1-63 The iteratorInOrder Method Like our linked example, the iteratorInOrder method uses a private method, inorder, to support recursion The traversals for a tree might be implemented as toString methods or iterators or both

1-64 ArrayBinaryTree - iteratorInOrder /** * Performs an inorder traversal on this binary tree by calling an * overloaded, recursive inorder method that starts with * the root. * an iterator over the binary tree */ public Iterator iteratorInOrder() { ArrayUnorderedList templist = new ArrayUnorderedList (); inorder (0, templist); return templist.iterator(); } /** * Performs an inorder traversal on this binary tree by calling an * overloaded, recursive inorder method that starts with * the root. * an iterator over the binary tree */ public Iterator iteratorInOrder() { ArrayUnorderedList templist = new ArrayUnorderedList (); inorder (0, templist); return templist.iterator(); }

1-65 ArrayBinaryTree - inorder /** * Performs a recursive inorder traversal. * node the node used in the traversal templist the temporary list used in the traversal */ protected void inorder (int node, ArrayUnorderedList templist) { if (node < tree.length) if (tree[node] != null) { inorder (node*2+1, templist); templist.addToRear(tree[node]); inorder ((node+1)*2, templist); } /** * Performs a recursive inorder traversal. * node the node used in the traversal templist the temporary list used in the traversal */ protected void inorder (int node, ArrayUnorderedList templist) { if (node < tree.length) if (tree[node] != null) { inorder (node*2+1, templist); templist.addToRear(tree[node]); inorder ((node+1)*2, templist); }