trees Flashcards
(42 cards)
What is a graph?
A structure of nodes (vertices) connected by edges that model relationships.
What are the components of a graph?
Vertices (nodes) and edges (connections between nodes).
What is a directed graph?
A graph where edges have direction (arrows from one node to another).
What is an undirected graph?
A graph where edges have no direction; connections are bidirectional.
What is a weighted graph?
A graph where edges carry weights, often representing cost or distance.
What is a path in a graph?
A sequence of connected edges where each edge leads to the next node.
What is a cycle in a graph?
A path that starts and ends at the same node with all edges distinct.
What does it mean for a graph to be connected?
There is a path between every pair of vertices.
What is an acyclic graph?
A graph with no cycles.
What defines a tree in graph theory?
An undirected, connected, and acyclic graph.
How many paths are there between two nodes in a tree?
Exactly one path.
What is a rooted tree?
A tree with one node designated as the root; defines levels in the tree.
What is a level in a tree?
The number of edges from the root to a given node.
What is the main difference between BFS and DFS?
BFS uses a queue (FIFO) to explore level-by-level; DFS uses a stack (LIFO) to go deep first.
Why must you track visited nodes in graph traversal?
To avoid revisiting nodes and prevent infinite loops in cyclic graphs.
Why don’t you need to track visited nodes in trees?
Because trees are acyclic and each node is reached only once.
What is preorder traversal?
Visit root, then traverse left subtree, then right subtree.
What is postorder traversal?
Traverse left subtree, then right subtree, then visit root.
What is inorder traversal?
Traverse left subtree, visit root, then traverse right subtree (binary trees only).
What is level-order traversal?
Traverse nodes level by level using a queue — same as BFS.
Which traversal method uses a queue?
Breadth-first search (BFS) and level-order traversal.
Which traversal method uses a stack or recursion?
Depth-first search (DFS), preorder, postorder, and inorder traversals.
Why might a hash table not be suitable for range queries?
Because hash tables do not maintain any ordering of keys.
What kind of data structure allows efficient key-based lookup and range queries?
A binary search tree (BST).