DS Flashcards
Data structure trade offs
What is The maximum number of nodes at level ‘l’ of a binary tree?
2^(l-1).
level or root = 1
What is the maximum number of nodes in a binary tree of height ‘h’?
2^(h) – 1.
height of a tree is maximum number of nodes on root to leaf path.
In a Binary Tree with N nodes, what is the minimum possible height or minimum number of levels?
Log2(N+1)
What is a full binary tree?
A Binary Tree is full if every node has 0 or 2 children.
What is a complete binary tree?
A Binary Tree is complete Binary Tree if all levels are completely filled except possibly the last level and the last level has all keys as left as possible
What is a perfect binary tree?
A Binary tree is Perfect Binary Tree in which all internal nodes have two children and all leaves are at the same level.
What is a balanced binary tree
A binary tree is balanced if the height of the tree is O(Log n)
AVL tree maintains O(Log n) height by making sure that the difference between heights of left and right subtrees is 1. Red-Black trees maintain O(Log n) height by making sure that the number of Black nodes on every root to leaf paths are same and there are no adjacent red nodes.
What is a degenerate tree?
A degenerate or pathological tree is a tree where every internal node has one child.
When DFS vs BFS?
Depth First Traversals are typically recursive and recursive code requires function call overheads.
The most important points is, BFS starts visiting nodes from root while DFS starts visiting nodes from leaves. So if our problem is to search something that is more likely to closer to root, we would prefer BFS. And if the target node is close to a leaf, we would prefer DFS.
When to use trees?
- Storing hierarchical data
- an search for a given key in moderate time (quicker than Linked List and slower than arrays)
- can insert/delete keys in moderate time (quicker than Arrays and slower than Unordered Linked Lists).
- Like Linked Lists and unlike Arrays, Pointer implementation of trees don’t have an upper limit on number of nodes as nodes are linked using pointers.