Recursion and Binary Trees - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Recursion and Binary Trees

Description:

In the binary tree, you can 'push' the last node you have visited, giving the ... Binary Trees - Building ... Binary Trees - Traversal. Definitions: V - Visit ... – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 41
Provided by: Jim455
Category:

less

Transcript and Presenter's Notes

Title: Recursion and Binary Trees


1
Recursion and Binary Trees
  • MINS 116
  • Spring 2000
  • Data Structures

2
Topics
  • Recursion
  • Binary Search - Review
  • Binary Trees
  • Balancing Trees

3
Recursion
  • Recursive of, relating to, or constituting a
    procedure that can repeat itself indefinitely or
    until a specified condition is met.

4
Recursion
  • Recursion is when a function calls itself from
    within itself.
  • E.g.
  • public int countDown( int count )
  • if ( count 0 )
  • count--
  • retVal countDown( count
    )
  • return retVal
  • ...

5
Recursion
  • All recursive methods require
  • Argument (or arguments) passed into the method.
  • Return value resulting from the recursive call.
  • Logical test to terminate the recursive call.

6
Recursion - Stacks
  • To understand what is happening with recursion,
    we must understand a data structure called
    stacks.
  • A Stack is known as a Last In - First Out data
    structure (LIFO).

7
Recursion - Stacks
  • How a Stack works - A stack is just like a stack
    of books, papers, lunch trays, etc.
  • When you add something to a stack, it gets placed
    on the top. This is called a push event.

8
Recursion - Stacks
  • Push Item 3

After
Before
Item 3 is put on top of the stack.
9
Recursion - Stacks
  • To remove something from a stack, we can only
    take off the top. This operation is called a
    pop.

10
Recursion - Stacks
  • Pop Item 3

After
Before
Item 3 is removed from the top of the stack.
11
Recursion - Stacks
  • How do stacks relate to recursion?
  • The Java virtual machine uses a stack called a
    call stack.
  • Every time a method call is made, the return
    address is pushed onto the stack.
  • When a return is made from a method call, the
    return address is popped off of the stack.
  • The stack keeps track of where the call came
    from allowing the ability to get back to the
    original caller.

12
Recursion - Stacks
  • You can simulate the call stack by using a stack
    structure in your programs. For instance
  • In the binary tree, you can push the last node
    you have visited, giving the ability to retrace
    your steps in the tree traversal.

13
Binary Search - Review
  • What types of searches are there?
  • What is the efficiency of these searches?

14
Binary Search - Review
  • I am thinking of a number between 1 and 100.
  • I will tell you if you need to guess higher or
    lower.
  • Guess the number.

15
Binary Search - Review
  • What condition must be true to use a binary
    search?
  • How many comparisons are required to find an item
    in a binary search?

16
Binary Trees
  • A binary tree is a data structure used to store
    information in a sorted order.
  • A node in a binary tree has reference to an item
    which is greater and an item which is less.
  • As in linked lists, a node is merely a place
    holder for the information being contained in the
    tree.

17
Binary Trees
Level Items
1 1
2 3
3 7
4 15
5 31
In general, the number of items you can store is
one less than the number 2 raised to the Nth
power where N is the number of levels. 2 - 1
N
18
Binary Trees - Building
  • Building of a binary tree requires the insertion
    of a node based on the information contained
    within the node.
  • Example insert the integers 16, 8, 24, 20, 3 and
    25 into a binary tree structure.

19
Binary Trees - Building
  • 16

20
Binary Trees - Building
  • 16, 8

21
Binary Trees - Building
  • 16, 8, 24

22
Binary Trees - Building
  • 16, 8, 24, 20

23
Binary Trees - Building
  • 16, 8, 24, 20, 3

24
Binary Trees - Building
  • 16, 8, 24, 20, 3, 25

25
Binary Trees - Traversal
  • Definitions
  • V - Visit the node. This is when the information
    referenced in the node is accessed.
  • L - Traverse the node to the left.
  • R - Traverse the node to the right.
  • These rules apply recursively to each node of the
    tree.

26
Binary Trees - Traversal
  • Preorder V L R
  • What is the preorder traversal of

27
Binary Trees - Traversal
  • Inorder L V R
  • What is the inorder traversal of

28
Binary Trees - Traversal
  • Postorder L R V
  • What is the postorder traversal of

29
Binary Trees - Traversal
  • Questions
  • How can a stack be used to help with binary tree
    traversal?
  • How can recursion be used in binary tree
    traversal?
  • How are recursion and the use of a stack
    different?

30
Binary Trees
  • Exercise
  • Build a binary tree out of the number sequence
    1, 3, 5, 7, 13, 24, 31
  • What do you notice about the tree that was
    constructed?

31
Binary Trees
  • Exercise (contd)
  • Does the above binary tree have any advantage
    over a singly linked list?
  • What feature of the data sequence caused the
    special tree configuration?
  • How might you balance this tree?

32
Binary Trees - Deleting a node
  • How would you go about deleting node 24 from the
    binary tree?

33
Binary Trees - Deleting a node
  • Cases to consider
  • Deletion of a leaf (a leaf is a node with neither
    a left or right node attached).
  • Deletion of a node with one empty subtree (a
    subtree is all nodes below a node with the
    current node as root)
  • Deletion of a node where neither subtree is empty.

34
Binary Trees - Deleting a node
  • Example - Case 1 Deleting a leaf (delete 4)

Becomes
35
Binary Trees - Deleting a node
  • Example - Case 1 Deleting a leaf (delete 4)

Tree
Container
Root
16
8
24
20
3
27
30
26
1
36
Binary Trees - Deleting a node
  • Example - Case 2 Deleting empty right subtree
    (delete 8)

Tree
Container
Becomes
Root
16
8
24
20
3
27
30
26
1
37
Binary Trees - Deleting a node
  • Example - Case 2 Deleting empty right subtree
    (delete 8)

Tree
Container
Root
16
24
3
20
27
1
30
26
38
Binary Trees - Deleting a node
  • Example - Case 3 Deleting neither subtree is
    empty (delete 24)

Tree
Container
Root
Becomes
16
24
3
20
27
1
30
26
39
Binary Trees - Deleting a node
  • Example - Case 3 Deleting neither subtree is
    empty (delete 24)

Tree
Container
Root
16
27
3
26
30
1
20
40
Summary
  • Recursion is a method calling itself.
  • Recursion uses the virtual machine stack to keep
    track of program execution.
  • Binary trees are a specialized data structures
    dealing with storing data in a specific order.
  • Recursion and stacks can be used for traversal
    and manipulation of binary trees.
Write a Comment
User Comments (0)
About PowerShow.com