slides

advertisement
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.

Disadvantage: Amount of memory required is
proportional to the depth of recursion
Problem 1

Implement 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
Binary tree and binary search tree

A 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.
An example
Searching

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.
Insertion

Insertion begins as a search would begin

If 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.
Example: Add 9 and 2
9
2
Tree Traversal

Starting 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
In order traversal

To 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)
Example: In order
Remember:
- Left
- Print
- Right
In-order (LPR) traversal yields: A, B, C, D, E, F, G, H, I
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)
Download