- If the node has no children: -1
- If the node is a parent: 0
- Height of a random node is the max height (left subtree , right subtree) + 1
- Balance is (the height of the left subtree – height of the right subtree) is smaller or equal to 1
If the equation of the balance is positive: left-heavy. If it is negative: right-heavy.
If the tree is left-heavy:
- Right rotation
- Left-right rotation
If the tree is right-heavy:
- Left rotation
- Right-left rotation
Trees important points
- A node without children is a leaf (external node)
- Each node with children is an internal node
- The depth of a node is the length of the path from that node to the root
- The height of a node is the length of the longest path from that node to a leaf
A Binary tree: each node has AT MOST 2 children
A full Binary tree: all internal nodes have 2 children OR 0 children. The following holds if the tree is a
binary tree: the amount of leaves = the amount of internal nodes + 1
Running time of 3 traversal algorithms: Big Theta of n. Each node will be visited exactly 2 times ( in
preorder and inorder) and three times in postorder. Total number of inorder and preorder is 2n and
postorder 3n Theta(n). Each visit is Theta(1)
AVL trees
Height-Balanced property: for every internal node v, the heights of its children differ at most 1
The height of an AVL tree storing n keys = O(log(n))
Running time of search operation assuming the size of the input tree is n? Big O of log(n)
Running time of insertion operation assuming the size of the input tree is n? Big O of log(n)
Running time of deletion operation assuming the size of the input tree is n? Big O of log(n)
Hash tables
In general: hash tables are able to perform every dictionary operation in O(1) time, in average
(expectation)
,Preventing collisions by chaining
Worst case search time could be Theta(n), because than it is just a linked list
Simple Uniform Hashing: the hash function maps a key to a uniform choice each of the slots is
equally likely to be hashed to
For a set of n keys and slots we have: the expected (average) length of each chain is n/m (load
factor)
If Simple Uniform Hashing is applied: each dictionary operation will take O(1 + load factor)
Open addressing
In a has table with load factor alpha in which collisions are resolved by open-addressing, the
expected runtime of an unsuccessful search is theta (1/(1-alpha)), under the assumption of simple
uniform hashing.
In a hash table with load factor alpha in which collisions are resolved by open-addressing, the
expected runtime of a successful search is theta ((1-alpha) * log(1/1-alpha)), under the assumption
of simple uniform hashing.
If you want to delete an item in hashtable, write a special character once the deletion has been
performed. If you search for new items to delete, the algorithm to delete an item will assume “it is
full” and will continue searching for the to-delete item.
What is the space complexity of a hash table of size m storing n items, in which collisions are
resolved using chaining?
Theta(n+m)
What is the space complexity of a hash table of size m storing n items, in which collisions are
resolved using open-addressing?
Theta(m)
Suppose we wat to store a set S of n keys in a hash table T with size m, with m = n + 2, and a hash
function that behaves uniformly. Should we resolve collisions with chaining or with open-
addressing?
, Hash-table using open addressing from hashtable with m slots to 2m slots is expanding. (If the
amount of elements cannot suit the amount of slots). From 2m slots to m slots is shrinking.
Lecture 6: AVL trees and Hash Tables
AVL trees
Data structure that is an efficient data structure for implementing dictionary ADT. AVL trees are able
to perform every dictionary operation in O(log(n)) worst-case time.
Ideally we wish to have BSTs that have always a small height, no matter how the sequence of
insertion/deletion is. This would result in having fast dictionary data structures! AVL trees are BSTs
that have always a small height.
Height-Balanced Property: For every internal node v of T, the heights of children of v differ by at
most 1. Any binary search tree T that satisfies the height-balanced property is called an AVL tree.
The promise is that the heights of siblings don't differ by more than 11. In other words,
the promise is that for every node pp, the heights of the children of pp differ by at
most 11 from each other.
The height of an AVL tree storing n keys is O(logn)
If you perform an insertion, this might violate the height-balance property of T. If it violates, how to
“repair”?
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller gideonrouwendaal. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $11.24. You're not tied to anything after your purchase.