Posts

Showing posts from October, 2024

Dynamic Memory Allocation + Pointers

Pointers Pointer — stores the address of a variable int *a = &b → a is a pointer to the address of b → use *a to access b int &a = b → a is alias (reference) to b → both share the same address → use a to access b arr [ 3 ] = { 5 , 10 , 20 }; // *arr → 5 // *(arr + 1) → 10 // For 2-D array: *(*arr + 1) → arr[0][1] *(*(arr + 1)) → arr[1][0] Dereferencing Retrieve the data stored at the memory location a pointer points to. Void pointers give great flexibility — they can point to any data type. But to dereference → first cast it to a concrete data type. Memory Leak When a program forgets to deallocate memory, making it unavailable for future use. m = malloc ( 5 ); m = NULL ; // Allocates 5 bytes on heap → stores address in pointer m // Sets m = NULL → program loses that address // No pointer refers to that memory anymore // → Cannot be accessed or freed for the lifetime of the program Dangling Pointer d...

DSA Trees + Heap

Image
Trees are special form of graph If n nodes => n-1 edges (means 2 points have only 1 line joining them) They are acyclic => means from node A to node B there is only 1 way to reach Whenever see  stuffs like  connected ,  unique path  -> tree Types of traversal in trees inorder traversion - left -> root -> right preorder traversion - root -> left  -> right postorder traversion - left  -> right -> root If first time visited -> must be root (bcz parent hai)  => PRE-ORDER If second time visited -> left subtree fully analyzed bcz we are moving in anti-clockwise direction => toh left check krke hi aayenge  => IN-ORDER If third time visited -> left and right subtree fully analyzed => POST-ORDER Post-order is reverse of Pre-order from clockwise Binary search tree (BST) = Binary tree + Search property each subtree follows -- left subtree < root value < right subtree Inorder traversal in BST is...