Announcements: Assignment #2 is posted, due Wed. Oct. 7 (no extension). Lab 4 is posted. No office hours Friday Oct. 2. Let me know if you have any suggestions for the Lab content. Take the code you handed in to Lab #4 (even if it did not work properly), do not change to the model solution and take my code.
Lecture 10: Binary Tree Sort http://www.nevron.com/Gallery.DiagramFor.NET.Symmetrical.aspx
3 public class SplitList { LinkedList list1; LinkedList list2; public LinkedList joinList() LinkedList restored; list1.rear.next= list2.start; restored= new LinkedList(list1.n + list2.n, list1.start, list2.rear); return(restored); } Question #7: Assignment #1
4 public SplitList(int size1, LinkedList list) { ListNode current, rear; int i; current= list.start; for (i=1; i < size1; i++) { current= current.next; } rear= current; current= current.next; rear.next= null; list1= new LinkedList(size1, list.start, rear); list2= new LinkedList(list.n-size1, current, list.rear); } // A constructor for a SplitList Question #6: Assignment #1
5 public void printList(int nPerLine, int nDigit) { ListNode current; int count=0; current= start; while (current != null) { count++; format_int(0, current.data, nDigit); if (count % nPerLine == 0) System.out.println(); else System.out.print(" "); current= current.next; } if (count % nPerLine !=0) System.out.println(); } // This is in LinkedList class Question #5: Assignment #1
6 Main: Assignment #1 public static void main(String args[]) { int i; LinkedList list; SplitList split; Scanner in = new Scanner(System.in); list= new LinkedList(); while (list.readRear(in)) { System.out.println("The original list:"); list.printList(5, 3); System.out.println("Starting the splits:"); for (i=1; i < list.n; i++) { split= new SplitList(i, list); System.out.println("Split List for " + i + " List 1: "); split.list1.printList(5, 3); System.out.println("Split List for " + i + " List 2: "); split.list2.printList(5, 3); list= split.joinList(); System.out.println("After restoration:"); } Main: Assignment #1
BeginMax/MiddleMax/EndMax The original list: 0 9 1 8 2 7 3 6 4 5 Starting the splits: Split List for 1 List 1: Split List for 1 List 2: 9 1 8 2 7 3 6 4 5 After restoration: Split List for 2 List 1: 0 9 Split List for 2 List 2: 1 8 2 7 3 6 4 5 Split List for 3 List 1: 0 9 1 Split List for 3 List 2: 8 2 7 3 6 4 5 BeginMax/MiddleMax/EndMax Split List for 4 List 1: 0 9 1 8 Split List for 4 List 2: 2 7 3 6 4 5 After restoration: 0 9 1 8 2 7 3 6 4 5 Split List for 5 List 1: Split List for 5 List 2: Split List for 6 List 1: 7 Split List for 6 List 2: 3 6 4 5 Split List for 7 List 1: 0 9 1 8 2 7 3 Split List for 7 List 2: 6 4 5 After restoration: 7 3 6 4 5 Split List for 8 List 1: 7 3 6 Split List for 8 List 2: 4 5 Split List for 9 List 1: 7 3 6 4 Split List for 9 List 2: 5
Binary search tree: If a node has key value x then -all keys in the left subtree have a data value which is less than or equal to x, and - all keys in the right subtree have a data value which is greater than x.
If items are inserted one by one, the resulting tree can have arbitrary shape:
http://scienceblogs.com/goodmath/upload/2007/01/unbalanced-trees.jpg
Best Case Shape Worst Case Shape http://www.brpreiss.com/books/opus5/html/page304.html
Inductive Definition of a Binary Search tree: A binary search tree is built up of nodes each of which has a key value, a left child and a right child (where the children are pointers to other nodes). [Basis] A tree with 0 nodes is a binary search tree with root NULL. [Inductive step] If T1 and T2 are binary search trees with roots r1 and r2 respectively, and r is a node with key value k, and further, all nodes in T1 have key value ≤ k, and all nodes in T2 have key value > k, then setting the left child of r to point to r1 and the right child of r to point to r2 creates a new binary search tree T with root r. T1 and T2 are said to be respectively the left and right subtrees of the tree T rooted at r.
The pseudocode for a binary tree sort is: Start with an empty binary search tree T. 2. Add each key to T. 3. Traverse T using an in order traversal.
In Order Traversals A traversal of the nodes of a binary search tree which visits the keys in sorted order is called an in order traversal of the tree. Recursive code for an in order traversal: /* root- pointer to the root node of a binary search tree. */ Inorder(root) If (the left_child is not null) Inorder(root->left_child) 2. Visit the root node. 3. If (the right_child is not null) Inorder(root->right_child)
public class BinaryTreeNode { int data; BinaryTreeNode leftChild; BinaryTreeNode rightChild public void inOrder() if (leftChild!= null) leftChild.inOrder(); System.out.print(data + " "); if (rightChild!= null) rightChild.inOrder(); }
public void preOrder() { System.out.print(data + " "); if (leftChild!= null) leftChild.preOrder(); if (rightChild!= null) rightChild.preOrder(); } public void postOrder() if (leftChild!= null) leftChild.postOrder(); if (rightChild!= null) rightChild.postOrder();
Secret technique for doing traversals by hand:
Constructor for a BinaryTreeNode: public BinaryTreeNode(int value) { data= value; leftChild= null; rightChild= null; } Next, code for adding a node to a binary tree which has at least one node in it already.
public void addTree(int value) { if (value <= data) if (leftChild == null) leftChild= new BinaryTreeNode(value); else leftChild.addTree(value); } if (rightChild == null) rightChild= new BinaryTreeNode(value); rightChild.addTree(value);
How much time does the binary tree sort take to sort: in the best case? in the worst case? Give examples of input which can be provided for arbitrary n which realize the: best case behaviour? worst case behaviour? How much extra space does it use: