Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture 8 Trees sortTree Class public class sortTree { public Node root = null; public sortTree(double val) { root = new Node(val); } public Node add(Node node, double val) { if (node == null) return node = new Node(val); if (val < node.data) node.left = add(node.left, val); else node.right = add(node.right, val); return node; } protected class Node { public void showAll(Node node) double data; { Node left; if (node != null) Node right; { showAll(node.left); public Node(double val) System.out.println(node.data); { showAll(node.right); this(val,null,null); } } } public Node(double val, Node left_node, Node right_node) { data = val; left = left_node; right = right_node; } } protected class Node { double data; Node left; Node right; public Node(double val) { this(val,null,null); } public Node(double val, Node left_node, Node right_node) { data = val; left = left_node; right = right_node; } } data left null right null TreeSort add( ) Method 5, 3, 7, 2, 1, 8, 9, 0, 4, 6 5 3 2 7 4 6 1 0 8 9 public Node add(Node node, double val) { if (node == null) return node = new Node(val); if (val < node.data) node.left = add(node.left, val); else node.right = add(node.right, val); return node; } sortTreeDemo public class sortTreeDemo { public static void main(String[] args) { sortTree myTree = new sortTree(Math.random()*1000.0); for(int i=1;i<1000000;i++) myTree.add(myTree.root,Math.random()*1000.0); myTree.showAll(myTree.root); } } TreeSort for Strings public class TreeSort { public static void main(String []args) { String[] words = {"this", "is", "a", "test", "this", "is", "only", "a", "test"}; Tree tree= new Tree(); for(int i=0;i<words.length;i++) tree.insert(words[i]); tree.showNodes(); } } The Tree Class class Tree { Node root=null; public void showNodes() { if (root==null) return; else root.showNodes(); } public void insert(Comparable c) { if (root==null) root = new Node(c); else root.insert(c); } } The Node Class for TreeSort class Node { int count; Node left; Node right; Comparable key; public void insert(Comparable c) { if(c.compareTo(this.key)<0) { if (left == null) left=new Node(c); else left.insert(c); } public Node(Comparable c) { key=c; count=1; left=null; right=null; } public void showNodes() { if (left!=null) left.showNodes(); System.out.println(key + " if (right!=null) right.showNodes(); } if(c.compareTo(this.key)==0) this.count += 1; " + this.count); if(c.compareTo(this.key)>0) { if (right == null) right=new Node(c); else right.insert(c); } } } Redirecting Standard Output to a File an important side issue import java.io.*; public class outputRedirectDemo { public static void main(String[] args) throws FileNotFoundException { File file = new File("redirect.txt"); PrintStream printStream = new PrintStream(new FileOutputStream(file)); System.out.println("1"); System.setOut(printStream); System.out.println("2"); } } Using TreeSort in the Lexicon Demo Program import java.util.Scanner; import java.io.*; public class LexiconDemo2 { public static String[] words = new String[1000000]; public static int[] counts = new int[1000000]; public static int numwords = 0; public static void main(String[] args) throws FileNotFoundException { FileReader reader = new FileReader("warandpeace.txt"); Scanner in = new Scanner (reader); File file = new File("warandpeace.out"); PrintStream printStream = new PrintStream(new FileOutputStream(file)); System.setOut(printStream); Tree tree= new Tree(); while(in.hasNextLine()) { String line = in.nextLine(); String[] str = line.split("[.,:;?(){}!-= ]"); for(int i = 0;i<str.length;i++) if(str[i].length()>0) tree.insert(str[i].toLowerCase()); } tree.showNodes(); } } Run-Time Analysis What are the run-times (order of complexity) for LexiconDemo and LexiconDemo2? O(n2) O(n lg2n) Estimate the time to execute for n= 500,000 in seconds on our lab computers.