Mod 5 Flashcards
In a tree, what is a parent?
A node P in a tree is called the parent of another node C if P has an edge that points directly to C.
In a tree, what is a descendant and what is an ancestor?
The descendants of a node N are all of N’s children, plus its children’s children, plus the children of those nodes, and so forth.
A node A is the ancestor of another node D if D is a descendant of A.
Can a tree have two roots?
The ancestor of all other nodes in the tree is called the root. Each tree has exactly one root.
How is an interior node different from a leaf node?
A node in a tree is an interior node if it has at least one child.
node in a tree is a leaf node (or just a leaf) if it has no children. When implementing a tree, a leaf node conventionally will not reference any child nodes (e.g., in Python these references would be set to None.)
What is a subtree?
A subtree is the portion of a tree that consists of a single node N, all of N’s descendants, and the edges joining these nodes. We call this the subtree rooted at N.
In a tree with n nodes, how many subtrees exist?
n-1. We can make a subtree out of each node except the root (as that would be the same tree).
What is a path and how do we find the path length?
A path is the collection of edges in a tree joining a node to one of its descendants.
The length of a path in a tree is equal to the number of edges in that path.
What is node depth?
The depth of a node N in a tree is the length of the path from the root to N. By definition, the root node has depth 0.
What is tree height?
A tree’s height is the maximum depth of any node in the tree.
How is a full binary tree different from a perfect binary tree?
A full binary tree is a binary tree in which every node has exactly two child nodes or zero child nodes (i.e. a leaf).
A perfect binary tree is a full binary tree (i.e., one where all interior nodes have two children) where all the leaves are at the same depth. Importantly, if we know a perfect binary tree has height h, then we know:
It has 2h leaves.
It has 2h+1 - 1 total nodes.
Hence, a perfect binary tree is different in that it does not have any leaves at a depth less than the height of the tree.
What is a complete binary tree?
a complete binary tree is a binary tree that is perfect except for the deepest level, where all the nodes are all as far left as possible.
What is a binary search tree?
A binary search tree (or BST), then, is a binary tree that satisfies the following property (known as the “binary search tree property”):
Within a binary search tree, the key of each node N is greater than all the keys in N’s left subtree and less than or equal to all the keys in N’s right subtree.
What is the difference between a depth-first and breadth-first traversal?
- Depth-first – A depth-first traversal explores a tree subtree by subtree, visiting all of a node’s descendants before visiting any of its siblings (and their descendants). To put it another way, depth-first traversal moves as far downward in the tree as it can go before moving across in the tree.
- Breadth-first – A breadth-first traversal explores a tree level by level, visiting every node at a given depth in the tree before moving downward and traversing the nodes at the next-deepest level. In other words, a breadth-first traversal moves as far across the tree as it can go before moving down in the tree.
Excluding traversals that go from right to left, name three kinds of depth first traversals?
- Preorder traversal – NLR – process the current node before traversing either of its subtrees
- Inorder traversal – LNR – traverse the current node’s left subtree before processing the node itself, and then traverse the node’s right subtree
- Postorder traversal – LRN – traverse both of the current node’s subtrees (left, then right) before processing the node itself
Describe the steps to find an element in a binary search tree.
Given a query key kq, here’s how it works:
- Examine one node at a time. Keep a pointer to the current node, starting at the root.
- If the current node is None, the key kq doesn’t exist in the tree, and the search has failed. Return failure.
- If the current node’s key is equal to kq, the search has succeeded. Return success.
- Otherwise, if kq is less than the current node’s key, move the current node pointer to point to its left child and repeat.
- Otherwise, kq is greater than the current node’s key, so we move the current node pointer to point to its right child and repeat.
find(bst, kq):
n ← bst.root
while n is not None:
if n.key equals kq:
return success
else if kq < n.key:
n ← n.left
else:
n ← n.right
return failure