0% found this document useful (0 votes)
91 views22 pages

CSN261 PPT

The document is a lab assignment document for a data structures course that discusses various tree data structures like binary search trees (BSTs), AVL trees, and red-black (RB) trees. It provides definitions and properties of each tree type. It also discusses algorithm design paradigms like dynamic programming. The document aims to help students complete coding assignments on tree implementations and algorithms.

Uploaded by

Leshna Balara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views22 pages

CSN261 PPT

The document is a lab assignment document for a data structures course that discusses various tree data structures like binary search trees (BSTs), AVL trees, and red-black (RB) trees. It provides definitions and properties of each tree type. It also discusses algorithm design paradigms like dynamic programming. The document aims to help students complete coding assignments on tree implementations and algorithms.

Uploaded by

Leshna Balara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

INDIAN INSTITUTE OF TECHNOLOGY ROORKEE

CSN-261 (DATA STRUCTURES LABORATORY)


Lab Assignment 3

Dr. Sudip Roy


Assistant Professor
Department of Computer Science and Engineering

Piazza Class Room: https://piazza.com/iitr.ac.in/fall2019/csn261


[Access Code: csn261_2019]
Moodle Submission Site: https://moodle.iitr.ac.in/course/view.php?id=46
[Enrollment Key: csn261_2019]
Binary Search Tree (BST)

• Binary Search Tree Property:


The value stored at
a node is greater than
the value stored at its
left child and less than
the value stored at its
right child

2
Binary Search Tree (BST)

• Binary Search Tree


Property:
• In a BST, the value stored
at the root of a subtree is
greater than any value in
its left subtree and less
than any value in its right
subtree!

3
Binary Search Tree (BST)

• Where is the smallest


element?
• Ans: leftmost element

• Where is the largest


element?
• Ans: rightmost element

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

• An AVL tree is a binary search tree with a balance


condition.
• AVL is named for its inventors: Adel’son-Vel’skii and
Landis
• AVL tree approximates the ideal tree (completely
balanced tree).
• AVL Tree maintains a height close to the minimum.

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

• Minimum tree of height H

8
AVL Tree

• The depth of a typical node in an AVL tree is very close to


the optimal log N.
• Consequently, all searching operations in an AVL tree
have logarithmic worst-case bounds.
• An update (insert or remove) in an AVL tree could destroy
the balance. It must then be rebalanced before the
operation can be considered complete.
• After an insertion, only nodes that are on the path from
the insertion point to the root can have their balances
altered.

9
Red-Black (RB) Tree

Definition: a binary tree, satisfying:


1. Every node is red or black
2. The root is black
3. Every leaf is NIL and is black
4. If a node is red, then both its children are black
5. For each node, all paths from the node to descendant
leaves contain the same number of black nodes.
• Purpose: keep the tree balanced.
• Other balanced search tree:
– AVL tree, 2-3-4 tree, Splay tree, Treap

10
Red-Black (RB) Tree

• The root of a Red Black tree is black


• Every other node in the tree follows these rules:
– Rule 4: If a node is Red, all of its children are Black
– Rule 5: The number of Black nodes must be the same in all paths
from the root node to null nodes

11
Algorithm Paradigms:

• Algorithm types we will consider include:


– Simple recursive algorithms
– Backtracking algorithms
– Divide and conquer algorithms
– Dynamic programming algorithms
– Greedy algorithms
– Branch and bound algorithms
– Brute force algorithms
– Randomized algorithms

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

• Invented by American mathematician Richard Bellman in the 1950s to


solve optimization problems and later assimilated by CS

• “Programming” here means “planning”

• 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

You might also like