CS112_35_slides-trees - Trees Yinglin Wang Dept, SJTU CS112...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CS112 Fundamentals of Programming Abstractions Trees Yinglin Wang Dept, SJTU CS112 Fundamentals of Programming Abstractions contents Review of listADT Trees Binary trees Binary search trees
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions Review of Using listADT: An Exercise How would we write a recursive function to sort a list? There are two things that suggest that we might want to consider the first element in relation to the rest of the list. First, our general approach to recursion involves reducing the problem to a simpler one of the same form. For lists, the easiest way to come up with a simpler list is to look at the tail. Second, the recursive design of the data structure draws our attention to the tail of the list. What else is there to do recursion on? Sorting a list (7 2 9 6 3 8) 7 (2 9 6 3 8) CS112 Fundamentals of Programming Abstractions The Belief What would the recursion on the tail accomplish? It would sort it. Sorting a list (7 2 9 6 3 8) 7 (2 3 6 8 9)
Background image of page 2
CS112 Fundamentals of Programming Abstractions Combine the Result of Simpler Problems Now if we could just insert the first element of the original list into the proper place in the sorted tail, it would be done. Sorting a list (7 2 9 6 3 8) (2 3 6 7 8 9) CS112 Fundamentals of Programming Abstractions The Codes The main function is really simple.This is one of those cases where recursion almost seems like cheating. This definition says: "to sort a list, just insert the first element into the sorted tail." Easy! listADT ListSort(listADTlist) { if (list == NULL) return NULL; return ListInsert(ListHead(list), ListSort(ListTail(list))); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions The Codes Writing ListInsert isn’ t much harder. We just have to identify the base cases, then do the rest with recursion. Base case 1: if the list is empty, just return a new list whose one item is element. Base case 2: if element belongs on the front of the list, just "cons" it onto the front and return the result. Recursive case: insert the element into the tail of the list, then cons the head of the list (i.E., The first element) onto that. listADT ListSort(listADT list) { if (list == NULL) return NULL; return ListInsert(ListHead(list), ListSort(ListTail(list))); } listADT ListInsert (listElementTelement, listADT list) { if (list == NULL) return ListCons(element, NULL); if (element <= ListHead(list)) return ListCons(element, list); else return ListCons(ListHead(list), Insert(element, ListTail(list))); } CS112 Fundamentals of Programming Abstractions Trees Yinglin Wang Dept, SJTU
Background image of page 4
Programming Abstractions Trees Definition: trees are branching structures with a root node at the top, and with every other node connected to the root by a unique path. CS112 Fundamentals of
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/06/2010 for the course CS 112 taught by Professor Yinlinwang during the Spring '08 term at Shanghai Jiao Tong University.

Page1 / 25

CS112_35_slides-trees - Trees Yinglin Wang Dept, SJTU CS112...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online