Computer Science Notes

Notes From CS Undergrad Courses FSU

This project is maintained by awa03

Trees

Insert (Recursive)

if (root == nullptr) {
        return new TreeNode(key);
    }

    if (key < root->key) {
        root->left = insert(root->left, key);
    } else if (key > root->key) {
        root->right = insert(root->right, key);
    }

    return root;

Delete (Recursive)

if (root == nullptr) {
        return root;
    }

    if (key < root->key) {
        root->left = deleteNode(root->left, key);
    } else if (key > root->key) {
        root->right = deleteNode(root->right, key);
    } else {
        if (root->left == nullptr) {
            TreeNode* temp = root->right;
            delete root;
            return temp;
        } else if (root->right == nullptr) {
            TreeNode* temp = root->left;
            delete root;
            return temp;
        }

        TreeNode* temp = minValueNode(root->right);

        root->key = temp->key;

        root->right = deleteNode(root->right, temp->key);
    }

Find Min (Recursive)

TreeNode* current = node;

    while (current->left != nullptr) {
        current = current->left;
    }

    return current;

A binary tree is a data structure in which each node has at most two children, referred to as the left child and the right child. Every node, except for the leaves, has exactly one parent. The left and right children of a node are distinct, and the difference in the number of nodes between the left and right subtrees of any node is at most one.

A complete binary tree is a special type of binary tree in which all levels are completely filled, except possibly for the last level, which is filled from left to right. In other words, all nodes are as left as possible in each level.

Pre-order Traversal:

  1. Visit the root node.
  2. Traverse the left subtree in pre-order.
  3. Traverse the right subtree in pre-order.

In-order Traversal:

  1. Traverse the left subtree in in-order.
  2. Visit the root node.
  3. Traverse the right subtree in in-order.

Post-order Traversal:

  1. Traverse the left subtree in post-order.
  2. Traverse the right subtree in post-order.
  3. Visit the root node.

Level-order Traversal (Breadth-First Traversal):

  1. Start at the root node.
  2. Traverse each level of the tree from left to right.

Binary Search Tree

Operation Description
Insert Start at the root. Compare the key to be inserted with the key of the current node. If the key is less, move to the left subtree; if greater, move to the right subtree. Repeat this process until reaching a null position, then insert the new node with the given key at this position.
Delete Search for the node to be deleted starting at the root. Identify different cases: if the node has no children (a leaf node), remove it; if the node has one child, replace the node with its child; if the node has two children, find the in-order successor (or predecessor), replace the node's key with the successor's (or predecessor's) key, and then recursively delete the successor (or predecessor).
Search Start at the root. Compare the key to be searched with the key of the current node. If the key is equal to the current node's key, you have found the node. If the key is less, move to the left subtree; if greater, move to the right subtree. Repeat this process until you find the node with the matching key or reach a null position, indicating the key is not in the tree.

More info on binary search trees can be found in [[Trees 2 🌲]]

AVL Tree

An AVL tree is a binary search tree with the balance condition, for every node in the tree, height of left and right subtree can differ at most by 1. An avl tree is maintained by fixing the nodes that violate this condition after each insertion or deletion. Fixing is done through either single or double rotation.

Check from the parent of the newly inserted node upwards to the root to ensure that the AVL tree is not being violated by a newly inserted node.

![[AVLins.png]]

B-tree

A b-tree is a m-ary search tree with the following balance restrictions


Hash Table

Hash table is a vector of lists, conflicts are resolved with the list. Multiple items can be stored in one list.

For more on hash tables view... [[Hash Table]] and [[Hash Table 2]]


Priority Queue

Each job within a computer takes turns using the cpu. If a job comes earlier than another job it needs to be executed earlier. If a job has been scheduled, it should not be scheduled a second time if there exists a job that hasn't been scheduled once. The queue is the answer.

Vector Representation

Heap operations

Percolate Up (Heapify Up):

This operation is typically used during the insertion of a new element into the priority queue.

  1. Insert Element at the Bottom:
    • Insert the new element at the bottom of the heap (last position).
  2. Compare with Parent and Swap:
    • Compare the value of the new element with its parent.
    • If the value of the new element is greater (for a max-heap) or smaller (for a min-heap) than its parent, swap the element with its parent.
    • Repeat this process until the heap order property is restored.

Percolate Down (Heapify Down):

This operation is typically used during extraction or removal of the top element from the priority queue.

  1. Replace Top with Bottom:
    • Replace the top element (the root) with the last element at the bottom of the heap.
  2. Compare with Children and Swap:
    • Compare the value of the new root with its children.
    • If the value is smaller (for a max-heap) or greater (for a min-heap) than at least one of its children, swap the element with the smaller (for a max-heap) or larger (for a min-heap) child.
    • Repeat this process until the heap order property is restored.

Building the heap