Recursive routines are used for size, makeEmpty,and in Java

Creation Code 39 Full ASCII in Java Recursive routines are used for size, makeEmpty,and
Recursive routines are used for size, makeEmpty,and
Make Code-39 In Java
Using Barcode generation for Java Control to generate, create Code-39 image in Java applications.
duplicate
Encode Barcode In Java
Using Barcode printer for Java Control to generate, create bar code image in Java applications.
Because duplicate is a BinaryNode method, we make recursive calls only after verifying that the subtrees are not
Bar Code Recognizer In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
NULL
Code-39 Creator In C#.NET
Using Barcode generator for .NET framework Control to generate, create Code 39 Full ASCII image in Visual Studio .NET applications.
As makeEmgty is not a BinaryNode member, the parameter t might be NULLWe thus test for it at the start of the routine and do not test prior to a recursive call
Generate ANSI/AIM Code 39 In .NET
Using Barcode printer for ASP.NET Control to generate, create Code-39 image in ASP.NET applications.
1 / / Return a pointer to a node that is the root of a 2 / / duplicate of the tree rooted at the current node 3 template <class Object> 4 BinaryNode<Object> * BinaryNode<Object>::duplicate( ) const
Code 39 Full ASCII Generator In .NET Framework
Using Barcode generation for .NET framework Control to generate, create ANSI/AIM Code 39 image in .NET framework applications.
5 I 6
USS Code 39 Encoder In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create Code-39 image in VS .NET applications.
7 8 9 10 11 12 13 14 i
European Article Number 13 Generator In Java
Using Barcode generator for Java Control to generate, create GTIN - 13 image in Java applications.
BinaryNode<Object> *root = new BinaryNode<Object>( element
UPC-A Supplement 2 Encoder In Java
Using Barcode creator for Java Control to generate, create UPC Code image in Java applications.
if( left ! = NULL ) / / If there's a left subtree root->left = left->duplicate( ) ; / / Duplicate; attach if ( right ! = NULL ) / / If there's a right subtree root->right = right->duplicate( ) ; / / Duplicate; attach return root; / / Return resulting tree
Draw Bar Code In Java
Using Barcode generator for Java Control to generate, create bar code image in Java applications.
Figure 1817 A routine for returning a copy of the tree rooted at the current node
ECC200 Generator In Java
Using Barcode generation for Java Control to generate, create Data Matrix image in Java applications.
1 2 3 4 5
Create Code39 In Java
Using Barcode drawer for Java Control to generate, create Code 39 Full ASCII image in Java applications.
/ / Make tree rooted at t empty, freeing nodes, / / and setting t to NULL
Planet Creation In Java
Using Barcode creator for Java Control to generate, create USPS Confirm Service Barcode image in Java applications.
template <class Object> void BinaryTree<Object>::makeEmpty(BinaryNode<Object> *
Code 128 Reader In VS .NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
if ( t ! = NULL
Code39 Generator In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create Code 39 image in .NET framework applications.
7 8 9 10 11 12 13 1
Make Code 39 Extended In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create Code 3 of 9 image in ASP.NET applications.
makeEmpty ( t->left ) makeEmpty( t->right delete t; t = NULL;
Bar Code Drawer In VS .NET
Using Barcode creation for Visual Studio .NET Control to generate, create barcode image in VS .NET applications.
Figure 1818 A routine to delete all nodes in a tree rooted at t
Bar Code Reader In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
Figure 1819 Recursive view used to calculate the size of a tree: ST= S,
DataMatrix Drawer In C#
Using Barcode creation for .NET framework Control to generate, create ECC200 image in .NET framework applications.
+ S + 1 ,
Encode Code 128 Code Set C In VS .NET
Using Barcode drawer for ASP.NET Control to generate, create Code 128 Code Set C image in ASP.NET applications.
The size routine is easily implemented recursively after a drawing is made
The height routine is also easily implemented recursivelyThe height of an empty tree is -1
The next method we write is the size routine in the BinaryNode class It returns the size of the tree rooted at a node pointed at by t,which is passed as a parameter If we draw the tree recursively, as shown in Figure 1819, we see that the size of a tree is the size of the left subtree plus the size of the right subtree plus 1 (because the root counts as a node) A recursive routine requires a base case that can be solved without recursion The smallest tree that size might have to handle is the empty tree (if t is NULL), and the size of an empty tree is clearly 0 We should verify that the recursion produces the correct answer for a tree of size 1 Doing so is easy, and the recursive routine is implemented as shown in Figure 1820 The final recursive routine presented in this section calculates the height of a node Implementing this routine is difficult to do nonrecursively but is trivial recursively, once we have made a drawing Figure 1821 shows a tree viewed recursively Suppose that the left subtree has height H , and the right subtree has height HR Any node that is d levels deep with respect to the root
Recursion and ~
1 / / Return size of tree rooted at t 2 template <class Object> 3 int BinaryNode<Object>::size( BinaryNode<Object> * t )
if( t == NULL ) return 0; else return 1 + size( t->left
+ size( t->right 1 ;
Figure 1820 A routine for computing the size of a node
Figure 1821 Recursive view of the node height calculation: H,= Max (H,+ 1, HR + 1)
1 / / Return height of tree rooted at t 2 template <class Object> 3 int BinaryNode<Object>::height( BinaryNode<Object> * t
if( t = = NULL ) return -1 ; else return 1 + max( height( t->left ),height( t->right
Figure 1822 A routine for computing the height of a node
of the left subtree is d + 1 levels deep with respect to the root of the entire tree The same holds for the right subtree Thus the path length of the deepest node in the original tree is I more than its path length with respect to the root of its subtree If we compute this value for both subtrees, the maximum of these two values plus 1 is the answer we want The code for doing so is shown in Figure 1822
184 Tree Traversal: lterator Classes
In this chapter we have shown how recursion can be used to implement the binary tree methods When recursion is applied, we compute information about not only a node but also about all its descendants We say then that we are traversing the tree Two popular traversals that we have already mentioned are the preorder and postorder traversals In a preorder traversal, the node is processed and then its children are processed recursively The duplicate routine is an example of a preorder traversal because the root is created first Then a left subtree is copied recursively, followed by copying the right subtree In a postorder traversal, the node is processed after both children are processed recursively Three examples are the methods size,height,and makeEmptyIn every case, information about a node (eg, its size or height) can be obtained only after the corresponding information is known for its children In makeEmpty,a node is deleted only after its children are recursively deleted A third common recursive traversal is the inorder traversal, in which the left child is recursively processed, the current node is processed, and the right child is recursively processed This mechanism is used to generate an algebraic expression corresponding to an expression tree For example, in c Figure 1 810 the inorder traversal yields ( a+ ( (b- ) * d)) Figure 1823 illustrates routines that print the nodes in a binary tree using each of the three recursive tree traversal algorithms Figure 1824 shows the order in which nodes are visited for each of the three strategies The running time of each algorithm is linear In every case, each node is output only once Consequently, the total cost of an output statement over any traversal is O(N) As a result, each if statement is also executed at most once per node, for a total cost of O(N) The total number of method calls made (which involves the constant work of the internal run-time stack pushes and pops) is likewise once per node, or O(N) Thus the total running time is O(N) Must we use recursion to implement the traversals The answer is clearly no because, as discussed in Section 83, recursion is implemented by using a might expect that a somewhat stack Thus we could keep our own stack"e faster program could result because we can place only the essentials on the stack rather than have the compiler place an entire activation record on the stack The difference in speed between a recursive and nonrecursive algorithm
3 We can also add parent pointers to each tree node to avoid both recursion and stacks In this chapter we demonstrate the relation between recursion and stacks, so we do not use parent pointers
In an inorder traversal, the current node is processed between recursive calls Simple traversal using any of these strategies takes linear time