IS 2610: Data Structures Discuss HW 2 problems Binary Tree (continued) Introduction to Sorting Feb 9, 2004.

Slides:



Advertisements
Similar presentations
IS 2610: Data Structures Sorting Feb 16, Sorting Algorithms: Bubble sort Bubble sort  Move through the elements exchanging adjacent pairs if the.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Sorting Suppose you wanted to write a computer game like Doom 4: The Caverns of Calvin… How do you render those nice (lurid) pictures of Calvin College.
CHAPTER 11 Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Data Structures & Algorithms Recursion and Trees.
Data Structures & Algorithms Sorting. Recall Selection Sort Insertion Sort Merge Sort Now consider Bubble Sort Shell Sort Quick Sort Sorting.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
F453 Computing Searches. Binary Trees Not this kind of tree!
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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 ),
Dale Roberts Sorting Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
3 – SIMPLE SORTING ALGORITHMS
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
SORTING & SEARCHING - Bubble SortBubble Sort - Insertion SortInsertion Sort - Quick SortQuick Sort - Binary SearchBinary Search 2 nd June 2005 Thursday.
IS 2610: Data Structures Recursion, Divide and conquer Dynamic programming, Feb 2, 2004.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Discrete Mathematics Chapter 5 Trees.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Binary Search Trees (BST)
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.
1 Chapter 8 Sorting. 2 OBJECTIVE Introduces: Sorting Concept Sorting Types Sorting Implementation Techniques.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
Example Algorithms CSE 2320 – Algorithms and Data Structures Alexandra Stefan University of Texas at Arlington Last updated 1/31/
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Trees Chapter 15.
Recursive Objects (Part 4)
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
UNIT III TREES.
Week 6 - Wednesday CS221.
Trees.
Tree.
Section 8.1 Trees.
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CS223 Advanced Data Structures and Algorithms
MergeSort Source: Gibbs & Tamassia.
Department of Computer and Information Science, School of Science, IUPUI Quicksort Dale Roberts, Lecturer Computer Science, IUPUI
CO 303 Algorithm Analysis And Design Quicksort
Trees (part 2) CSE 2320 – Algorithms and Data Structures
Design and Analysis of Algorithms
Binary Trees, Binary Search Trees
Topic 6: Binary Search Tree Data structure Operations
Data Structures & Algorithms
Tree traversals BST properties Search Insertion
Binary Trees, Binary Search Trees
Presentation transcript:

IS 2610: Data Structures Discuss HW 2 problems Binary Tree (continued) Introduction to Sorting Feb 9, 2004

Binary tree A binary tree with N internal nodes has 2N links  N-1 to internal nodes Each internal node except root has a unique parent Every edge connects to its parent  N+1 to external nodes Level, height, path  Level of a node is 1 + level of parent (Root is at level 0)  Height is the maximum of the levels of the tree’s nodes  Path length is the sum of the levels of all the tree’s nodes  Internal path length is the sum of the levels of all the internal nodes

Examples Level of D ? Height of tree? Internal length? External length? Height of tree? Internal length? External length? A B E CD F

Binary Tree External path length of any binary tree with N internodes is 2N greater than the internal path length The height of a binary tree with N internal nodes is at least lg N and at most N-1  Worst case is a degenerate tree: N-1  Best case: balanced tree with 2 i nodes at level i. Hence for height: 2 h-1 < N+1 ≤ 2 h – hence h is the height

Binary Tree Internal path length of a binary tree with N internal nodes is at least N lg (N/4) and at most N(N-1)/2  Worst case : N(N-1)/2  Best case: (N+1) external nodes at height no more than  lg N  (N+1)  lg N  - 2N < N lg (N/4)

Tree traversal (binary tree) Preorder  Visit a node,  Visit left subtree,  Visit right subtree Inorder  Visit left subtree,  Visit a node,  Visit right subtree Postorder  Visit left subtree,  Visit right subtree  Visit a node A B E CDFI GH

