Properties Of Trees Flashcards
Each tree must have what?
Each tree must have a root node.
Trees cannot be what?
Trees cannot be cyclical.
Do nodes have to be in any particular order for you to have a tree?
No. Nodes may or may not be in any particular order.
What could the value of a node be?
A nodes value could be any data type.
ex: string, int, boolean
Can a nodes value link back to their parent?
Yes but it doesn’t have to.
What would a simple node class contain for a tree?
class Node :name, :children # name could be value # children an array End
What would a simple tree class contain?
class Tree
:root
# has a root node and root can have children nodes
End
What determines if a tree is a binary tree?
- each node must less then or equal to two children nodes.
- if this is not the case you may have an n-ary tree n being the amount of children nodes for example 10-ary
What are the properties of a leaf?
If node.children? == nil
node.leaf? = true.
End
What type of tree is a binary search tree?
A binary search tree is a type of binary tree.
What is unique about a binary search tree that makes it different then just a binary tree?
A binary search tree must fit the following ordering properties:
- Left Descendants <= Current Node < Right D.
- Sometimes not permitted to have duplicate values.
- Sometimes duplicates; but will be on the right; or possibly either side.
A binary search tree imposes what condition?
Nodes.each do |node|
If node.left <= node.current_node && node.current_node < node.right_node
Left descendants <= Current node < right d
What does a balanced tree not mean?
A balanced does not mean that the left and right sub-trees are of equal size. (This is a perfect tree).
*For problems with trees be sure to ask interviewer if it is balanced or not.
What should a “balanced” tree be thought of as?
You could think of a “balanced tree” as a “Not terribly unbalanced tree”.
- It must be balanced enough to meet O(log n) times for insert and find.
What are two types of balanced trees?
- Red-Black-Tree
- AVL Tree
What do you call a Binary Tree in which every level of the tree is fully filled and is filled from left to right?
A Complete Tree
What do you call a tree in which every node has <= 2 children.
Full Binary Tree
What type of tree is both considered a Full Binary Tree and a Complete Binary Tree?
A Perfect Binary Tree is both full and complete!
How could you determine if a binary tree is perfect if you have k number of levels and the number of nodes?
A binary tree must have 2^k-1 nodes for it to be a perfect binary tree. (K being the number of levels or degrees deep the tree is).
What are the 3 types of Binary tree traversals?
- ) In-Order
- ) Pre-Order
- ) Post-Order
What is the most commonly used binary tree traversal?
In-Order
When used on a Binary Search Tree In-Order traversal visits nodes in ascending or descending order?
When performed on a binary search tree an In-Order traversal will visit nodes in Ascending order.
In Pre-Order traversal what order are the nodes visited?
First the current node then the left node and then the right node. (NLR).
For post-order traversal what order are the nodes visited in?
First the current node then the children. (NLR)