Trees Flashcards
What is a ‘Tree’ and what is the main benefit of storing data in a tree?
Trees: A hierarchical/ nested structure.
Storing data in trees allows us to speed up O(N) algorithms to O(lgN). This is because if the tree is fully balanced we can eliminate half of it.
Describe the Tree ADT and its basic operations.
Elements of a Tree:
r: root node
Non-Empty subtrees (T1, T2, …, Tn) each connected by a ‘directed edge’ from r.
Operations:
get(), size(), set(), add(), remove(), find()
Tree Terminology:
Root: The top of the tree.
Internal Nodes: Nodes that have both a parent and a child.
Child: Bottom nodes, that are only connected to a parent.
Siblings: Nodes that share a common parent.
Define the ‘Depth’ of a tree.
The total number of edges from the root node to the leaf node in the longest path.
Root Node Height : 0
What is a ‘Binary Tree’?
A k-array (k=2) data structure where every node has at most two children.
What is the typical runtime of a binary tree?
lg N
What is a ‘Full’ binary tree?
A tree in which every node is a leaf or has two children
What is a ‘Complete’ binary tree?
When all levels (except the last) are completely filled and every node is as far left as possible.
What is the ‘Diameter’ of a binary tree?
The number of nodes on the longest path between any two nodes.
What is the ‘Width’ of a binary tree?
The number of nodes on the level w/ the most node.
What are the three most common types of tree traversals?
Preorder: Root L R
Inorder: L Root R
Postorder: L R Root
What is a ‘Binary Search Tree” (BST) and what is its main advantage?
A binary tree where for each node on the tree:
- All Nodes in the left subtree contain nodes that are < than the root.
- All Nodes in the right subtree contain nodes that are > than the root.
Its main advantage is that it can be used as a tree map
Describe the BST data type, its purpose, and its operations.
Its keys are unique, but its values are not.
Goal: Reduce finding an item to O(ln N)
Operations: insert(x), contains(x), findMin(), findMax(), remove(x).
What is an ‘AVL Tree’?
A BST in which the following balance condition holds after each operation.
For all nodes, the height of the left subtree and the right subtree differs by at most 1.
What is the height of an AVL Tree?
At most 0(lnN).