Title: Recursion and Binary Trees
1Recursion and Binary Trees
- MINS 116
- Spring 2000
- Data Structures
2Topics
- Recursion
- Binary Search - Review
- Binary Trees
- Balancing Trees
3Recursion
- Recursive of, relating to, or constituting a
procedure that can repeat itself indefinitely or
until a specified condition is met.
4Recursion
- 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
-
- ...
5Recursion
- 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.
6Recursion - 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).
7Recursion - 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.
8Recursion - Stacks
After
Before
Item 3 is put on top of the stack.
9Recursion - Stacks
- To remove something from a stack, we can only
take off the top. This operation is called a
pop.
10Recursion - Stacks
After
Before
Item 3 is removed from the top of the stack.
11Recursion - 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.
12Recursion - 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.
13Binary Search - Review
- What types of searches are there?
- What is the efficiency of these searches?
14Binary 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.
15Binary 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?
16Binary 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.
17Binary 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
18Binary 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.
19Binary Trees - Building
20Binary Trees - Building
21Binary Trees - Building
22Binary Trees - Building
23Binary Trees - Building
24Binary Trees - Building
25Binary 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.
26Binary Trees - Traversal
- Preorder V L R
- What is the preorder traversal of
27Binary Trees - Traversal
- Inorder L V R
- What is the inorder traversal of
28Binary Trees - Traversal
- Postorder L R V
- What is the postorder traversal of
29Binary 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?
30Binary 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?
31Binary 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?
32Binary Trees - Deleting a node
- How would you go about deleting node 24 from the
binary tree?
33Binary 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.
34Binary Trees - Deleting a node
- Example - Case 1 Deleting a leaf (delete 4)
Becomes
35Binary Trees - Deleting a node
- Example - Case 1 Deleting a leaf (delete 4)
Tree
Container
Root
16
8
24
20
3
27
30
26
1
36Binary 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
37Binary Trees - Deleting a node
- Example - Case 2 Deleting empty right subtree
(delete 8)
Tree
Container
Root
16
24
3
20
27
1
30
26
38Binary 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
39Binary Trees - Deleting a node
- Example - Case 3 Deleting neither subtree is
empty (delete 24)
Tree
Container
Root
16
27
3
26
30
1
20
40Summary
- 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.