import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; class Node { Node left, right; int ele; int colour; public Node (int element) { this( element, null, null ); } public Node (int theElement, Node lt, Node rt) { left = lt; right = rt; ele = theElement; colour = 1; } } class RBTree { private Node curr; private Node parent; private Node gra; private Node gre; private Node header; private static Node nullNode; static { nullNode = new Node(0); nullNode.left = nullNode; nullNode.right = nullNode; } static final int BLACK = 1; // Black - 1 RED - 0 static final int RED = 0; public RBTree(int negInt) { header = new Node(negInt); header.left = nullNode; header.right = nullNode; } public boolean isEmpty() { return header.right == nullNode; } public void insert(int item ) // Function to insert item { curr = parent = gra = header; nullNode.ele = item; while (curr.ele != item) { gre = gra; gra = parent; parent = curr; curr = item < curr.ele ? curr.left : curr.right; //conditional statement if (curr.left.colour == RED && curr.right.colour == RED) handleReorient( item ); } if (curr != nullNode) //insertion check return; curr = new Node(item, nullNode, nullNode); if (item < parent.ele) parent.left = curr; else parent.right = curr; handleReorient( item ); } private void handleReorient(int item) { curr.colour = RED; curr.left.colour = BLACK; curr.right.colour = BLACK; if (parent.colour == RED) { gra.colour = RED; if (item < gra.ele != item < parent.ele) parent = rotate( item, gra ); curr = rotate(item, gre ); curr.colour = BLACK; } header.right.colour = BLACK; } private Node rotate(int item, Node p) { if(item < p.ele) return p.left = item < p.left.ele ? rotateWithLeftChild(p.left) : rotateWithRightChild(p.left) ; else return p.right = item < p.right.ele ? rotateWithLeftChild(p.right) : rotateWithRightChild(p.right); } private Node rotateWithLeftChild(Node k3) // Rotate binary tree node with left child { Node k1 = k3.left; k3.left = k1.right; k1.right = k3; return k1; } private Node rotateWithRightChild(Node k3) // Rotate binary tree node with right child { Node k2 = k3.right; k3.right = k2.left; k2.left = k3; return k2; } public void inorder() //traverse { inorder(header.right); } private void inorder(Node r) //traverse { if (r != nullNode) { inorder(r.left); char c = 'b'; if (r.colour == 0) c = 'r'; System.out.print(r.ele +""+c+" "); inorder(r.right); } } public void preorder() //traverse { preorder(header.right); } private void preorder(Node r) //traverse { if (r != nullNode) { char c = 'b'; if (r.colour == 0) c = 'r'; System.out.print(r.ele +""+c+" "); preorder(r.left); preorder(r.right); } } public void postorder() //traverse { postorder(header.right); } private void postorder(Node nod) //traverse { if (nod != nullNode) { postorder(nod.left); postorder(nod.right); char ch = 'B'; if (nod.colour == 0) ch = 'R'; System.out.print(nod.ele +""+ch+" "); } } } public class MyMain { public static void main(String[] args) throws FileNotFoundException { Scanner scan = new Scanner(System.in); RBTree rbt = new RBTree(Integer.MIN_VALUE); Scanner scanner = new Scanner(new File("test.txt")); int [] arr = new int [100]; int i = 0; while(scanner.hasNextInt()) { arr[i++] = scanner.nextInt(); } for (i=0; i<arr.length; i++) { rbt.insert(arr[i]); } System.out.print("\nPost order : "); rbt.postorder(); System.out.print("\nPre order : "); rbt.preorder(); System.out.print("\nIn order : "); rbt.inorder(); char ch; //use this block of code for user input /* do { System.out.println("\nEnter Your Choice:\n"); System.out.println("1. Insert "); System.out.println("2. Display "); int choice = scan.nextInt(); switch (choice) { case 1 : System.out.println("Enter integer element to insert"); rbt.insert( scan.nextInt() ); break; case 2 : System.out.print("\nPost order : "+rbt.postorder()+"\nPre order : "+rbt.preorder()+"\nIn order : "+rbt.inorder()); break; default : System.out.println("Wrong Entry \n "); break; } System.out.println("\nDo you want to continue (Type y or n) \n"); ch = scan.next().charAt(0); } while (ch == 'Y'|| ch == 'y'); */ } }
Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from...
Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....
Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....
Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....
Part A Analyze the following recurrences and show their time complexity functions using (I) iteration method and (2) Master Theorem. AI. T(n) = 2T 3 A2. T(n) = 3T 2n АЗ. Т(п) — Т(п — 2) + 3 А4. Т(п) — 2Т (п — 1) + 1 A5. T(n)= 4T +n log n A6. T(n) = 3T +n log n n2 A7. T(n) = 27 Part B Do 2.3-4 (р39) and Problem 2-1 (р39) Part C Implement MERGE-SORT() algorithm that...
Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....
C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....
Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...
Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a = [ [93, 80, 99, 72, 86, 84, 85, 41, 69, 31], [15, 37, 58, 59, 98, 40, 63, 84, 87, 15], [48, 50, 43, 68, 69, 43, 46, 83, 11, 50], [52, 49, 87, 77, 39, 21, 84, 13, 27, 82], [64, 49, 12, 42, 24, 54, 43, 69, 62, 44], [54, 90, 67, 43, 72, 17, 22, 83, 28, 68], [18, 12, 10,...
PYTHON 3 node Node ADT Question. Question 3 (18 points): Purpose: To practice working with node chains created using the Node ADT to implement slightly harder functionality Degree of Difficulty: Moderate to Tricky In this question you'll implement merge sort for node chains! Recall, merge sort is a divide-and-conquer technique that uses recursion to sort a given sequence (in this case a node chain). A overview of the algorithm is given below. Algorithm mergeSort (NC) Borts node chain NC using...
Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...