Trees Flashcards
What is a binary tree?
A tree whose elements have at most 2 children (left and right child)
How are binary trees stored in memory?
Each node has a pointer to parent (or NULL/itself if root), pointer to left/right child (or NULL/itself if there isn’t one), and a value representing the item stored in the node
How do you search a binary tree?
Start at the root and search the tree level-by-level (top to bottom, left to right)
This has time complexity O(n)
What is a binary search tree?
A binary tree where for every node (with value v), all elements in the left sub-tree are smaller than v and all elements in the right sub-tree are bigger than v
Preorder vs inorder vs postorder tree traversal
Preorder = print(root), preorder(left), preorder(right)
Starts with root, goes all the way left, ends with rightmost
Inorder = preorder(left), print(root), preorder(right)
Starts with leftmost, ends with rightmost, items in sorted order
Postorder = preorder(left), preorder(right), print(root)
Starts with leftmost, ends with root
Inserting into a BST
Call the .insert() function on the root
This function:
Compares the new value to the root
If it’s less than the root, recurses into the left subtree
If it’s greater than the root, recurses into the right subtree
If the left/right subtree doesn’t exist, it appends the new value there
Searching a BST
Call the .lookup(n) function on the root
If this is a match, return it and its parent
If our target is smaller, recurse into the left sub-tree, otherwise recurse into the right sub-tree
Deleting from a BST
Call root.delete(n)
No children: simply remove n
One child: Transfer child’s value to n and then delete child
Two children: Find the smallest node v that’s bigger than n, copy v’s data into n, and then delete v
Time complexity of searching, inserting and deleting
Average: O(h) = O(log(n))
Worst: O(n)
Finding the smallest element in a BST
Always go left
Finding the smallest element bigger than a given node in a BST
Start at that node, go right once and then always go left. Stop when you hit NULL.
What is an AVL tree?
One where a given node’s subtrees do not differ in height by more than 1
Tree.LeftRotate(x)
Remove x.right.left and store for later
Replace x with x.right
The new x.left should now be empty
x.left = old x, left: old x.left, right: old x.right.left
What is trinode restructuring
A combination of rotations, applied to a parent, child, and grandchild (z, y, x) depending on whether the child and grandchild are on the same side
Trinode restructuring: single rotation (child and grandchild on same side)
Assuming both right:
Tree.LeftRotate(z)
Trinode restructuring: double rotation (child and grandchild on opposite sides)
Assuming right left:
Tree.RightRotate(y)
Tree.LeftRotate(z)
The middle one out of x,y,z always ends up as the root of the subtree
Height of an AVL tree storing n keys
O(log(n))
How to perform post-insertion fix-up
From the newly-inserted node p, find the lowest node which is unbalanced. If one exists, name it z and do trinode restructuring on z, y, and x.
You can stop when you reach a node whose height didn’t change
How to perform post-deletion fix-up
Let p be the parent of the deleted node
From p, start searching for z (the first unbalanced node)
Let y be the taller child of z
Let x be the taller child of y (or if the children have the same height, on the same side as y)
Keep going up the tree, restructuring when you meet an unbalanced node
You can stop when you reach a node whose height didn’t change, or when restructuring results in the same height as before insertion/deletion
AVL tree performance
Uses O(n) space to store n items
Restructuring = O(1)
Searching = O(log(n))
Insertion and removal = O(log(n)) for find + O(log(n)) for restructuring = O(log(n)) overall
What is a priority queue
Data structure where each element is a tuple (key-value pair) and the key is an integer representing the priority of that item
Priority queue operations
P.add(k, v): Insert an item with key k and value v
P.min(): Return the tuple (k, v) with the lowest priority
P.remove_min(): Remove P.min() from the queue and return it
P.is_empty()
len(P)
What is a heap
A data structure like a tree, except they are stored in a flat array
The tree has to complete except for the lowest level
HeapSize(A) = number of elements in heap stored in A
Height of a heap = floor(log(len(A)))
Max-heap vs Min-heap
Max-heap: Every node’s value is less than than or equal to its parent
Min-heap: Every node’s value is greater than or equal to its parent