Binary Trees Flashcards
What are ancestor nodes?
nodes that are between the current node and the root node, including the root node
What is the root node?
the very first node of the tree?
What is a grandparent node?
the parent node of the parent node of the current node
What are leaf nodes?
nodes without any children
What is a binary tree?
a data structure made up of nodes, where each node has at most two children nodes
How do you calculate the depth of a node?
The amount of parent nodes between the current node and the root node, including the current node
What is the height of a tree?
the height of its root node. the amount of its children node layers.
What is a complete binary tree?
every level except possible the last, is completely filled and all the nodes in the last level are as far left as possible
What is a full binary tree?
a tree in which every nodes has either 0 or 2 children
How do we implement a Binary Tree?
We create a class Node. The Node class will have three attributes: value, left, right. We create a class BinaryTree. WIthin the BinaryTree class, we use the Node class to create new nodes in our binary tree
Tree traversal
is the process of visiting each node in the tree data structure exactly once. There are multiple ways to traverse a free: depth-first or breadth-first order.
What are the Three ways to traverse a tree in depth-first oder?
In-order, pre-order, post-order
Explain pre-order depth-first tree traversal
follows the order: Root -> Left -> Right
checks if the current node is empty, displays the value of the current node, recursively calls the pre-order function on the left subtree, then recursively calls the pre-order function of the right subtree
Explain in-order depth-first
similar to pre-order depth-first, except it follows the order of: Left -> Root -> Right
make recursive call to the left child, concat the value of the current node with traversal, make a recursive call to the right subtree
Explain post-order depth-first
make recursive call to the left subtree and the right subtree before concatenating the value of the current node with traversal