© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Computer Science C++ High School Level By Guillermo Moreno.
1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
1 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
C++ Plus Data Structures
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Searching Arrays Linear search Binary search small arrays
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.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
CIS 068 Welcome to CIS 068 ! Lesson 9: Sorting. CIS 068 Overview Algorithmic Description and Analysis of Selection Sort Bubble Sort Insertion Sort Merge.
1 MT258 Computer Programming and Problem Solving Unit 9.
F453 Computing Searches. Binary Trees Not this kind of tree!
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
CSC 211 Data Structures Lecture 13
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Searching and Sorting Algorithms
Lecture No.45 Data Structures Dr. Sohail Aslam.
Data Structures Using C++ 2E
Binary Search Trees.
Chapter 20: Binary 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.
Description Given a linear collection of items x1, x2, x3,….,xn
Chapter 21: Binary Trees.
Quicksort and Mergesort
CSC215 Lecture Algorithms.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Welcome to CIS 068 ! Lesson 9: Sorting CIS 068.
IT 4043 Data Structures and Algorithms
Search,Sort,Recursion.
Searching.
Search,Sort,Recursion.
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
Heaps By JJ Shepherd.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
EE 312 Software Design and Implementation I
Presentation transcript:

© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms

© M. Gross, ETH Zürich, 2014 Agenda  Trees  Binary Trees  Binary Search Trees  Sorting Algorithms

© M. Gross, ETH Zürich, 2014 Linked Lists & Trees  A linked list is a „tree“ with degree 1  A binary tree is a tree with degree 2 Linked list Binary tree

© M. Gross, ETH Zürich, 2014 Definitions for Trees

© M. Gross, ETH Zürich, 2014 Tree Node Element Struct struct element { int value; // The value of the element struct element *left; // Pointer to the left child struct element *right; // Pointer to the right child }; typedef struct element Node; Node *root = NULL; //Pointer to root element  Like a list element but with more than one child (Pointer)  Value can be anything  „Empty Tree“ is just a NULL pointer  Example for binary tree nodes

© M. Gross, ETH Zürich, 2014 Use Recursive Functions  A tree is a fractal:  If root is a large tree  root->left will also be a tree  A function that can handle the root node can also handle the child nodes

© M. Gross, ETH Zürich, 2014 Use Recursive Functions void print_recursive( element * node ) { Cout <<... // print value // to print the childrens content print_recursive(... ); }  Create a function that takes any tree element as a parameter  Let the function print the data of the element  Print the data of its child elements recursively

© M. Gross, ETH Zürich, 2014 Use Recursive Functions void print_recursive( element * node ) { if( node != NULL ) { Cout <<... print_recursive(... );... }  Don't forget the base  Otherwise: Endless loop or (here) NULL pointer dereferencing

© M. Gross, ETH Zürich, 2014 Traversing Recursively void recursive( element * node ) { if( node != NULL ) { recursive( node->left ); //do something with this node recursive( node->right ); } Initial call node Iteration 1 node Iteration 2 node

© M. Gross, ETH Zürich, 2014 Searching an Unsorted Tree  Check if tree is empty  Check if value of current node is the value we search -> return true;  Else look in child nodes recursively  Stop looking in child nodes when result was found or there are no more children

© M. Gross, ETH Zürich, 2014 Searching an Unsorted Tree: Example bool rec_search( Node *node, int i ) { if (node == NULL) { return false; } if (node->value == i) { return true; } return rec_search(node->left,i) || rec_search(node->right,i); }

© M. Gross, ETH Zürich, 2014 Deleting Tree Elements  It's simple to create a memory leak void delete_element( Node * node ) { delete node; }

© M. Gross, ETH Zürich, 2014 Deleting Tree Elements  Check for empty trees  Delete all Childelements first with recursion  After that delete this node too void delete_element( Node * node ) { if( node != NULL ) { delete_element( node->left ); delete_element( node->right ); delete node; }

© M. Gross, ETH Zürich, 2014 Binary Search Tree  Just like usual Trees, but on the left there is always the smaller key and on the right the larger (or vise versa)  Makes it much easier to search for a given key.

© M. Gross, ETH Zürich, 2014 Binary Search Tree Example searching without recursion Node * next = root; int search = 6; while( next != NULL && next->value != search ) { if( next->value < search ) { next = next->right; } else { next = next->left; }

© M. Gross, ETH Zürich, 2014 Insert Element in Tree  Create new node  In search tree:  Search for value you want to insert until reaching null  Change the adress of the null pointer to the adress of your new node Node *node = new Node; node->value = 42; node->right = NULL; node->left = NULL;

© M. Gross, ETH Zürich, 2014 Sorting  Frequently used in software  File Manager: Detail View  Task Manager: Sort by RAM usage  Managing large Music collections  Task: find an order in a sequence of n data elements  The higher n, the more time t it takes to sort

© M. Gross, ETH Zürich, 2014 Efficiency Algorithm:SelectionBubbleMergeQuick Worst case О(n^2) O(n log n)O(n^2) Best case О(n^2)O(n)O(n log n) Average case О(n^2) O(n log n)

© M. Gross, ETH Zürich, 2014 Selectionsort 1. Find the minimum value in the list 2. Swap it with the value in the first position 3. Repeat the steps above for the remainder of the list (starting at the second position and advancing each time)

© M. Gross, ETH Zürich, 2014 Selectionsort

© M. Gross, ETH Zürich, 2014 Selectionsort

© M. Gross, ETH Zürich, 2014 Selectionsort

© M. Gross, ETH Zürich, 2014 Bubblesort  Bubble sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.  The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

© M. Gross, ETH Zürich, 2014 Bubblesort

© M. Gross, ETH Zürich, 2014 Bubblesort

© M. Gross, ETH Zürich, 2014 Mergesort  Divide and conquer algorithm  Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).  Repeatedly Merge sublists to produce new sublists until there is only 1 sublist remaining.

© M. Gross, ETH Zürich, 2014 Mergesort

© M. Gross, ETH Zürich, 2014 Mergesort

© M. Gross, ETH Zürich, 2014 Quicksort  divide and conquer algorithm.  first divides a large list into two smaller sub-lists: the low elements and the high elements.  Quicksort can then recursively sort the sub-lists.  The steps are:  Pick an element, called a pivot, from the list.  Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it  After this partitioning, the pivot is in its final position. This is called the partition operation.  Recursively sort the sub-list of lesser elements and the sub-list of greater elements.

© M. Gross, ETH Zürich, 2014 Quicksort, Example

© M. Gross, ETH Zürich, 2014 Quicksort