Binary Search Trees: Implementation, Properties, and Comparison with Sorted Arrays, Study notes of C programming

An overview of Binary Search Trees (BSTs), including their implementation, operations supported, and comparison with sorted arrays. It covers the time complexity of various operations and discusses the concept of a balanced BST. The document also includes examples and references to resources for further learning.

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

kourtney
kourtney 🇺🇸

4.8

(6)

220 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE 100: BSTS, BIG O,
AND C++
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Binary Search Trees: Implementation, Properties, and Comparison with Sorted Arrays and more Study notes C programming in PDF only on Docsity!

CSE 100: BSTS, BIG O,

AND C++

Announcements

  • PA0 due Tuesday @10am. PA1 coming Tuesday (noon).

Will be due a week from the following Friday.

  • Concentrate on the reading assignment until then

Binary Search Tree – What is it?

42 32 12 45 41 50 47 46 51 48

Which of the following is/are a binary search tree? 42 32 12 42 12 32 42 12 32 65 30 38

A. B.

42 32 12 56 45

D.
C.

E. More than one of these

Binary Search Trees

  • What is it good for?
    • If it satisfies a special property i.e. Balanced, you can think of it as

a dynamic version of the sorted array

Operations supported: sorted array

Running Time

Operations (sorted array)

Search

Selection

Min/Max

Predecessor/ Successor

Rank

Output in sorted order

Ref: Tim Roughgarden (Stanford)

Operations supported: Balanced Binary Search Trees vs. Sorted Array

Running Time

Operations (Balanced-BST) (sorted array)

Search O(log 2 n)

Selection O(1)

Min/Max O(1)

Predecessor/ Successor O(1)

Rank O(log 2 n)

Output in sorted order O(n)

Insert O(n)

Delete O(n)

Ref: Tim Roughgarden (Stanford)

Operations supported: Balanced Binary Search Trees vs. Sorted Array

Running Time

Operations (Balanced-BST) (sorted array)

Search O(log 2 n) O(log 2 n)

Selection O(log 2 n) O(1)

Min/Max O(log 2 n) O(1)

Predecessor/ Successor O(log 2 n) O(1)

Rank O(log 2 n) O(log 2 n)

Output in sorted order O( n) O( n)

Insert O(log 2 n) O( n)

Delete O(log 2 n) O( n)

Ref: Tim Roughgarden (Stanford)

Under the hood of the BST: Searching an element in the tree 42 32 12 45 41

Q: In the worst case how many comparisons do we

need to find an element in any tree and in particular

the given tree

A. The number of nodes in the tree, 5

B. The number of leaves in the tree, 3

C. The number of roots in the tree, 1

D. None of the above

Towards describing the running time of the search operation: Height of the tree What is the height of the given tree?

Height of a node: Longest path from node to any leaf node plus one

Height of the tree: Height of the root

42 32 12 45 41

Relating H (height) and N (#nodes) for a Balanced BST Level 0 Level 1 Level 2 … … How many nodes are on level L in a completely filled binary search tree? A. 2 B. L C. 2*L D. 2 L

Relating H (height) and N (#nodes)

Level 0 Level 1 Level 2 … … How many nodes are in a completely filled BST? A.

Relating H (height) and N (#nodes)

Level 0 Level 1 Level 2 … … How many nodes are in a completely filled BST? 𝑁 = ෍ 2 ௅ ு ௅ୀ଴

௅ 𝑁 = ෍ 2 ு ே ுୀ଴

ுା௅ A. (^) N = 2 B. (^) C. D. − 1 L L = 0 H − 1 ∑

Relating H (height) and N (#nodes)

Level 0 Level 1 Level 2 … … Representing the sum in closed form: N = 2 L L = 0 H − 1 ∑ =^2 H − 1

Relating H (height) and N (#nodes)

Level 0 Level 1 Level 2 … … Finally, what is the height of the tree in terms of N? And since we knew finding a node was O(H), we now know it is:

A. B. C.

N = 2 L L = 0 H − 1 ∑ =^2 H − 1 H = ( N + 1 ) / 2 H = log 2 ( N ) H = log 2 ( N + 1 )