Data Structures - Trees Flashcards

1
Q

What is the definition of a tree?

A

Trees are a type of data structure consisting of a non-empty collection of nodes and edges which may carry properties.

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

What is the definition of a node?

A

A node is an object that has a name - normally with some information associated with it.

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

What is the definition of an edge?

A

An edge is a connection between two nodes.

This can also have information associated with it - for example, in neural networks, edges have their own weight attached to them.

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

What is the definition of a path?

A

A path is a sequence of adjacent nodes connected by edges.

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

What is the definition of a rooted tree?

A

A rooted tree is a tree where one element is designated as the “root” of that tree.

They are the most common form of a tree in computer applications, so much so that sometimes it is worth just referring to them as a “tree” themselves.

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

What is meant by a “forest” of trees?

A

A forest of trees is a disjoint set of trees. This means that two or more separate trees exist within the set.

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

What is meant by the “parent”, “child” and “siblings” of a rooted tree?

A

A node’s “parent” is the node immediately above this node.
A node’s “child” is each adjacent node below this node.
A node’s “siblings” are all other nodes connected to its parent.

Note that nodes with no children are called “leaves”.

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

What is meant by an “M-ary tree”, where M is a positive integer?

A

An M-ary tree is a tree where each node has a maximum number of children equal to M.

For example, a Binary Tree is a special type of M-ary tree where all nodes have at most 2 children. Hence, they are simply 2-ary trees.

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

What is the “level” of a node, and subsequently what is the “height” of the entire tree?

A

The level of a node is one higher than the level of its parent. For example, a child of the node with level 11 would be level 12.

The height of the whole tree is the maximum level of all of its nodes EXACTLY. For example, if a tree consists only of the root at level 0, then the height will also be 0.

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

If a rooted tree consists only of its root, would its height be 0 (exact node level) or 1 (numeric height)?

A

0, as a tree’s height is EXACTLY its maximum level.

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

What is a full binary tree?

A

A full binary tree is one in which all the nodes EXCEPT THE LEAVES have exactly two children.

In other words, they have either two children, or no children.

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

What is meant by a binary tree being “complete”?

A

A complete binary tree is one such that all levels except possibly the last are completely full, and the last level has all of its nodes to the left side.

So, every level of nodes has 0 or 2 children, and for the last level in the tree, all of its nodes are on the left subtree of the root node.

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

How may we implement a binary tree using an array?

A

We start from the topmost number (the root node) with 1, then move down the levels and number the nodes with 2 and 3, starting from the left.

This is an easyway to do it, as the numbering can be used as indexes in an array representation.

For example:
Z F S G J H Q X C
0 1 2 3 4 5 6 7 8 9

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

What is a binary search tree?

A

A BST is a binary tree where the nodes are organised such that…

All elements in the left subtree of a node are LESS than the node, and
all elements in the right subtree of a node are GREATER than the node.

Note that obviously, BSTs do not support repeated elements, as there is no way to classify whether 12 should be considered greater or less than 12.

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

What benefits do Binary Search Trees provide?

A

When they are balanced, BSTs provide blazing fast searches, at speeds of around O(h), where h is the height of the tree.

It is important to note that tree height is normally O(log n) to be randomly generated, meaning that we can search through n elements in logarithmic time.

To put into perspective how big this is, we could search through 1 million elements in under 20 operations!

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

What problems may happen if a Binary Search Tree becomes unbalanced (too many nodes on one side)?

A

Since BST searches operate on O(h), if we have a tree that has a height of 10, but our left subtree has a height of 2, then regardless of if you’re going to the left or right, you will always be searching at a factor of 10.

17
Q

Why is it a problem that most operations in a Binary Search Tree are better understood recursively?

A

Because recursive functions are more taxing than iterative solutions, and iterative solutions are very hard to write for BSTs.

18
Q

What is a balanced Binary Search Tree?

A

A balanced BST is one where no leaf is more than a certain amount farther from the tree than any other.

The measure we use for this is called a “balance factor”, and it’s equal to the absolute difference between the left and right subtree’s height.

19
Q

Why may insertion take O(n) in a balanced binary tree, when it only takes O(log n) in an ordinary binary tree?

A

Every time we add a new node, we need to rebalance the tree to ensure it is still balanced and efficient.

To rebalance the tree, we need to change the position of every node in the tree, which will take somewhere around O(n).

If we include the rebalancing as part of the insertion method, it will take linear time to complete. This downside is solved with AVL trees.

20
Q

What is an AVL tree?

A

An AVL tree is a type of balanced binary tree where the height of the two subtrees of a node differs by at most one.

In other words, the balance factor (difference in subtree height) of all nodes in the tree must be no less than -1 and no greater than 1.

The most important characteristic of an AVL tree is that rebalancing can be achieved using one of four methods, called rotations. Rotations make it so that rebalancing can be completed much faster, keeping all common methods at O(log n).

21
Q

What is meant by a minimum spanning tree?

A

A minimum spanning tree is a collection of edges on a tree such that the sum of the weights of the edges is the smallest possible.

This can also be defined as the lowest cost way to connect all points.

We can find a minimum spanning tree using Prim’s algorithm.