recursion and binary tree

12
Recursion and Binary Recursion and Binary Tree Tree ICS 51 – Introductory Computer Organization

Upload: sawyer

Post on 06-Feb-2016

44 views

Category:

Documents


0 download

DESCRIPTION

Recursion and Binary Tree. ICS 51 – Introductory Computer Organization. Recursion. Recursion in computer programming is exemplified when a function is defined in terms of itself . Advantage of recursion : Simple to write and understand . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Recursion and Binary Tree

Recursion and Binary TreeRecursion and Binary TreeICS 51 – Introductory Computer Organization

Page 2: Recursion and Binary Tree

RecursionRecursionRecursion in computer programming is

exemplified when a function is defined in terms of itself.

Advantage of recursion: Simple to write and understand.

Disadvantage: Amount of memory required is proportional to the depth of recursion

Page 3: Recursion and Binary Tree

Problem 1Problem 1Implement the Factorial function

using assembly.

Given an unsigned integer number n,we define Factorial(n) as: ◦ Factorial(n)= 1 , if n=0 or n=1 ◦ n * Factorial(n-1), if n>1

Page 4: Recursion and Binary Tree

Binary tree and binary Binary tree and binary searchsearch tree treeA binary tree is a tree data structure in

which each node has at most two children.◦ Typically the child nodes are called left and right.

A binary search tree (BST) is a binary tree data structure which has the following properties:◦ Each node has a value. ◦ The left subtree of a node contains only values less

than the node's value. ◦ The right subtree of a node contains only values

greater than or equal to the node's value.

Page 5: Recursion and Binary Tree

An exampleAn example

Page 6: Recursion and Binary Tree

SearchingSearching Performed recursively because of the order in which

values are stored.

Begin by examining the root. If the value we are searching for equals the root, the value exists in the tree.

If it is less than the root, then it must be in the left subtree, so we recursively search the left subtree in the same manner.

Similarly, if it is greater than the root, then it must be in the right subtree, so we recursively search the right subtree.

If we reach a leaf and have not found the value, then the item is not where it would be if it were present, so it does not lie in the tree at all.

Page 7: Recursion and Binary Tree

InsertionInsertionInsertion begins as a search would beginIf the root is not equal to the value, we search the left or right subtrees as before. Eventually, we will reach an external node and add the value as its right or left child, depending on the node's value. In other words, we examine the root and recursively insert the new node to the left subtree if the new value is less than to the root, or the right subtree if the new value is greater or equal than the root.

Page 8: Recursion and Binary Tree

Example: Add 9 and 2Example: Add 9 and 2

9

2

Page 9: Recursion and Binary Tree

Tree TraversalTree TraversalStarting at the root of a binary tree,

there are three main steps that can be performed with the order that they are performed defining the traversal type.◦performing an action on the current node

(referred to as printing the node);◦repeating the process with the left

subtree◦repeating the process with the right

subtree

Page 10: Recursion and Binary Tree

In order traversalIn order traversalTo traverse a non-empty binary tree in

inorder, perform the following operations:◦ Traverse the left subtree in inorder◦ Print the root of current subtree.◦ Traverse the right subtree in inorder.

inorder(node) if node.left ≠ null then inorder(node.left) print node.value if node.right ≠ null then inorder(node.right)

Page 11: Recursion and Binary Tree

Example: In orderExample: In order

In-order (LPR) traversal yields: A, B, C, D, E, F, G, H, I

Remember:- Left- Print- Right

Page 12: Recursion and Binary Tree

Problem 2: Problem 2: Given an array of integers, build a binary search tree

containing the elements of the array.

You have to write a recursive function insertNode(struct tNode* root,struct tNode* newNode, unsigned long data), where root is the root of the binary search tree.

After building the tree , you will do an inorder (left subtree-root-right subtree) traversal using the sortedTree(struct tNode* root, unsigned long a[ ], unsigned long *counter) function and you will store the elements of the tree in the a array. After doing this, the a array will contain the elements of the tree sorted in ascending order.

Next, you have to implement the reversedTree(struct tNode* root, unsigned long a[ ], unsigned long *counter) function, which will place the elements of the tree into the a array , sorted in descending order (a right subtree-root-left subtree traversal)