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.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
1 Theory I Algorithm Design and Analysis (2 - Trees: traversal and analysis of standard search trees) Prof. Th. Ottmann.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Data Structures Using C++1 Chapter 11 Binary Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Data Structures Using C++ 2E Chapter 11 Binary Trees.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Lab 4 Due date: March 29. Linked Representation Each binary tree node is represented as an object whose data type is binaryTreeNode. The space required.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Binary Search Trees (BST) Let’s look at some pics …and some code.
(c) University of Washington20-1 CSC 143 Java Trees.
Binary Trees.
Trees Chapter 15.
Data Structure and Algorithms
Binary Trees.
Trees Chapter 11 (continued)
Binary Trees and Binary Search Trees
Trees Chapter 11 (continued)
Binary Search Trees Chapter 7 Objectives
Binary Tree.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Trees Another Abstract Data Type (ADT)
Section 8.1 Trees.
ITEC 2620M Introduction to Data Structures
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Trees 7/14/2009.
Binary Tree Traversal Methods
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
Trees Another Abstract Data Type (ADT)
Binary Tree Traversal Methods
Trees Another Abstract Data Type (ADT)
Lecture 12 CS203 1.
Binary Tree Traversal Methods
Traverse this binary search tree using:
Binary Trees.
CSC 143 Java Trees.
BINARY TREE CSC248 – Data Structure.
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
Tree traversals BST properties Search Insertion
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

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: