cs126 binary search trees

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/13

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

14 Terms

1
New cards

What is an ordered map?

A map where keys satisfy the properties of a total order.

2
New cards

What is a binary search tree?

  • A proper binary tree that stores keys / key-value pairs as its internal nodes

  • Each node in a left subtree of a node is smaller than it

  • Each node in a right subtree of a node is larger than it

  • External nodes do not store items

3
New cards

Name the (3) fundamental methods of a binary search tree.

  • get(k)

  • put(k,v)

  • remove(k)

4
New cards

What does the binary search tree operation get(k) do?

Returns the value v associated with key k, or return null.

5
New cards

What does the binary search tree operation put(k,v) do?

Replaces the value associated with k, or inserts a new entry if there is none.

6
New cards

What does the binary search tree operation remove(k) do?

Remove and returns entry with key k, or return null.

7
New cards

What is the worst-case time cost associated with searching for an item in a BST?

O(logn)

8
New cards

What is the worst-case time cost associated with removing / inserting an item in a BST?

O(n) for both

9
New cards

How is a search performed on a BST?

  • Inspect a node

  • If the node is the item then its found

  • If the node is a leaf, the item is not in the tree

  • If the key at the node  is greater than the item, inspect the right subtree

  • Otherwise inspect the left subtree

10
New cards

How is an insertion performed on a BST?

  • Perform a search for the key

  • When a leaf is reached, insert the key at the node, and expand the node into an internal node

11
New cards

How is a deletion performed on a BST if the node has no children?

Delete the node.

12
New cards

How is a deletion performed on a BST if the node has one child?

Make the child of the node, the child of the parent, then delete the node.

13
New cards

How is a deletion performed on a BST if the node has two children?

Replace the value by the max (rightmost) value of the left subtree and delete() the max node of the left subtree.

14
New cards

Describe the performance of BST.

  • If a tree has n items and a height of h

    • Space used is O(n)

    • get(), put(), remove() all take O(h) time

  • In the worst case, h is O(n)

  • In the best case, h is O(logn)