1170 Data Structures and Collections Chapter 23 23.6.1 (Web hosting servers)

1170 Data Structures and Collections Chapter 23 23.6.1 Binary Search Tree of Integer Values The application of Fig. 23.17 and Fig. 23.18 creates a binary search tree of integers and traverses it (i.e., walks through all its nodes) in three ways using recursive inorder, preorder and postorder traversals. The program generates 10 random numbers and inserts each into the tree. Figure 23.17 defines class Tree in namespace BinaryTreeLibrary for reuse purposes. Figure 23.18 defines class TreeTest to demonstrate class Tree s functionality. Method Mainof class TreeTestinstantiates an empty Treeobject, then randomly generates 10 integers and inserts each value in the binary tree by calling Treemethod InsertNode. The program then performs preorder, inorder and postorder traversals of the tree. We will discuss these traversals shortly. Class TreeNode (lines 9 95 of Fig. 23.17) is a self-referential class containing three private data members leftNode and rightNode, of type TreeNode, and data, of type int. Initially, every TreeNodeis a leaf node, so the constructor (lines 16 20) initializes references leftNodeand rightNodeto null. Properties LeftNode(lines 23 34), Data (lines 37 48) and RightNode (lines 51 62) provide access to a ListNode s privatedata members. We discuss TreeNodemethod Insert(lines 67 93) shortly. 1 // Fig. 23.17: BinaryTreeLibrary.cs 2 // Definition of class TreeNode and class Tree. 3 4 using System; 5 6 namespace BinaryTreeLibrary 7 { 8 // class TreeNode definition 9 class TreeNode 10 { 11 private TreeNode leftNode; 12 private int data; 13 private TreeNode rightNode; 14 15 // initialize data and make this a leaf node 16 public TreeNode( int nodeData ) 17 { 18 data = nodeData; 19 leftNode = rightNode = null; // node has no children 20 } 21 22 // LeftNode property 23 public TreeNode LeftNode 24 { 25 get 26 { 27 return leftNode; 28 } 29 Fig. 23.17 Fig. 23.1FiFig. 23.17g. 23.17 Fig. 23.17 Definitions of TreeNodeand Treefor a binary search tree. (Part 1 of 5.)
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

Leave a Reply