13-15 Flashcards

1
Q

What is a BST and what are some key functions?

A

BSTs are pointer based data structures that lend themselves to storing fluctuating data, it is either the empty tree or a root node containing a key field and datat fiels, a left and a right subtree. They can be efficient to search as long as they are balanced, hence a self-balancing feature is useful.
A BST should support the operations of insert, search, and delete, but also many other useful operations. E.g BST to sort data, priority queue, in-order successor/processor of a key, traversal of the data in various orders.

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

How does deletion in a BST work?

A

For BST deletion a recursive algorithm is used. If the deleted node has no children or one child this is easy as we can just delete it and move the subtree up. We need to change the links when we do this.
If there are two children we go either right or left and then the opposite direction until we hit the first root with no children in the second direction. (e.g smallest element on the right, or largest on the left.

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

What is the in-order predecessor in a BST

A

If it exists, the in-order predecessor will usually be in the left subtree of a root x, and will have the maximum key value of that subtree. If this subtree is empty however it will be the first node in which x is in the right subtree.

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

What are some examples of self balancing BSTs?

A

RBTs, AVL, splay, tries.

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

What properties does a RBT have?

A

A RBT has the following properties :
1. Every node is either red or black
2. The root is black.
3. All dummy leaves are black (null subtrees).
4. 4.If a node is red, the roots of both its subtrees are black.
5. For each node all paths from the node to the leaves contains the same number of black nodes (includes the dummy leaves).
As long as these properties are maintained the tree is typically balanced.

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

What is the black height of a node?

A

The black height of a node is the number of black nodes on the path of that node to the leaf, not including the node, but including the dummy leaves.

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

What is the maximum number of nodes in an RBT?

A

The maximum number of nodes in a red black tree is greater than or equal to (2^k) -1, where k is the maximum black height.

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

What can we do if the red black nature of a tree is ruined?

A

If adding an item to the red black tree ruins the red black nature we can do a rotation, this involves moving a root so that it keeps its left subtree but its right rubtree becomes the left subtree of a neighbouring subtree, this neighbouring subtree keeps its right subtree and the parents of the two nodes changes. (This could be the opposite way around).

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

When are Hash tables useful? When are they not?

A

If maximum size of the table is known beforehand, and if emphasis is on insertion and retrieval.
If we need to do lots of deletions/traversals, or dynamic storage allocation then pointer based structures are better.

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

what are inorder, preorder and postorder traversals?

A

Inorder: left, root, right

preorder: root, left , right
postorder: left, right, root

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

What is sorting using a BST similar to?

A

quicksort. Once built we can use in-order traversal to print the data in sorted order.

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