Chapter 23 Data Structures and Collections (Web hosting domains) 1175 32
Chapter 23 Data Structures and Collections 1175 32 tree.PreorderTraversal(); 33 34 // perform inorder traversal of tree 35 Console.WriteLine( “nnInorder traversal” ); 36 tree.InorderTraversal(); 37 38 // perform postorder traversal of tree 39 Console.WriteLine( “nnPostorder traversal” ); 40 tree.PostorderTraversal(); 41 Console.WriteLine(); 42 } 43 44 } // end class TreeTest 45 } Inserting values: 39 69 94 47 50 72 55 41 97 73 Preorder traversal 39 69 47 41 50 55 94 72 73 97 Inorder traversal 39 41 47 50 55 69 72 73 94 97 Postorder traversal 41 55 50 47 73 72 97 94 69 39 Fig. 23.18 Fig. 23.1FiFig. 23.18g. 23.18 Fig. 23.18 Creating and traversing a binary tree. (Part 2 of 2.) Class Tree (lines 98 198 of Fig. 23.17) manipulates objects of class TreeNode. Class Tree has as private data root (line 100) a reference to the root node of the tree. The class contains public method InsertNode (lines 111 121) to insert a new node in the tree and publicmethods PreorderTraversal (lines 124 130), InorderTraversal (lines 149 155) and PostorderTraversal (lines 174 180) to begin traversals of the tree. Each of these methods calls a separate recursive utility method to perform the traversal operations on the internal representation of the tree. The Treeconstructor (lines 103 106) initializes rootto nullto indicate that the tree initially is empty. The Tree class s method InsertNode (lines 67 74) first locks the Tree object for thread safety, then determines whether the tree is empty. If so, line 116 allocates a new TreeNode, initializes the node with the integer being inserted in the tree and assigns the new node to root. If the tree is not empty, InsertNodecalls TreeNode method Insert (lines 67 93), which recursively determines the location for the new node in the tree and inserts the node at that location. A node can be inserted only as a leaf node in a binary search tree. The TreeNode method Insert compares the value to insert with the data value in the root node. If the insert value is less than the root-node data, the program determines whether the left subtree is empty (line 73). If so, line 74 allocates a new TreeNode, initializes it with the integer being inserted and assigns the new node to reference leftNode. Otherwise, line 78 recursively calls Insertfor the left subtree to insert the value into the
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services