Recursive/Nonrecursive Preorder void traverse(link h, void (*visit)(link)) { If (h == NULL) return; (*visit)(h); traverse(h->l, visit); traverse(h->r, visit); } void traverse(link h, void (*visit)(link)) { STACKinit(max); STACKpush(h); while (!STACKempty()) { (*visit)(h = STACKpop()); if (h->r != NULL) STACKpush(h->r); if (h->l != NULL) STACKpush(h->l); }

Recursive binary tree algorithms Exercise on recursive algorithms:  Counting nodes  Finding height

Sorting Algorithms: Selection sort Basic idea  Find smallest element and put in the first place  Find next smallest and put in second place .. Try for #define key(A) (A) #define less(A, B) (key(A) < key(B)) #define exch(A, B) { Item t = A; A = B; B = t; } void selection (Item a[], int l, int r) { int i, j; for (i = l; i < r; i++) { int min = i; for (i = i+1; i <= r; j++) { if (less(a[j], a[min]) min = j; exch(a[i], a[min]); }

Sorting Algorithms: Selection sort Recursive?  Eliminate the outer loop  Find the minimum and place it in the first place  Make recursive call to sort the remaining parts of the array Complexity  i goes from 1 to N- 1  There is one exchange per iteration; total N -1  There is N-i comparisons per iteration (N-1) + (N+2) = N(N-1)/2

Sorting Algorithms: Bubble sort Bubble sort  Move through the elements exchanging adjacent pairs if the first one is larger than the second  Try out ! #define key(A) (A) #define less(A, B) (key(A) < key(B)) #define exch(A, B) { Item t = A; A = B; B = t; } void selection (Item a[], int l, int r) { int i, j; for (i = l; i < r; i++) for (j = r; j > r; j--) if (less(a[j-1], a[j]) exch(a[j-1], a[j]); }

Sorting Algorithms: Bubble sort Recursive? Complexity  i th pass through the loop – (N-i) compare-and- exchange  Hence N(N-1)/2 compare-and-exchange is the worst case  What would be the minimum number of exchanges?

Sorting Algorithms: Insertion sort Insertion sort  “People” method Complexity  About N 2 /4  About half of the left array #define less(A, B) (key(A) < key(B)) #define exch(A, B) { Item t = A; A = B; B = t; } #define compexch(A, B) if (less(B, A)) exch(A, B) void insertion(Item a[], int l, int r) { int i; for (i = r; i > l ; i--) compexch(a[i-1], a[i]); for (i = l+2; i <= r; i++) { int j = i; Item v = a[i]; while (less(v, a[j-1])) { a[j] = a[j-1]; j--; } a[j] = v; }

Sorting Algorithms: Shell sort Extend of insertion sort  Taking every h th element will  Issues Increment sequence? h = 13 ASORTINGEXAMPLE AEORTINGEXAMPLS void shellsort(Item a[], int l, int r) { int i, j, h; for (h = 1; h <= (r-l); h = 3*h+1) ; for ( ; h > 0;) h = h/3; for (i = l+h; i <= r; i++) { int j = i; Item v = a[i]; while (j >= l+h && less(v, a[j-h])) { a[j] = a[j-h]; j -= h; } a[j] = v; }

Sorting Algorithms: Shell sort h = 4 AEORTINGEXAMPLS AENRTIOGEXAMPLS AENGTIOREXAMPLS … void shellsort(Item a[], int l, int r) { int i, j, h; for (h = 1; h <= (r-l); h = 3*h+1) ; for ( ; h > 0;) h = h/3; for (i = l+h; i <= r; i++) { int j = i; Item v = a[i]; while (j >= l+h && less(v, a[j-h])) { a[j] = a[j-h]; j -= h; } a[j] = v; }

Sorting Algorithms: Quick sort A divide and conquer algorithm  Partition an array into two parts  Sort the parts independently  Crux of the method is the partitioning process Arranges the array to make the following three conditions hold  The element a[i] is in the final place in the array for some I  None of the elements in a[l]….a[i-1] is greater than a[i]  None of the elements in a[i+1]….a[r] is less than a[i]

Sorting Algorithms: Quick sort int partition(Item a[], int l, int r); void quicksort(Item a[], int l, int r) { int i; if (r <= l) return; i = partition(a, l, r); quicksort(a, l, i-1); quicksort(a, i+1, r); } int partition(Item a[], int l, int r) { int i = l-1, j = r; Item v = a[r]; for (;;) { while (less(a[++i], v)) ; while (less(v, a[--j])) if (j == l) break; if (i >= j) break; exch(a[i], a[j]); } exch(a[i], a[r]); return i; }

Sorting Algorithms: Quick sort int partition(Item a[], int l, int r) { int i = l-1, j = r; Item v = a[r]; for (;;) { while (less(a[++i], v)) ; while (less(v, a[--j])) if (j == l) break; if (i >= j) break; exch(a[i], a[j]); } exch(a[i], a[r]); return i; } ij i