Question

In: Computer Science

Using Jeliot, execute the following tree algorithm:      import Prog1Tools.IOTools;           class Node {          ...

Using Jeliot, execute the following tree algorithm:

     import Prog1Tools.IOTools;
    
     class Node {
          Node left;
          Node right;
          int value;

public Node(int value) {
              this.value = value;
          }
     }

     public class GeneralTreeTest {
       public static void main(String[] args) {

// build a simple tree add 5 nodes to the tree
          Node root = new Node(5);
          System.out.println("Tree Example");
          System.out.println("Building tree with root value " + root.value);
          insert(root, 1);
          insert(root, 8);
          insert(root, 6);
          insert(root, 3);
          insert(root, 9);
          System.out.println("Traversing tree ");
          printOrder(root);

}

     public static void insert(Node node, int value) {
          if (value < node.value) {
            if (node.left != null) {
              insert(node.left, value);
            } else {
              System.out.println(" Inserted " + value + " to left of "
                  + node.value);
              node.left = new Node(value);
            }
         } else if (value > node.value) {
            if (node.right != null) {
              insert(node.right, value);
            } else {

System.out.println(" Inserted " + value + " to right of "
                      + node.value);
                 node.right = new Node(value);
             }
          }
     }

     public static void printOrder(Node node) {
        if (node != null) {
           printOrder(node.left);
           System.out.println(" Traversed " + node.value);
           printOrder(node.right);
        }
     }
}

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.

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.

If you have arrived at a different answer or analysis than your peers, discuss your findings with your peers and attempt to determine whose analysis is most accurate.

Solutions

Expert Solution

Tree structure that is being created using the algorithm is binary search tree.

We can say that it is creating a binary search tree, because when the node value is less than current node then we move to left of tree and if the node value is greater than current node value we move to the right of the tree.

This is the algorithm that is used to insert node in a binary search tree.

Below image is the binary search tree that is created in the code above

Order of traversal:-

Code is conducting an in order traversal on binary search tree.

We can say that because we are following (left, root, right) traversal order, that means traverse left, print root and then traverse right.

And also from console output we can conclude that it is in order traversal, because in order traversal of a binary search tree is always in sorted order.
Here is the console output for traversal.

Traversing tree
Traversed 1
Traversed 3
Traversed 5
Traversed 6
Traversed 8
Traversed 9

Complexity analysis:-

Insert function time complexity:-

As we know that binary search tree is not a balanced tree, that means that the height of the tree can grow upto O(n) for n nodes. So worst case complexity to insert a node will be when the height of tree is n and the node we are inserting is inserted at the end of the tree. That will take O(n) time.

So worst case complexity for insert is O(n)

printOrder function will traverse every node exactly once. So for a binary search tree of n nodes, it will take theta(n) time.


Related Solutions

Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode* find(int i) { // implement } If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right;...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
Using Jeliot, execute the following algorithm which implements a buffer pool algorithm. The algorithm offers options...
Using Jeliot, execute the following algorithm which implements a buffer pool algorithm. The algorithm offers options for three different heuristics including LRU, LFU, and FIFO.
I have code for an AVL tree. I Have have a node class and tree class....
I have code for an AVL tree. I Have have a node class and tree class. I need a  node object that refrences the root(top) of the tree. my current code throws NullPointerException when I try to access the root properties. here is the important snippets of my code, it is in java. public class Node { private Node left=null,right=null; public int height=1; private String Item; private int balance=0; Node(String item) { this.Item=item; } } -------------------------------------------------- public class AVLTree { public...
Create a (partial) BST class and a driver program to test it. The tree node will...
Create a (partial) BST class and a driver program to test it. The tree node will store integers as the data/key field (single field). Note that you will need to guarantee there are no duplicates in your insert function (the tree should refuse to insert a duplicate key). Call your files “tree.h”, “tree.cpp” and “main.cpp”. In addition, draw a picture of your tree (see note about random values below) Public methods to include: Constructor Copy Constructor Overloaded Assignment Operator Destructor...
. Let BT Node be the class we often use for binary-tree nodes. Write the following...
. Let BT Node be the class we often use for binary-tree nodes. Write the following recursive methods: (a) numLeaves: a method that takes a BT Node T as parameter and returns the number of leaves in the tree rooted at T. (b) isEven: a boolean method that takes a BT Node T and checks whether its tree is strictly binary: every node in the tree has an even number of children.
Implement a function to remove a leaf node from a binary search tree. Using the following...
Implement a function to remove a leaf node from a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode * remove_leaf(int item, bool &removed) { // implement } The remove function will return the node with the item if it's present in the tree. If the node is a leaf node and is removed by the function,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT