Trees Flashcards
Explain binary trees
In general, nodes in trees can have any number of children. However, for binary trees every node has two or fewer children. The number of children each node has in a tree is called the branching factor, and each node has exactly one parent. The branching factor for a binary tree is 2.
What are leaves?
Nodes pointing to no other nodes are called leaves.
What is the depth of a node?
Nodes pointing to no other nodes are called leaves.
What is the node height?
The node height is the number of nodes from between the original node and the deepest leaf that is a descendent.
What is the tree height?
The tree height is the node height of the root, i.e. the number of branches along the longest path in the tree.
What is the largest number of nodes a binary tree can have that has a tree height of 5?
2n - 1 // n being 6 because of including the root node
What is a full binary tree?
A full binary tree is a binary tree in which each node has exactly zero or two children.
What is a complete tree?
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
What is a traversal?
so that you can navigate the tree and find all the data it contains. This is known as a traversal, since it traverses the whole binary tree.
What is the code for a pre-order traversal?
def traverse(tree): if tree: print(tree.getRootVal()) traverse(tree.getLeftChild()) traverse(tree.getRightChild())
What is the code for an in-order traversal?
def traverse(tree): if tree: traverse(tree.getLeftChild()) print(tree.getRootVal()) traverse(tree.getRightChild())
What is the code for a post-order traversal?
def traverse(tree): if tree: traverse(tree.getLeftChild()) traverse(tree.getRightChild()) print(tree.getRootVal())
What is DFS?
DFS, is the strategy that goes down a branch all the way until a leaf is reached, processes said branch, and then moves on to a different branch.
Is the depth-first search always the way to go? For what cases might there be a better solution?
How about if you would like to see how many connections there are between you and another person on Facebook?
BFS, is the counterpart to DFS in tree traversing techniques. It is a search algorithm that traverses down a tree using a queue as its data array, where elements are visited in a FIFO, first-in first-out, mechanism.
How is BFS often used?
This type of search is often used when looking for the “nearest” solution.