Trees and Recursive Algorithms. Recursive Algorithms A function can call another function; What would happen if a statement in F contained a call of F?

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

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.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Trees in C CSE 2541 Rong Shi. Tree definition Recursively defined data structure Tree (in general) – Empty – Data + a specific number of subtrees Binary.
BINARY TREES && TREE TRAVERSALS. DEFINITION : Binary Tree A binary tree is made of nodes Each node contains –a "left" pointer -- left child –a "right"
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
Trees. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. Nonlinear Containers root node leaf edge.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree Data Structures.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
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.
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.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
Binary Search Trees (BST)
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
CSE 373 Data Structures Lecture 7
Chapter 12 – Data Structures
Data Structure and Algorithms
Recursive Objects (Part 4)
Binary Search Trees Chapter 7 Objectives
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
ITEC 2620M Introduction to Data Structures
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Binary Trees.
slides created by Alyssa Harding
Abstract Data Structures
Trees.
Tree data structure.
Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Binary Trees.
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Binary Search Tree.
Presentation transcript:

Trees and Recursive Algorithms

Recursive Algorithms A function can call another function; What would happen if a statement in F contained a call of F? – Recursive. – Reduce the original problem to smaller problem(s), and execute the same function to each smaller problem. – For the “smallest” problem (stopping case), solve it directly.

Recursive Example Recursive code usually looks like this: if (the stopping case is reached) { Solve it } else { Reduce the problem using recursion } Here is a recursive function to compute the factorial of a positive number. int Factorial (int N) // Computes the factorial of N recursively, returns N! { if (N == 1) { return 1; // stopping case } else { return N * Factorial(N-1); // recursion } }

Tree definition Recursively defined data structure Tree (in general) – Empty – Data + a specific number of subtrees Binary tree – Empty – Data + left subtree + right subtree

C Binary Tree node struct btnode { int data; struct btnode *left; struct btnode *right; } ;

Visual example of a binary tree Each value corresponds to a node in the tree root is a struct btnode pointer that points at the node containing the 9

Tree traversal How to visit each node only once. Do not miss any node.

Tree traversal (preorder) PreOrderPrint(struct btnode *anode) { if(anode == NULL) return; printf(“%i ”, anode->data); PreOrderPrint(anode->left); PreOrderPrint(anode->right); } Output of PreOrderPrint(root) is:

Tree traversal (preorder incorrect) PreOrderPrint(struct btnode *anode) { printf(“%i”, anode->data); PreOrderPrint(anode->left); PreOrderPrint(anode->right); } Any problems with this function?

Tree traversal (inorder) InOrderPrint(struct btnode *anode) { if(anode == NULL) return; InOrderPrint(anode->left); printf(“%i ”, anode->data); InOrderPrint(anode->right); } Output of InOrderPrint(root) is:

Tree traversal (postorder) PostOrderPrint(struct btnode *anode) { if(anode == NULL) return; PostOrderPrint(anode->left); PostOrderPrint(anode->right); printf(“%i ”, anode->data); } Output of PostOrderPrint(root) is:

Tree termination NULL pointers NULL are not btnodes, but the value of their parent’s left and right pointers NULL data -1 are btnodes, whose left and right btnodes are uninitialized Assumption: -1 is never valid data in the tree

Creating nodes struct node * NewNode(int data) { struct node *mynode = (struct node *) malloc (sizeof(struct node)); mynode->data = data; mynode->left = NULL; mynode->right = NULL; return(node); }

Deallocating binary trees Three things to do – Free current node – Recursively free left subtree – Recursively free right subtree What is the order? void delete_tree(struct btnode *leaf) { if( leaf != NULL ) { delete_tree(leaf->left); delete_tree(leaf->right); free( leaf ); }