# Trees and Graphs Flashcards

1
Q

Trees

A

Data structures with a root node and children nodes. Each child may also have children. The tree does not contain cycles.

2
Q

Binary Trees

A

A tree in which each node has up to two children

3
Q

Leaf Node

A

A node with no children.

4
Q

Binary Search Tree

A

A binary tree in which every node fits a specific ordering property: left descendants <= n < all right descendants. This must be true for each node n and all of node n’s descendants, not just immediate children. Some binary search trees cannot have duplicates, others will have duplicates on the right, or either side.

5
Q

Balanced Tree

A

Balanced enough to ensure O(log n) for insert and find, but not necessarily perfectly balanced.

6
Q

Complete Binary Tree

A

A binary tree in which every level of the tree is completely filled, except for maybe the last level. Filling on the last level is from left to right.

7
Q

Full Binary Tree

A

A binary tree in which every node has either zero or two children. No nodes have only one child.

8
Q

Perfect Binary Tree

A

All Interior node have two children and all leaf nodes are at the same level.

9
Q

Number of Nodes in a Perfect Binary Tree

A

The first level has 20, the second level has 21, the ith level has 2(i-1) and the last level has 2(N-1) where N is the depth of the tree. Total: 2**N - 1.

10
Q

In-Order Binary Tree Traversal

A

Visit the left branch, then the current node, and finally, the right branch. When performed on a binary search tree, it visits the nodes in ascending order.

11
Q

Implement In-Order Binary Tree Traversal

A

If node is not null, traverse the left child, then visit the center node, and then traverse the right child.

12
Q

Pre-Order Binary Tree Traversal

A

Visits the current node before its child nodes. The root is always the first node visited.

13
Q

Implement Pre-Order Binary Tree Traversal

A

if node is not null, visit the current node, traverse the left child node, then traverse the right child node.

14
Q

Post-Order Tree Traversal

A

Visits the current node after its child nodes. The root is always the last node visited.

15
Q

Implement Post-Order Tree Traversal

A

if node is not null, traverse the left child node, then traverse the right child node, then visit the current node.

16
Q

Min-Heaps

A

Complete binary tree where each node is smaller than its children. The root is the minimum element of the tree. Two key operators exist: insert and extract_min

17
Q

Max-Heaps

A

Same as a min-heap, but elements are in descending order.

18
Q

insert

A

start by inserting the element at the bottom of the min-heap. Insert at the next available spot, so as to maintain the complete tree property. Then fix tree by swapping the new element with its parent, until we find an appropriate spot for the element. Bubble up mini element. This takes O(log n).

19
Q

Extract Minimum Element (Min Heap, Binary Search Tree)

A

Remove the min element and swap it with the last element in the heap. Then, bubble down element, swapping it with its smaller child until the min-heap property is restored. The algorithm will take O(log n) time.

20
Q

Tries (Prefix Trees)

A

Variant of an n-ary tree in which characters are stored at each node. Null or * words indicate the end (like the end of a word). Very commonly used to store the entire language for quick prefix lookups.

21
Q

Graphs

A

A collection of nodes connected by edges. Graphs can either be directed or undirected. Graphs may contain more than one disconnected sub-graphs.

22
Q

Connected Graph

A

A path exists between every pair of vertices of a graph.

23
Q

Acyclic Graph

A

Contains no cycles.

24
Q

A

Every vertex or nodes stores a list of adjacent vertices. An undirected node will store each edge twice.

25
Q

A

One Graph Class with field nodes. One Node class with field children and field data.

26
Q

A

An NxN boolean matrix where N is the number of nodes in the graph. A true value at matrix[i][j] means edge from node i to node j. Undirected graphs have symmetric matrices.

27
Q

Depth-First Search

A

Start at root node or arbitrarily selected node, and explore each branch completely. DFS is preferred if we want to visit every node in the graph. Tree traversal algorithms are simpler forms of DFS, but w/o markers.

28
Q

A

Start at root node or arbitrarily selected node, and explore each neighbor before moving onto any of their children. BFS is preferred if we want to find the shortest path between two nodes.

29
Q

Implement DFS

A

Recursive function searching a root node. If root node is null, return. Visit root, then mark it as visited. For each neighboring node, if node is not visited, search it recursively.

30
Q

Implement BFS

A

Use a method that searches the root node using a queue. Set root to marked. Enqueue the root. While queue is not empty, dequeue a node, visit the node. For all adjacent nodes, if the adjacent node is not marked, set it to mark, and enqueue the adjacent node.

31
Q

Bidirectional Search

A

Finds shortest path between a source and destination node. Two BFS run simultaneously from each node.

32
Q

Run time of BS vs. BFS

A

BS will have O(kd/2) while BFS will have O(kd) if we assume each node has at most k connections. This is because searching each level will require k*number of nodes we are searching. Previous levels will have at most k nodes.