(Pre-order and post-order traversal). Implement the textbook's algorithm (not the Java implementation) for pre-order and post-order traversal. To "process" or "visit" a node means to print the data at that node.
Pre-order traversal output: 10 6 4 8 18 15 21
Post-order traversal output: 4 8 6 15 21 18 10
Additionally, create and traverse the following trees:
Answer:
Preorder implementation:
display(root->data) preorder(root->left) preorder(root->right)
Postorder implementation:
postorder(root->left) postorder(root->right) display(root->data)
Traversal of given trees:
Preorder traversal of first tree:
1 -> 2 -> 4 -> 5 -> 3 -> 6 -> 7
Postorder traversal of first tree:
4 -> 5 -> 2 -> 6 -> 7 -> 3 -> 1
Preorder traversal of second tree:
Root -> edu -> unc -> med -> cs -> com -> google -> maps -> yahoo
Postorder traversal of second tree:
med -> cs -> unc -> edu -> maps -> google -> yahoo -> com -> Root
Please give thumbsup, if you like it. Thanks.
(Pre-order and post-order traversal). Implement the textbook's algorithm (not the Java implementation) for pre-order and post-order...
2. Write a recursive algorithm which computes the number of nodes in a general tree. 3. Show a tree achieving the worst-case running time for algorithm depth. 4. Let T be a tree whose nodes store strings. Give an efficient algorithm that computes and prints, for every node v of T, the string stored at v and the height of the subtree rooted at v. Hin Consider 'decorating' the tree, and add a height field to each node (initialized to...
Could you please write the algorithms for In-order, Post-order and Level-order in this form as Pre-order is. Thank you. void preorder (BinNode rt) if (rt-null) return; // Empty subtree - do nothing visit(rt); preorder (rt.left()); // Process аїї nodes in ïeft preorder (rt.right // Process root node ()); // Process аїї nodes in right
In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT { private BinaryTreeNode root; /** * Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; } /** * Creates a binary tree from an existing root. */ public LinkedBinaryTree(BinaryTreeNode root) { this.root = root; } /** * Creates a binary tree with the specified element...
Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
TestCodeForAssigment.py def main(): # testing recursive find, pre- and post-order, and __len__ """ creating the following BST: 25 15 29 12 20 27 50 17 """ t1 = BST() t1.insert_rec(25) t1.insert_rec(15) t1.insert_rec(12) t1.insert_rec(20) t1.insert_rec(17) t1.insert_rec(29) t1.insert_rec(27) t1.insert_rec(50) print("Let's see if we can find 27 using the non-recursive find: ", t1.find(27)) print("Let's see if we can find 27 using the recursive find: ", t1.findRecursive(27)) print("The length of the BST tree is ",t1.__len__()) print("The preorder traversal of the tree:", list(t1.preOrder())) print("The postorder...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
I am stuck on a data structure problem, I am just going off of Geeks for Geeks for help but I need to print 100 random numbers in the range of [1-200]. I can currently only print 5 numbers. Here is my code: #include <stdio.h> #include <stdlib.h> #include <limits.h> using namespace std; //binary tree has data & left and right child struct node{ int data; struct node *left; struct node *right; }; //create a new node struct node* newNode (int...
write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...
using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...