Posts

Showing posts from October, 2024

Dynamic Memory Allocation + Pointers

Pointers -  stores address of variable int *a = &b --> a is pointer pointing to address of b --> use *a to access b int &a = b --> a is alias to b --> both a and b have same address => use a to access b arr [ 3 ] = { 5 , 10 , 20 }; *arr --> 5 but *(arr + 1) --> 10 for 2-D array -> *(*arr + 1) --> arr[0][1] and *(*(arr + 1)) --> arr[1][0] Dereferencing - Retrieve data contained at memory location where the pointer points void pointers give great flexibility as can point to any data type. But to dereference => first transform it into some other data type Memory Leak -  When user forgets to deallocate memory for future use m = malloc ( 5 ); m = NULL ;  →  allocate 5-byte memory from heap and store address in pointer m  → but now  program has lost this address bcz no pointer points to it anymore  →  this memory can't be accessed or freed for lifetime of program Dangling Pointer  - When delete pt...

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...