Binary Trees Flashcards

1
Q

What is a tree?

A

A tree is a data structure consisting of nodes.

  • Each tree has a root node
  • Each root node has zero or more
  • Each child node has zero or more nodes and so on.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Balanced vs Unbalanced

A

Does not necessarily mean that the tree has the same exact number of leaves. It just needs to be balanced enough to get O(logN) for insert and find.

Two common types of balanced trees are AVL and red/black trees.

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

Complete Binary Tree

A

Where every level is filled except the very last level. To the extent the last level is filled, its filled left to right.

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

Full Binary Tree

A

When every node has two or zero children.

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

Perfect Binary Trees

A

One that is full and complete. All leaves are the same level and have the maximum number of nodes.

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

Traversal

A

In-order:
“Visiting” left, current, then right branch.

Pre-order:
“Visits” current before the children.

Post-order:
“Visits”

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

Binary Heaps (Min/Max Heaps)

A

A min heap is a complete binary tree where each node is smaller than its children. It is totally filled other than the rightmost elements on the last level. The root is the minimum element of the tree.

There are two functions: insert and extract_min.
Insert:
We start by inserting the element at the bottom. We insert at the right most spot as to maintain “complete tree” property.
We fix the tree by swapping the parent with the child…essentially bubbling up the min value.

Extract_min:
Remove min is easy because its always at the top. We remove the element by swapping it with the last element in the heap. When choosing which child to use in any situation, choose the one with the lesser value.

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

Trie (Prefix trees)

A

A variant of an n-ary tree in which characters are stored at each node. Each path down the tree may represent a word.

  • (aka null nodes) represent complete words. the implementation might be a special type of child (like TerminatingTrieNode which inherits from Trie Node). We could just use a boolean flag instead.

A trie could have anywhere from 1 thru ALPHABET_SIZE + 1 children.

There are used to store the entire English language for quick prefix lookups. A hash cant even tell is a string is prefix of any valid words. Tries can. O(lenOfStr) time.

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