150 likes | 272 Views
More Trees. Java implementation of trees Tree traversal When should we use trees. Binary Tree Class in Java. public class BinTree // binary search tree { private String value; private BinTree left, right; // constructor BinTree() // create an empty tree node
E N D
More Trees Java implementation of trees Tree traversal When should we use trees
Binary Tree Class in Java • public class BinTree // binary search tree • { • private String value; • private BinTree left, right; • // constructor • BinTree() // create an empty tree node • { • value = null; • left = right = null; • } • BinTree(String x) • // create a leaf node with value x • { • value = x; • left = right = null; • } A binary tree node: value left right
Binary Tree Class in Java- insertion method public void insertInOrder(String input_item) { if(value==null) { // an empty tree value = input_item; } else if(Func.lessThan(input_item, value)) { // insert to left subtree if(left==null) left = new BinTree(input_item); else left.insertInOrder(input_item); } else { // insert to right subtree if(right==null) right = new BinTree(input_item); else right.insertInOrder(input_item); } } A binary tree node: Insert 7
Tree traversal – a systematic way of visiting all nodes of a binary tree printLR() {// print bin tree from left to right if(left != null) // print left subtree left.printLR(); System.out.print(value + “ “); // do print if(right != null) // print right subtree right.printLR(); }
Tree traversal – a systematic way of visiting all nodes of a binary tree printLR() { if(left != null) left.printLR(); // do print print(value+“ “); if(right != null) right.printLR(); } 2 5 5 7 9 10 12
List Class in Java • public class List • { • private int current_size; • private final int limit = 8192; • private String[] data = new String[limit];// array to hold list • private String[] tmp = new String[limit]; // tmp buffer, need it? • // constructor • List(int size, String type) • { int i, n; • if(size < 1) current_size = 0; // create an empty list • else current_size = size; • if(type.equals("random")) { • Random Ran = new Random(0); // use seed = 0 • for(i=0; i<current_size; i++) { // create a random list • n = (int) (Ran.nextFloat() * limit); • data[i] = Integer.toString(n); • } • } • else if(type.equals("ordered")) { • for(i=0; i<current_size; i++) { // create an ordered list • data[i] = Integer.toString(i); • } • } • // otherwise, a list of "null"s is constructed • } • // end of constructor
List Class in Java – ordered insertion • public int insertInOrder(String input_item) • { • if(current_size < limit) { • int i,j; • for(i=0;i < current_size;i++) { • if(Func.lessThan(input_item, data[i])) • break; // find the position • } • j = current_size; • while(j>i) { // shift every item to its right • data[j] = data[j-1]; • j--; • } • data[i] = input_item; // insert to the location i • current_size++; // increase size by 1 • return i; // return inserted position • } • else { • return -1; // overflow, failed to insert • } • }
Test Program – using random numbers • public class TreeInsertTestR { • static public void main(String args[]) { • Random Ran = new Random(100); • int size = Integer.parseInt(args[0]); • Stopwatch timer = new Stopwatch(); • BinTree tree = new BinTree(); // make an empty tree • timer.go(); • for(int i=0; i<size; i++) { • String S = Integer.toString((int)(Ran.nextFloat()*size)); • tree.insertInOrder(S); • } • long ms = timer.stop(); • tree.display_flat(); • System.out.println( • "constructed a binary search tree by random input. time spent " + • ms + " millisecond"); • } • }
Test Program – using ordered numbers • public class TreeInsertTestO { • static public void main(String args[]) { • // Random Ran = new Random(100); • int size = Integer.parseInt(args[0]); • Stopwatch timer = new Stopwatch(); • BinTree tree = new BinTree(); // make an empty tree • timer.go(); • for(int i=0; i<size; i++) { • String S = Integer.toString(i); • tree.insertInOrder(S); • } • long ms = timer.stop(); • tree.display_flat(); • System.out.println( • "constructed a binary search tree by ordered input. time spent " + • ms + " millisecond"); • } • }
My timing results – pentium4 1.6GHz time in millisecond
Balanced and unbalanced trees The best case The worst case height of tree = size of tree height of tree = log (size of tree)
The timing results show …… • List insertion costs O(n) → performing n insertions costs O(n2) • Binary tree insertion costs O(log n) (if it is balanced) → performing n insertions costs O(n log n) • But in the worst case, insertion in an unbalanced binary tree costs O(n)
Conclusion • Only use list structures if insertion/deletion are not frequently needed • Tree structures are very efficient for accessing and updating data • Important issue – how to make a balanced tree (ref. UQC108S2)
The Challenge Question • A list L contains n-1 unique integers in the range [0,n-1], that is, there is one number from this range is not in L. • For example, L = (6,3,1,5,7,0,2), n=8, and 4 is the missing number. • Design an O(n)-time algorithm for finding that missing number. You are only allowed to use O(1) additional memory space besides the list L itself.
If O(n) additional memory is allowed L = (6,3,1,5,7,0,2), It is easy to have an O(n) algorithm to find out the missing number. Two phases: 1 Read the list L, one by one, store the number X into an extra list, at location X, 2. Go through the extra list to find the missing number But, what if this extra list is NOT allowed, ……?