Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Module Summary: Trees

Binary Search Trees


When searching a binary search tree we start from the root node and traverse the tree until we find the
key we are looking for, otherwise return the last found element.

When inserting we first use search to return a node. We then look if the key to be inserted is already at
the node, if it is smaller than the node or bigger than the node.

Deleting a node is more complicated. If the node has no child or only one child then the node is replaced
with that child or the BST is balanced. If there are two children we find the immediate successor of the
deleted node down the line and replace the deleted node with the successor.

To find out how long IDS takes we need to figure out the complexity of the search. Search requires
travelling down the tree making left or right decisions, the number of decision is equal to the height of
the tree which is log(n).

As BST time complexity is dictated on length, if you have long unbalanced trees the time complexity is
much longer. To fix this we look at the self-balancing BST. This requires rotating the tree at points so
each side is even or as close to even as possible.

AVL Trees
Is any BST that satisfies the Height-Balance Property, which is that for every node p in a tree T, the
heights of the children of p differ by at most 1 or at least -1, where the height of a node is the longest
path from itself to the root node.

To balance the trees we use rotations or Yoinking, which have 4 cases. Case 1 is the right rotation, case 2
is left rotation, case 3 is left-right rotation, where we first do a left rotation on the child node and then a
right rotation on the parent node, and case 4 the right-left rotation which is the inverse of case 3.

Red-Black Trees
An RB-Tree is a BST that balances itself through an internal mechanism, meaning there is no need to do
rotations yourself. Each node in the tree is labelled either black or red, it maintains balance by making
sure there aren’t more red nodes than black nodes.

You might also like