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