• Home
  • University of the People
  • Data Structures (proctored course) CS 3303
  • Understanding Binary Trees and Traversals

Understanding Binary Trees and Traversals

This algorithm first inserts five nodes into a tree structure and then traverses the tree. Using the Jeliot environment, load, compile and execute this java algorithm to understand its operation. Determine the kind of tree structure that is being created and determine what kind of traversal the algorithm is conducting. The algorithm starts by creating a tree root of 5 and inserts 5 nodes into a tree structure Node left Node le Nodeleft Node left] Node right Node right int value nt Yalue int value int value int value We can see from the picture that every node has at most 2 children, left and right. If nodes of the tree have 2 children, it means it is a binary tree. We also can define this from the structure of the node class. class Node Node left; Node right; int value; public Node(int value) this.value = value; We can see from the class Node that each node has left and right nodes. If it is a binary tree then it definitely not a sequential tree. But is this k-ary tree, where k is 2. Let's first picture the tree. This is our tree, and we can see the node 1 has just one child, only the right child, and the left is empty. From this, we can say this is not a k-ary tree or a 2-ary tree. In a 2-ary tree, internal node must have 2 children. In our case, it is just a Binary tree. Output: Tree Example Building tree with root value 5 Inserted 1 to left of 5 Inserted 8 to right of 5 Inserted 6 to left of 8 Inserted 3 to right of 1 Inserted 9 to right of 8 Traversing tree public static void printOrder(Node node) { ifnode!-null{ printOrder(node.left); Traversed+node.value; printorder(node.right); 51 1315 The traversal method is inorder traversal. In inorder traversal, it traverses from left to parent to right. In our case [1, 3, 5, 6, 8, 9]. Finally, conduct an Asymptotic analysis for the provided algorithm and report your analysis including Big O, Big Omega, or Big Theta as appropriate. Post your findings to the discussion forum and review and respond to the postings of your peers. In inorder, preorder, and postorder traversals big omega is O(n+m), where n is the number of nodes in the tree and m is the edges. In a binary tree, the maximum edges are n-1 O(n+n-1) => O(2n - 1) So we can say Big omega is O(n) The worst case is O(n) The best case is also BigOmega(n) If the best case and worst case are the same, it means an average case is also the same, so BigTheta(n) Reference: Shaffer, C. (2011). A Practical Introduction to Data Structures and Algorithm Analysis Blacksburg: Virginia. Tech. https://people.cs.vt.edu/shaffer/Book/JAVA3e20130328.pd