Trees Flashcards
What is a Tree?
A tree is collection of nodes that have a parent-child relationship
Tree
What are sibling nodes?
Sibling nodes are nodes that have the same parent
What is an external (leaf) node?
An external node is a node with 0 children
What is an internal node?
An internal node is a node that has 1 or more children
True or False
An ancestor node of n
is any node on the path from n
to the root node
True
True or false
A descendent node of n
is any node on the path from n
to a leaf node
True
What is an edge?
An edge is a pair of nodes such that one is a parent of the other. In different words, an edge is a link between 2 immediate nodes
What indicates if a tree is sorted?
If there is a meaningful linear order among the nodes at each level, the tree is sorted
- How can you calculate the depth of node
n
? - What is the depth of a tree’s root node?
- The depth of node
n
is the number of edges fromn
to the root node - Depth of root node = 0
Binary Search Tree
What happens when you insert sorted data into a Binary Search Tree?
Inserting sorted data will casue a Binary Search Tree to become linear. For example, inserting 1, 2, 3 will cause the tree to look like:
1 \ 2 \ 3
True or false
For N
nodes in an unbalanced tree, there are approximately log(N) levels
False
Describe how a Binary Search Tree works
A Binary Search Tree is a sorted tree where each parent has at most 2 children. The left child is less than the parent node and the right child is greater than the parent node
- How can you calculate the height of node
n
? - What is the height of a tree?
- The height of node
n
is the number of edges fromn
to its deepest node - The height of a tree is the number of edges from the root node to its deepest node
How can you calculate the number of edges in a tree?
Number of nodes - 1 = total edges
True or False?
2^level = number of nodes at that level
True. For example:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8