note-Tree
Tree
- Complete binary tree:
Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2^h nodes inclusive at the last level h.
Specific ordering property:
all left descendants <= n < all right descendants. Must be true for all node n.
Make sure to check whether if there’s anyduplicate
value. One definition isno duplication
, the other is duplication will be onright
oreither
side.Complete Binary Tree:
Every level is filled except for perhaps last level. To the extent that the last level is filled, it is filled left to right.Full Binary Tree
A full binary tree is every node has either zero or two children. That is, no nodes have only one children.Perfect Binary Tree
Both full and complete. All leaf nodes are on same level, and this level have maximum nodes. Have exactly2^h -1
nodes, h is number of levels(height).Trie(Prefix Trees)
Each path down the tree would represent a word. Normally, have a* node
(sometimes callednull node
)
Questions to ask for clarify
What type of tree?
- binary tree, binary search tree.
- balanced or unbalanced tree.
balanced means it ensures $O(logn)$ time toinsert
andfind
. - full binary tree: each node have two or zero children.
- complete binary tree: only right bottom part is missing.
empty tree
duplicate node. If it’s a BST, does duplicate node goes left or right?
skewed tree