RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls...

16
RECURSION Problem Solving with Computers-II

Transcript of RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls...

Page 1: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

RECURSION

Problem Solving with Computers-II

Page 2: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

Recursion

Zooming into a Koch’s snowflake

Sierpinski triangle

Koch’s snowflake

Fractal Tree

Asiep cmuse

q w

Recursion is atechnique that we useto solveproblems that have a

recursive structure A recursive structure can befound2 a freeloads

Page 3: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

Which of the following methods of class LinkedList CANNOT be implemented using recursion?

A. Finding the sum of all the values B. Printing all the values C. Deleting all the nodes in a linked list D. Searching for a value E. All the above can be implemented using recursion

recursively

Jo soe'm these problems Ionhave to inoffongpdeswiathfabreed

hecursive description of a linked list

list as a chain of Wdm A linkedlist is a node thathead J points to thefino node ofa

smaller limo list smaller linked list

Page 4: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

4050 2010

head

int IntList::sum(){

//Return the sum of all elements in a linked list }

Page 5: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

Helper functions• Sometimes your functions takes an input that is not easy to recurse on • In that case define a new function with appropriate parameters: This is

your helper function • Call the helper function to perform the recursion • Usually the helper function is private For example

Int IntList::sum(){ return sum(head); //helper function that performs the recursion.

}

Page 6: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

4050 2010

head

int IntList::sum(Node* p){

}

if C plreturn o H basecase no recursion required

The trim is to

Feturn p data 1 sum Lp next assumethat Tnhavesolved the

problem

for asmallerlinked

4 listvalue ofthe sum of the restofthe

listcurrent nods

Page 7: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

4050 2010

head

bool IntList::clear(Node* p){

}

Page 8: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

Concept Question

head tail(A)

(B): only the first node(C): A and B(D): All the nodes of the linked list (E): A and D

LinkedList::~LinkedList(){ delete head;

}

Which of the following objects are deleted when the destructor of Linked-list is called?

class Node { public: int info; Node *next; }; hesfinished

Tearing

p s

derek P Kcalls thedestructorof linked

list

Page 9: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

Concept question

head tail(A)

(B): All the nodes in the linked-list(C): A and B (D): Program crashes with a segmentation fault (E): None of the above

LinkedList::~LinkedList(){ delete head;

}

Which of the following objects are deleted when the destructor of Linked-list is called?

Node::~Node(){ delete next;

}fearsfinished

executing

Page 10: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

LinkedList::~LinkedList(){ delete head;

}

Node::~Node(){ delete next;

}

head tail

allstheHcalls model's destructor next node

destructorrecursive

deleteheadr nduelenert deletenext dyedbase case

lineedli model isdef Nodezisdotted Node 3 is deletedobjectisdeleted

Page 11: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

Binary Search • Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists.

• Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi].

• Ex. Binary search for 33.

821 3 4 65 7 109 11 12 14130

641413 25 33 5143 53 8472 93 95 97966

lo hi

33Lacmidlookinto the left half

lo hi mudO 14 7

0000 a Smio f mid 9 4 44lo

hi mid

phi

Page 12: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

Trees

!12

A tree has following general properties: • One node is distinguished as a root; • Every node (exclude a root) is connected

by a directed edge from exactly one other node;

A direction is: parent -> children • Leaf node: Node that has no children

Page 13: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

Which of the following is/are a tree?

A. B.

C.

D. A & B

E. All of A-C

!13

Page 14: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

Binary Search Tree – What is it?

42

32

12

45

41 50

!14

Do the keys have to be integers?

• Each node: • stores a key (k) • has a pointer to left child, right child

and parent (optional) • Satisfies the Search Tree Property

For any node, Keys in node’s left subtree <= Node’s key Node’s key < Keys in node’s right subtree

Page 15: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

Which of the following is/are a binary search tree?

42

32

12

42

3212

42

3212 65

30 38

A. B.

42

32

12

56

45

D.

C.

E. More than one of these

Page 16: RECURSION - GitHub Pages · delete head;} Node::~Node(){ delete next; } head tail allsthe Hcalls model's destructor next node destructor recursive deletehead duelenertr deletenextn

BSTs allow efficient search!

42

32

12

45

41 50

!16

• Start at the root; • Trace down a path by comparing k with the key of the

current node x: • If the keys are equal: we have found the key

• If k < key[x] search in the left subtree of x

• If k > key[x] search in the right subtree of x

Search for 41, then search for 53