Show 25 50 100
PDF
3 result(s) in Data Structure with 5+ ★ Clear

Index

Showing 3 topics
  1. Linked List
  2. Binary Tree and Traversals
  3. Binary Search Tree (BST)
1 Index

A linked list is a chain of nodes; each node holds data and a pointer to the next node. Size can grow at run time and insert/delete at the head is O(1), but there is no random access.

head → 10 → 20 → 30 → 40 → NULL

head → 10 → 20 → 30 → 40 → NULL

C++ 27 lines
struct Node {
    int data;
    Node *next;
    Node(int d) : data(d), next(nullptr) {}
};

void insertHead(Node *&head, int x) {
    Node *n = new Node(x);
    n->next = head;
    head = n;
}

void print(Node *head) {
    for (Node *cur = head; cur; cur = cur->next)
        cout << cur->data << " ";
}

Node* reverse(Node *head) {          // very common interview question
    Node *prev = nullptr;
    while (head) {
        Node *next = head->next;
        head->next = prev;
        prev = head;
        head = next;
    }
    return prev;
}
  • Singly — next pointer only.
  • Doubly — next and prev pointers.
  • Circular — last node points back to the first.

Q & A

  1. Array vs linked list?
    Array: contiguous, O(1) index access, fixed size, costly insert/delete. Linked list: scattered nodes, O(n) access, dynamic size, O(1) insert/delete when you already have the node.
  2. How to find the middle of a linked list?
    Slow and fast pointers: slow moves 1 step, fast moves 2 steps. When fast reaches the end, slow is at the middle.
  3. How to detect a cycle?
    Floyd's algorithm: the same slow/fast pointers meet inside the loop if there is a cycle.
2 Index

A binary tree is a tree where each node has at most two children. Terms: root, leaf, parent, child, height, depth, level.

  • Full — every node has 0 or 2 children.
  • Complete — all levels full except possibly the last, filled from the left.
  • Perfect — all internal nodes have 2 children and all leaves are at the same level (2h+1 − 1 nodes).
  • Balanced — height is O(log n), e.g. AVL, Red-Black tree.
C++ 22 lines
struct Node {
    int val;
    Node *left, *right;
    Node(int v) : val(v), left(nullptr), right(nullptr) {}
};

void inorder(Node *r)   { if (!r) return; inorder(r->left); cout << r->val << " "; inorder(r->right); }   // L N R
void preorder(Node *r)  { if (!r) return; cout << r->val << " "; preorder(r->left); preorder(r->right); } // N L R
void postorder(Node *r) { if (!r) return; postorder(r->left); postorder(r->right); cout << r->val << " "; } // L R N

int height(Node *r) { return r ? 1 + max(height(r->left), height(r->right)) : 0; }

void levelOrder(Node *root) {          // BFS
    queue<Node*> q;
    if (root) q.push(root);
    while (!q.empty()) {
        Node *cur = q.front(); q.pop();
        cout << cur->val << " ";
        if (cur->left) q.push(cur->left);
        if (cur->right) q.push(cur->right);
    }
}
3 Index

In a BST every left subtree value is smaller and every right subtree value is larger than the node. Inorder traversal gives sorted output. Search/insert/delete are O(log n) when balanced, O(n) when skewed.

A balanced BST with 7 nodes

A balanced BST with 7 nodes

C++ 14 lines
Node* insert(Node *root, int x) {
    if (!root) return new Node(x);
    if (x < root->val) root->left = insert(root->left, x);
    else               root->right = insert(root->right, x);
    return root;
}

bool search(Node *root, int x) {
    while (root) {
        if (x == root->val) return true;
        root = (x < root->val) ? root->left : root->right;
    }
    return false;
}

Delete: leaf → remove; one child → replace with the child; two children → replace with the inorder successor (smallest in the right subtree), then delete that successor.