Lecture 06_Binary Tree Operation (1)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

ITEC 2620

Introduction to Data
Structures
:Binary Tree Operation
Khairul Bashar
School of Information Technology
York University, Toronto.
Tree Traversals

• We have stored our data into a Binary Search Tree (BST)


• We can perform binary search on our data
• We can update the data in the tree efficiently
• How do we print the data in order?
Tree Traversals

• It is easy to print an array in order


• How do we print this tree in order?
Print items in order

• Print (in order) everything before the root node, print the root node,
print everything after the root node
• Print left sub-tree in order -> 1, 2, 3, 4
• Print the root node -> 5
• Print the right sub-tree in order -> 6, 7, 8, 9
• How to print the sub-trees in order?
• Recursion!
Tree Traversals

• Recursive sub-problem
• Print left (sub) sub-tree in order, print (sub) tree root, print right
(sub) sub-tree
• Base problem
• When can you stop?
• What’s a trivial BST to print?
• One node?
Recursive Method: Tree Traversal
// a method within public class BinaryNode

public void printTree()


{
if (left != null)
left.printTree();
System.out.println(key);
if (right != null)
right.printTree();
}
Code Trace: Start

• rootPtr.printTree()
• left.printTree()
Code Trace

• left.printTree()
• left == null
• System.out.println(key)
• right.printTree()
• left == null
• System.out.println(key) -> 2
• right == null
• System.out.println(key) -> 3
• right.printTree()
• left == null
• System.out.println(key) -> 4
• right == null
• System.out.println(key) -> 5
• right.printTree()
• rootPtr.printTree();
• node3.printTree();
• node1.printTree();
• System.out.println(1);
• node2.printTree();
• System.out.println(3);
• node4.printTree();
Traversing orders

• Three orders for traversals


• In-order
• Print in sorted order
• Pre-order
• Search on non-sorted key
• Post-order
• Delete a tree
Pre-order Traversal

public void preOrder() {


System.out.println(key);
if (left != null)
left.preOrder();
if (right != null)
right.preOrder();
}
Pre-order Traversal
Post-Order Traversal

public void postOrder() {


if (left != null)
left.postOrder();
if (right != null)
right.postOrder();
System.out.println(key);
}
Post-order
Traversal
Rule
Non-recursive?

• Cannot do non-recursively
• Definition of a binary tree is recursive
• Binary tree operations are all recursive
• Recursion
• High-level concept of the algorithm
• Trust recursion to execute the subproblems properly
Time Complexity of Recursive
algorithms

• The problem is solved through smaller sub-problems


• The time to solve a problem is based on the time to solve smaller
subproblems
• Express the time to solve a problem of size n as T(n)
From Recursion to Recurrence
Relations

• Recursion has two cases


• Recursive case
• Base case
• Recurrence relations need two cases
• Recurring case
• Base case
• Usually T(1) = 1
• Eliminate constant factors
Factorial Example

• Building the recurrence relations


• n! = n * n-1! if n > 1
• The time to do a problem of size n is the time to do a problem of size n-1
+ a multiplication
• n! = 1 if n = 1, 0
• The time to do a problem of size 1 is 1 (a constant)
Runtime: Factorial

• Recurrence relations
• The time to do a problem of size n
• T(n)
• The time to do a problem of size n-1
• T(n-1)
• A multiplication
• 1
• T(n) = T(n-1) + 1 T(1) = 1
• T(n) = O(n)
Time for a Binary Tree Traversal

• Recurrence relation
• Number of recursive case -> n
• Time required in each recursive case -> constant
• Total time -> T(n) = n*1 = O(n)
Time for Recursive Binary Search

• T(n) = T(n/2) + C
• T(n/2) = T(n/4) + C + C
• T(n/4) = T(n/8) + C + C + C -> T(n/23) + 3C -> pattern?

• n/2k = 1 -> k is the number of steps required to reach an array of 1 item


Or, k = logn
• For a binary search time spent on each recursive case is constant
• Total time, T(n) = logn * 1 = O(logn)
Thank you

You might also like