0% found this document useful (0 votes)
26 views

Trees Lecture - G5 -II - No Code

The document discusses binary trees and binary search trees, focusing on operations such as calculating maximum depth, searching, insertion, and deletion. It outlines algorithms for these operations, including handling different cases for node deletion and the time complexity associated with various tree operations. Additionally, it highlights common pitfalls and applications of trees in data structures and algorithms.

Uploaded by

remidan37
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)
26 views

Trees Lecture - G5 -II - No Code

The document discusses binary trees and binary search trees, focusing on operations such as calculating maximum depth, searching, insertion, and deletion. It outlines algorithms for these operations, including handling different cases for node deletion and the time complexity associated with various tree operations. Additionally, it highlights common pitfalls and applications of trees in data structures and algorithms.

Uploaded by

remidan37
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/ 40

Trees

Binary Trees and Binary


Search Trees

Part II
Simulation
Given the root of a binary tree, return its maximum depth. A binary tree maximum
depth is the number of nodes along the longest path from the root node down to
the farthest leaf node.

Example 1:
3
Input: root = [3,9,20,null,null,15,7]
Output: 3
9 20 Example 2:
Input: root = [1,null,2]
Output: 2
15 7
2
Simulation - Solution
● What if the tree is empty?
Answer: max_depth = 0

● What if we have just a node with no children?


Answer: max_depth = 1

● What if the current node has left and/or right children?


Answer: max_depth = 1 + max(left_child_max_depth,
right_child_max_depth)

By recursively calculating the max_depth of each subtree

3
Simulation - Solution
1 + max(max_depth(root.left), max_depth(root.right))

9 20

15 7
4
Simulation - Solution
1 + max(max_depth(root.left), max_depth(root.right))

1
9 20

15 7
5
Simulation - Solution
1 + max(1, max_depth(root.right))

9 20

15 7
6
Simulation - Solution
1 + max(1, max_depth(root.right))

1 + max(max_depth(root.left),
max_depth(root.right))

9 20

15 7
7
Simulation - Solution
1 + max(1, max_depth(root.right))

1 + max(max_depth(root.left),
max_depth(root.right))

9 20

15 7
8
Simulation - Solution
1 + max(1, max_depth(root.right))

1 + max(1,max_depth(root.right))

9 20

15 7
9
Simulation - Solution
1 + max(1, max_depth(root.right))

1 + max(1,max_depth(root.right))

9 20

15 7
10
Simulation - Solution
1 + max(1, max_depth(root.right))

1 + max(1, 1) = 2

9 20

15 7
11
Simulation - Solution
1 + max(1,2) = 3

9 20

15 7
12
Question
Insert into a Binary
Search Tree

13
Basic Operation on Trees

14
Operation - Searching
● The algorithm depends on the property of BST that if each left subtree
has values below parent and each right subtree has values above the
parent.

● If the value is below the parent, we can say for sure that the value is
not in the right subtree; we need to only search in the left subtree

● If the value is above the parent, we can say for sure that the value is
not in the left subtree; we need to only search in the right subtree.

● Let us try to visualize this with a diagram searching for 4 in the tree:

15
Operation - Searching

8
8

3 10

1 6 14

4 7

16
Operation - Searching

8
3

3 10
8

1 6 14

4 7

17
Operation - Searching

8
6

3 10
3

1 6 14
8

4 7

18
Operation - Searching

8
4

3 10
6

1 6 14
3

8
4 7

19
Operation - Insertion

● Inserting a value in the correct position is similar to searching because we

try to maintain the BST rule that the left subtree is lesser than root and the
right subtree is larger than root.
● We keep going to either right subtree or left subtree depending on the value
and when we reach a point left or right subtree is null, we put the new node
there.

Let's try to visualize how we add a number 5 to an existing BST.

20
Operation - Insertion
Insert 5 in to the BST
8

3 10

1 6
14

4 7

21
Operation - Insertion
Insert 5 in to the BST
8

3 10

1 6
14

4 7

22
Operation - Insertion
Insert 5 in to the BST
8

3 10

1 6
14

4 7

23
Operation - Insertion
Insert 5 in to the BST
8

3 10

1 6
14

4 7

24
Operation - Insertion
Insert 5 in to the BST
8

3 10

1 6
14

4 7

5
25
Practice Problem
Delete Node in a BST

26
Operation - Deletion
● There are three cases for deleting a node from a binary search tree.

Case One: In the first case, the node to be deleted is the leaf node. In such a case, simply
delete the node from the tree. 4 is to be deleted.

3 10

1 6
14

4 7

27
Operation - Deletion
8

3 10

1 6
14

4 7

28
Operation - Deletion
Case Two: In the second case, the node to be deleted lies has a single child node.
In such a case follow the steps below:

1. Replace that node with its child node.


2. Remove the child node from its original position.
8

3 10

1 6
14

4 7
29
Operation - Deletion
8

3 10

1 6
14

4 7

30
Operation - Deletion
Case Three: The node to be deleted has two children. In such a case follow the steps

below:

1. Get the inorder successor of that node. Why?

2. Replace the node with the inorder successor.

3. Remove the inorder successor from its original position.

31
Operation - Deletion
8 Inorder traversal: 1 3 4 6 7 8 10 14

3 10

1 6
14

4 7

32
Operation - Deletion
8 Inorder traversal: 1 3 4 6 7 8 10 14

3 10

1 6
14

4 7

33
Operation - Deletion
8 Inorder traversal: 1 4 6 7 8 10 14

4 10

1 6
14

3 7

34
Time and Space Complexity Analysis
Binary Tree Binary Search Tree

● Traversing ● Traversing
○ Time = ? ○ Time = ?
● Searching ● Searching
○ Time = ? ○ Time = ?
● Insertion ● Insertion
○ Time = ? ○ Time = ?
● Deletion ● Deletion
○ Time = ? ○ Time = ?
● Space = ? ● Space = ?

35
Time and Space Complexity Analysis
Binary Tree Binary Search Tree

● Traversing ● Traversing
○ Time = O(n) ○ Time = O(n).

● Searching ● Searching
○ Time = O(n) ○ Time = O(h) where h is the height of BST

● Insertion ● Insertion
○ Time = O(n) ○ Time = O(h)

● Deletion ● Deletion
○ Time = O(n) ○ Time = O(h)
● Space = O(n) … Why? ● Space = O(n)

36
Common Pitfalls

● Null pointer exceptions


● Assuming the tree is balanced
● Wrong choice of traversal
● Wrong recurrence relations and base cases
● Stack overflow

37
Applications of Trees
● Representation structure in File Explorer. (Folders and Subfolders) uses N-ary Tree.
● Auto-suggestions when you google something using Trie.
● Used in decision-based machine learning algorithms.
● Tree forms the backbone of other complex data structures like heap, priority queue,
spanning tree, etc.
● A binary tree is used in database indexing to store and retrieve data in an efficient
manner.
● Binary Search Trees (BST) can be used in sorting algorithms.

38
Practice Questions
● Merge Two Binary Trees
● Search in Binary Search Trees
● Same Tree
● Lowest Common Ancestor of Binary
Search Tree
● Validate Binary Search Trees
● Binary Tree Zigzag Level Order
Traversal
● Maximum Difference Between Node
and Ancestor
● Kth smallest Element in BST
● Maximum Sum BST in Binary Tree
39
Quote of the Day

"A tree with strong roots laughs at storms."


- Malay Proverb

40

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy