LAB 11 Data Structure Nadine ElHaddad S22107736 class Node int data Node left right public Node int item data item left right null class BinaryTree Node root public BinaryTree root null void insert int data root insertRec root data Node insertRec Node root int data if root null root new Node data return root if data root data root left insertRec root left data else if data root data root right insertRec root right data return root void inOrderTraversal Node node if node null inOrderTraversal node left System out print node data inOrderTraversal node right void preOrderTraversal Node node if node null System out print node data preOrderTraversal node left preOrderTraversal node right void postOrderTraversal Node node if node null postOrderTraversal node left postOrderTraversal node right System out print node data int sumNodes Node node if node null return 0 else return node data sumNodes node left sumNodes node right static int preIndex 0 Node constructTree int inorder int preorder int start int end if start end return null Node node new Node preorder preIndex if start end return node int inIndex search inorder start end node data node left constructTree inorder preorder start inIndex 1 node right constructTree inorder preorder inIndex 1 end return node int search int arr int start int end int value int i for i start i end i if arr i value return i return i public class BinaryTreeTraversal public static void main String args BinaryTree tree new BinaryTree int inorder 4 2 5 1 6 3 int preorder 1 2 4 5 3 6 tree root tree constructTree inorder preorder 0 inorder length 1 System out println InOrder Traversal tree inOrderTraversal tree root System out println nPreOrder Traversal tree preOrderTraversal tree root System out println nPostOrder Traversal tree postOrderTraversal tree root int sum tree sumNodes tree root System out println nSum of all integer values stored in the nodes sum
View Full Document