Skip to content

Commit 547aca9

Browse files
author
computer0101
committed
add notes of heaps
1 parent 5dcd05e commit 547aca9

File tree

6 files changed

+69
-30
lines changed

6 files changed

+69
-30
lines changed

notes/graphs/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ A graph `( G = <V, E> )` data structure consists of a finite (and possibly mutab
66

77
**Note**: The number of edges `|E|` possible in an undirected graph with `|V|` vertices and no loops: `0 ≤ |E| ≤ |V|(|V| − 1)/2`. We have to divide product `|V|(|V| - 1)` by `2`, however, because it includes every edge twice.
88

9-
#### Representation
9+
### Representation
1010

1111
Graph can be represented by using adjacency matrix, adjacency list or incidence matrix. Each implementations has their own pros and cons associated with it either it could be time complexity on operations or space complexity.
1212

13-
#### Types
13+
### Types
1414

1515
1. Simple graph
1616
2. Undirected or Directed graphs
@@ -20,7 +20,7 @@ Graph can be represented by using adjacency matrix, adjacency list or incidence
2020
6. Infinite graphs
2121
7. Graph having Connected or Disconnected components
2222

23-
#### Operations
23+
### Operations
2424

2525
1. **Add node `(G, x)`** - adds a node to the graph
2626
2. **Add edge `(G, x, y)`** - adds an edge from node x to y
@@ -33,7 +33,7 @@ Graph can be represented by using adjacency matrix, adjacency list or incidence
3333
9. **Breadth First Traversal** - traverses the graph in bfs order
3434
10. **Depth First Traversal** - traverses the graph in dfs order
3535

36-
#### BFS Traversal Psuedo code
36+
### BFS Traversal Psuedo code
3737

3838
1. Initialize a queue with starting node in it.
3939
2. Initialize an empty visited set.
@@ -43,7 +43,7 @@ Graph can be represented by using adjacency matrix, adjacency list or incidence
4343
- Mark the `currNode` as visited.
4444
- Expand the `currNode`, add resulting nodes to the queue if they aren't already visited and aren't in the stack.
4545

46-
#### DFS Traversal Psuedo code
46+
### DFS Traversal Psuedo code
4747

4848
1. Initialize a stack with starting node in it.
4949
2. Initialize an empty visited set.
@@ -55,7 +55,7 @@ Graph can be represented by using adjacency matrix, adjacency list or incidence
5555

5656
**Note**: The way you insert explored nodes into the queue or stack will result into different traversal paths.
5757

58-
#### Applications
58+
### Applications
5959

6060
1. **Shortest Path**: what is the best route between two cities? Google Maps.
6161

@@ -65,7 +65,7 @@ Graph can be represented by using adjacency matrix, adjacency list or incidence
6565

6666
4. **Topological Sorting**: Dependencies of packages can be represented using graphs and those packages are compiled first which have no dependenies or have dependencies but these have already been compiled.
6767

68-
#### Terminologies
68+
### Terminologies
6969

7070
A graph with every pair of its vertices connected by an edge is called **complete**.
7171

notes/heaps/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Heap
2+
3+
Heap is a tree based data structure where the tree is a complete binary tree. This binary tree satisfies the properties of heap:
4+
5+
1. For max heap, any given node C, if P is a parent node of C, then the key (the value) of P is greater than or equal to the key of C.
6+
2. For min heap, any given node C, if P is a parent node of C, then the key (the value) of P is less than or equal to the key of C.
7+
8+
### Representation
9+
10+
Heap can be implemented using binary tree or array.
11+
12+
### Types
13+
14+
1. Min Binary Heap: Returns the least value node present in the heap.
15+
2. Max Binary Heaps: Returns the max value node present in the heap.
16+
17+
### Operations
18+
19+
1. **Insert**: Inserts a node in the heap
20+
2. **ExtractMin/ExtractMax**: Extracts the min or max value node depending on the type of the heap
21+
3. **Delete**: Deletes a node from the heap
22+
4. **Increase/Decrease Key**: Increases or decreases the key value of given node
23+
24+
Main functions of heap are:
25+
26+
1. **MaxHeapify/MinHeapify (aka Bubble Up)**: This is called after inserting an element, which is inserted in the end of the heap. This function traverses the heap in upward direction while making necessary shifts between nodes.
27+
2. **Bubble down**: This is called after deleting an element, which is the root of the heap and the last node of the heap is placed there instead. This function traverses downward in direction while making necessary shifts between nodes.
28+
29+
### Applications
30+
31+
1. Heap Sort: Used to sort the elements in an array
32+
2. Priority Queue: Used to select the highest priority job
33+
3. Graph Algorithms: Used in graph algorithms like Dijkstra’s Shortest Path and Prim’s Minimum Spanning Tree
34+
35+
## References
36+
37+
- [https://en.wikipedia.org/wiki/Heap\_(data_structure)](<https://en.wikipedia.org/wiki/Heap_(data_structure)>)
38+
- [https://en.wikipedia.org/wiki/Binary_heap](https://en.wikipedia.org/wiki/Binary_heap)
39+
- [https://www.geeksforgeeks.org/binary-heap/](https://www.geeksforgeeks.org/binary-heap/)

notes/linked_lists/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ In its most basic form, each node contains: data, and a reference (in other word
66

77
A drawback of linked lists is that access time is linear (and difficult to pipeline). Faster access, such as random access, is not feasible.
88

9-
#### Types
9+
### Types
1010

1111
1. Singly Linked List
1212
2. Circular Linked List
1313
3. Doubly Linked List
1414
4. Circular Doubly Linked List
1515

16-
#### Applications
16+
### Applications
1717

1818
1. Linked Lists can be used to implement Stacks , Queues.
1919
2. Linked Lists can also be used to implement Graphs. (Adjacency list representation of Graph).
@@ -23,7 +23,7 @@ A drawback of linked lists is that access time is linear (and difficult to pipel
2323
6. Previous and next page in web browser – We can access previous and next url searched in web browser by pressing back and next button since, they are linked as linked list.
2424
7. Circular Doubly Linked Lists are used for implementation of advanced data structures like Fibonacci Heap.
2525

26-
#### Time Complexities
26+
### Time Complexities
2727

2828
- Indexing: Θ(n)
2929
- Insert/Delete at beginning: Θ(1)
@@ -32,7 +32,7 @@ A drawback of linked lists is that access time is linear (and difficult to pipel
3232
- Θ(n) when last element is unknown
3333
- Insert/Delete in middle: search time + Θ(1)
3434

35-
#### Operations
35+
### Operations
3636

3737
Following operations can be performed on each type of linked lists:
3838

notes/queues/README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ A queue is an example of a linear data structure, or more abstractly a sequentia
88

99
Queue can be implemented using an Array, Stack or Linked List. The easiest way of implementing a queue is by using an Array.
1010

11-
#### Types of Queues
11+
### Types of Queues
1212

1313
1. Simple Queue
1414
2. Circular Queue
1515
3. Double Ended Queue
1616
4. Circular Double Ended Queue
1717
5. Priority Queue
1818

19-
#### Applications
19+
### Applications
2020

2121
1. Scheduling of resources and multiple processes running on same cpu.
2222
2. When data is transferred asynchronously between two processes, Queue is used for synchronization.
@@ -28,7 +28,7 @@ Queue can be implemented using an Array, Stack or Linked List. The easiest way o
2828

2929
Simple Queue can be implemented using Array, Stack or Linked List. Easiest way to implement simple queue is using array.
3030

31-
#### Operations
31+
### Operations
3232

3333
1. Insert using tail(REAR) pointer.
3434
2. Delete using head(FRONT) pointer.
@@ -37,7 +37,7 @@ Simple Queue can be implemented using Array, Stack or Linked List. Easiest way t
3737

3838
A circular buffer, circular queue, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.
3939

40-
#### Operations
40+
### Operations
4141

4242
1. Insert using tail(REAR) pointer.
4343
2. Delete using head(FRONT) pointer.
@@ -46,7 +46,7 @@ A circular buffer, circular queue, cyclic buffer or ring buffer is a data struct
4646

4747
In computer science, a double-ended queue (abbreviated to deque) is an abstract data type that generalizes a queue, for which elements can be added to or removed from either the front (head) or back (tail). It is also often called a head-tail linked list, though properly this refers to a specific data structure implementation of a deque.
4848

49-
#### Operations
49+
### Operations
5050

5151
1. Insert using tail(REAR) pointer.
5252
2. Insert using head(FRONT) pointer.
@@ -57,7 +57,7 @@ In computer science, a double-ended queue (abbreviated to deque) is an abstract
5757

5858
Cicural Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends.
5959

60-
#### Operations
60+
### Operations
6161

6262
1. Insert using tail(REAR) pointer.
6363
2. Insert using head(FRONT) pointer.
@@ -68,13 +68,13 @@ Cicural Double Ended Queue is a generalized version of Queue data structure that
6868

6969
In computer science, a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. In some implementations, if two elements have the same priority, they are served according to the order in which they were enqueued, while in other implementations, ordering of elements with the same priority is undefined.
7070

71-
#### Operations
71+
### Operations
7272

7373
1. Insert - To insert a new element.
7474
2. Remove - To remove an element depending upon it's priority. Returns the element which is removed.
7575
3. Display - Display's the content of queue.
7676

77-
#### Applications
77+
### Applications
7878

7979
- **Dijkstra’s Shortest Path Algorithm**: When the graph is stored in the form of adjacency list or matrix, priority queue can be used to extract minimum efficiently when implementing Dijkstra’s algorithm.
8080

@@ -88,7 +88,7 @@ In computer science, a priority queue is an abstract data type which is like a r
8888

8989
- **Operating systems**: It is also used in Operating System for load balancing (load balancing on server), interrupt handling.
9090

91-
#### Time Complexities
91+
### Time Complexities
9292

9393
Time complexities of priority queue depends on which sorting algorithm it uses internally to sort elements based on their priority. Here I have used heap sort algorithm, hence the time complexities will be:
9494

notes/stack/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
44

5-
#### Operations
5+
### Operations
66

77
1. **PUSH** - Adds an item in the stack and increments the `top` pointer. If the stack is full, then it is said to be an Overflow condition.
88
2. **POP** - Removes an item from the stack and decrements the `top`. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
@@ -11,15 +11,15 @@ Stack is a linear data structure which follows a particular order in which the o
1111
5. **DISPLAY** - Displays content of stack.
1212
6. **getSize** - Returns the size of the stack irrespective of how many elements are in the stack.
1313

14-
#### Implementation
14+
### Implementation
1515

1616
Stack can be implemented using arrays, linked list or queues. Implementation using arrays is the simplest one.
1717

18-
#### Time Complexities
18+
### Time Complexities
1919

2020
- push(), pop(), isEmpty() and peek() all take O(1) time. We do not run any loop in any of these operations.
2121

22-
#### Applications
22+
### Applications
2323

2424
1. Expression evaluation
2525
2. Expression conversion

notes/trees/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
**A tree is a type of graph**. A tree data structure can be defined recursively (locally) as a collection of nodes/vertices (starting at a root node), where each node is a data structure consisting of a value, together with a list of references (edges) to nodes (the "children"), with the constraints that no reference is duplicated, and none points to the root.
44

5-
#### Types of Trees
5+
### Types of Trees
66

77
1. **Binary Tree** - It is a tree where a parent node has at most 2 children.
88
2. **Binary Search Tree** - It is a node-based binary tree data structure which has the following properties:
99
- The left subtree of a node contains only nodes with keys lesser than the node’s key.
1010
- The right subtree of a node contains only nodes with keys greater than the node’s key.
1111
- The left and right subtree each must also be a binary search tree.
12-
3. **AVL Tree (aka Self Balancing Binary Search Tree)** - It is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than 1 for all nodes and less than -1.
12+
3. **AVL Tree** - It is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than 1 for all nodes and less than -1.
1313
4. **Red Black Tree** - A red–black tree is a kind of self-balancing binary search tree in computer science. Each node of the binary tree has an extra bit, and that bit is often interpreted as the color (red or black) of the node. These color bits are used to ensure the tree remains approximately balanced during insertions and deletions.
1414

1515
[**Refer here**](https://en.wikipedia.org/wiki/List_of_data_structures) for more information on other types of trees and terminologies used.
1616

17-
#### Representation
17+
### Representation
1818

1919
1. Linked List - Trees can be represented using linked list where each parent/root node points to its children.
2020
2. Arrays - Sometimes trees can also be represented using arrays. However, some operations like deletion will take too many shifting of nodes in an array and hence it becomes impractical to implement such operations.
2121

22-
#### Operations
22+
### Operations
2323

2424
1. Insert - Inserts an element in a tree/create a tree.
2525
2. Search - Searches an element in a tree.
@@ -31,7 +31,7 @@
3131

3232
These are the common operations that can be performed on a tree. Different types of trees has different operations which can be performed.
3333

34-
#### Applications
34+
### Applications
3535

3636
1. Heap is a tree data structure which is implemented using arrays and used to implement priority queues.
3737
2. Syntax Tree: Used in Compilers.
@@ -42,7 +42,7 @@ These are the common operations that can be performed on a tree. Different types
4242
7. Router algorithms
4343
8. Many search algorithms in Aritificial Intelligence makes use of Trees and Graphs to maintain the search path and to backtrack if needed.
4444

45-
#### Terminologies
45+
### Terminologies
4646

4747
An **ordered tree** is a rooted tree in which all the children of each vertex are ordered. It is convenient to assume that in a tree's diagram, all the children are ordered left to right.
4848

0 commit comments

Comments
 (0)