BST coding Flashcards

1
Q

initilaizing BSTNode class:

A

self.key = key;
self.value = value;
self.left = None;
self.right = None;
self.weight = 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

BST: get(self, key):

A

self.key == key: return self;

if self.key > key:
>if self.left: return self.left.get(key);

if self.key < key:
>if self.right: return self.right.get(key);

raise KeyError(“key {} is not in tree”.format(key))

-# recursive binary search

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

BSTNode get keyerror

A

raise KeyError(key: {} is not in tree”.format(key))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

BST: put(self, key, value

A

if self.key == key: self.value = value;
elif self.key < key:

> if self.left: self.left.put(key, value);
else: self.left = BSTNode(key, value);

elif self.key > key:

> if self.right: self.right.put(key, value);
else: self.right = BSTNode(key, value);;

update_weight(self)

-# if <, out on left; if >, put on right; if self.l/r DNE, create one there

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

BST: update_weight(self):

A

left_weight = self.left.weight if self.left else 0;
right_weight = self.right.weight if self.right else 0;

self.weight = left_weight + right_weight + 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

BST runtime

A

generally, O(logn) operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Priority Queue runtime

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Priority queue runtime - reverse sorted

A

O(n) insert;
O(1) findmin()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

upheap runtime

A

O(logn)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

upheap

A

append, swap w parent until order is restored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

downheap

A

swap last element w root;
pop; removes old root;
swap new root with min child until order is restored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

downheap runtime

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly