Академический Документы
Профессиональный Документы
Культура Документы
2
Binary Search Tree (BST)
3
Binary Search Tree (BST)
4
Binary Search Tree (BST)
• Function
Insertion: Use the
binary search tree
property to insert
the new item at the
correct place
5
AVL Tree
Definition:
An AVL tree is a binary search tree such that
for any node in the tree, the height of the left and
right subtrees can differ by at most 1.
6
AVL Tree
• Two binary search trees: (a) an AVL tree; (b) not an AVL
tree (unbalanced nodes are darkened)
7
AVL Tree
8
AVL Tree
9
Red-Black (RB) Tree
10
Red-Black (RB) Tree
11
Algorithm Paradigms:
12
Algorithm Design Paradigm: Dynamic
Programming
• Dynamic Programming is a general algorithm design technique
• for solving problems defined by or formulated as recurrences with
overlapping subinstances
• Main idea:
- set up a recurrence relating a solution to a larger instance to
solutions of some smaller instances
• - solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
13
Algorithm Design Paradigm: Dynamic
Programming
• Dynamic Programming is an algorithm design technique for
optimization problems: often minimizing or maximizing.
• Like divide and conquer, DP solves problems by combining
solutions to subproblems.
• Unlike divide and conquer, subproblems are not
independent.
– Subproblems may share subsubproblems,
– However, solution to one subproblem may not affect the solutions to
other subproblems of the same problem. (More on this later.)
• DP reduces computation by
– Solving subproblems in a bottom-up fashion.
– Storing solution to a subproblem the first time it is solved.
– Looking up the solution when subproblem is encountered again.
• Key: determine structure of optimal solutions
14
Algorithm Design Paradigm: Dynamic
Programming
Steps in Dynamic Programming:
1. Characterize structure of an optimal solution.
2. Define value of optimal solution recursively.
3. Compute optimal solution values either top-down with
caching or bottom-up in a table.
4. Construct an optimal solution from computed values.
We’ll study these with the help of examples.
15
Lab Assignment:
16
Lab Assignment:
17
Lab Assignment:
18
Lab Assignment:
19
Lab Assignment:
20
Lab Assignment:
21
Happy Coding…
22