Recursion and Binary Tree - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Recursion and Binary Tree

Description:

... the left subtree, so we recursively search the left subtree in the same manner. ... root, or the right subtree if the new value is greater or equal than the root. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 13
Provided by: Ari59
Category:

less

Transcript and Presenter's Notes

Title: Recursion and Binary Tree


1
Recursion and Binary Tree
  • ICS 51 Introductory Computer Organization

2
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

3
Problem 1
  • Implement the Factorial function using assembly.
  • Given an unsigned integer number n,we define
    Factorial(n) as
  • Factorial(n) 1 , if n0 or n1
  • n Factorial(n-1), if ngt1

4
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.

5
An example
6
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.

7
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.

8
Example Add 9 and 2
9
2
9
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

10
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.righ
    t)

11
Example In order
Remember - Left - Print - Right
In-order (LPR) traversal yields A, B, C, D, E,
F, G, H, I
12
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)
Write a Comment
User Comments (0)
About PowerShow.com