Skip to content

Commit bd8ce1f

Browse files
authored
Update data_structures.md
1 parent ada33d5 commit bd8ce1f

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

notes/data_structures.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,16 @@ A _heap_ is a specialized tree-based data structure that satisfies the heap prop
235235
Heaps are always complete binary trees, which means they are filled at all levels, except the last, which is filled from left to right. This structure ensures that the heap remains balanced.
236236

237237
```
238-
[100]
239-
/ \
240-
[19] [36]
241-
/ \ / \
242-
[17] [3] [25] [1]
238+
[100]
239+
├── [19]
240+
│ ├── [17]
241+
│ └── [3]
242+
└── [36]
243+
├── [25]
244+
└── [1]
243245
```
244246

245-
#### Key Methods of the Heap Interface
247+
#### Methods of the Heap Interface
246248

247249
The basic operations of a heap are as follows:
248250

@@ -273,11 +275,10 @@ These time complexities make heaps especially useful in situations where we need
273275

274276
Heaps can be represented efficiently using a one-dimensional array, which allows for easy calculation of the parent, left, and right child positions. The standard mapping from the heap to this array representation is as follows:
275277

276-
1. The root of the tree is placed at index 1.
277-
2. For an element at index `i` in the array:
278-
2.1. The left child is placed at index `2*i`.
279-
2.2. The right child is placed at index `2*i + 1`.
280-
3. The parent of an element at index `i` is placed at index `floor(i/2)`.
278+
- In a *heap*, the root of the tree is stored at index `1` of the array representation.
279+
- The *left child* of any node at index `i` is positioned at index `2*i` in the array.
280+
- The *right child* of a node located at index `i` can be found at index `2*i + 1`.
281+
- To find the *parent* of any node at index `i`, calculate the index as `floor(i/2)`.
281282

282283
This layout keeps the parent-child relationships consistent and makes navigation within the heap straightforward and efficient.
283284

@@ -289,11 +290,12 @@ This layout keeps the parent-child relationships consistent and makes navigation
289290
A Binary Search Tree (BST) is a type of binary tree where each node has up to two children and maintains a specific ordering property among its values. For every node, the values of all nodes in its left subtree are less than its own value, and the values of all nodes in its right subtree are greater than its own value. In a well-balanced BST, this property enables efficient lookup, addition, and deletion operations, ideally distributing the nodes evenly across the left and right subtrees. Note that BSTs typically disallow duplicate values.
290291

291292
```
292-
[1]
293-
/ \
294-
[2] [3]
295-
/ \ \
296-
[4] [5] [6]
293+
#
294+
[1]
295+
/ \
296+
[2] [3]
297+
/ \ \
298+
[4] [5] [6]
297299
```
298300

299301
#### Key Operations of the BST Interface
@@ -331,6 +333,7 @@ Insertion, deletion, and search operations are often implemented recursively to
331333
Named after its inventors, G.M. Adelson-Velsky and E.M. Landis, AVL trees are a subtype of binary search trees (BSTs) that self-balance. Unlike regular BSTs, AVL trees maintain a stringent balance by ensuring the height difference between the left and right subtrees of any node is at most one. This tight control on balance guarantees optimal performance for lookups, insertions, and deletions, making AVL trees highly efficient.
332334

333335
```
336+
#
334337
[20]
335338
/ \
336339
[10] [30]
@@ -392,10 +395,11 @@ These rotations keep AVL trees balanced, ensuring consistent performance.
392395
Red-Black trees are a type of self-balancing binary search trees, with each node bearing an additional attribute: color. Each node in a Red-Black tree is colored either red or black, and these color attributes follow specific rules to maintain the tree's balance. This balance ensures that fundamental tree operations such as insertions, deletions, and searches remain efficient.
393396

394397
```
395-
(30B)
396-
/ \
397-
(20R) (40B)
398-
/ \ / \
398+
#
399+
(30B)
400+
/ \
401+
(20R) (40B)
402+
/ \ / \
399403
(10B) (25B) (35B) (50B)
400404
```
401405

0 commit comments

Comments
 (0)