Data Structures Flashcards
trees
- data structures that consist of nodes which are linked together in a certain way
- Nodes in a tree have a parent-child relationship
- Each node is linked to 0 or more child nodes and at most 1 parent
root
- special node at the top of the tree from which all other nodes descend.
- The root has no parent
leaf
Nodes without any children
binary tree
each node can only have 0, 1, or 2 children (at most 2 children)
branch
- signifies a decision path, a choice that connects 1 node to another
- binary tree may have a left branch and a right branch
subtree
mini tree within a binary tree, whose root can be any node and all of its descendants rooted at that node.
Binary search trees(BST)
- All of the nodes in the left-hand branch of a node are guaranteed to have lower values than the node itself, and
- all of the nodes in the right-hand branch of a node are guaranteed to have a higher value than the node itself.
- each node holds a key, a value, and left and right pointers.
balanced tree
- nodes are inserted equally on the left and right branches
- the width grows exponentially with the number of nodes. This means that conversely, the height must grow logarithmically with the number of nodes. So the average case is O(log(n)).
- a skewed binary search tree is basically a linked list. Therefore, you will need to iterate through each of those nodes in order to get to the bottom of the tree to insert something. This makes the time complexity O(n).
- example of the best case would be inserting the root only, which would be O(1).
retrieval
follows the same pattern as insertion. You check the value of the key against the key stored in a node in the BST and recursively follow the left or right branch accordingly.
retrieval time complexity
- average time complexity of finding something in a BST would require traversing the height of a balanced tree and would be O(log(n)).
- Just like insert, the worst case for finding something in a BST will occur when the tree is skewed left or right and you are searching for the node at the bottom where everything is inserted to 1 side, so it is O(n).
- The best case is that the node you are trying to find is the root node, which would be O(1).
removal
- After you find the item that you want to remove using the find process described above, there are 3 scenarios that you may encounter.
1. no children (a leaf node)
2. 1 child (left or right, doesn’t matter)
3. 2 children (left and right)
removal-with no children
-After you find the node, and that node has no children, you remove it from the BST by breaking the link to the parent
removal-with 1 child
- you make the parent of the item you are removing point to this 1 child, and then detach the item that you want to remove from the parent. We will make 21 the right child of 19 and detach 28 from being the right child of 19.
- the child of the node that you removed (21) has a new parent (19), the parent of the node you just removed.
removal-with 2 children
- find the minimum value in the right subtree
- replace the value of the node to be removed with the found minimum - now, the right subtree contains a duplicate
- apply remove to the right subtree to remove the duplicate
removal time complexity
you can use similar logic to insertion and retrieval to show that the best case is O(1), the average case is O(log(n)), and the worst case is O(n)