Category Archives: Data Structures

My collection of notes on data structures.

Mighty Morphin Data Structures, Part 5

Introduction

The goal for this season of AllWebSD is to deconstruct lessons learned from going head first into data structures. It’s my hope that I can translate a complex topic into an easy-going format and I’ll do so by channeling my inner 10-year-old self. You see back in my day, I was a big fan of the original Mighty Morphin Power Rangers series. So I’m going to take snapshots from that show and brush it with some code.

Information on data structures are abundant. To be honest, you don’t have to invent this. And frankly, neither did I. Hence, the code is pre-baked and available on Github.

The additional and intentional challenge here is that this is in audio only. If you can follow along, awesome. But remember, the source code is on the repo. I’ll upload a video as well, just in case.

That said, are you ready? Alright then. It’s Morphin Time!




The Situation

We need DinoZord power now!

Recall from our last episode that we’ve reprioritized the emergencies based on our priority queue. Now it’s time to call on the power of MegaZord by assembling the DinoZords. We’ll do so with a stacked data structure.

This introduces us to the concept of LAST IN FIRST OUT.

The Data Structure

function createStack() {
	// Store our items in an array held in closure.
	const stack = [];

	// Return our stack as a plain JS object.
	return {
		// Place new items at the end of the array.
		push(x) {
			stack.push(x);
		},
		// Remove the final item in the array. This ensures order is maintained.
		pop() {
			if (stack.length === 0) {
				return undefined;
			}
			return stack.pop()
		},
		// Return the last item in the array.
		peek() {
			if (stack.length === 0) {
				return undefined;
			}
			if (stack.length === 5) {
				return 'MegaZord activated!';
			}
			return stack[stack.length - 1]
		},
		// Use a getter ensures we always get the current length.
		get length() {
			return stack.length;
		},
		isEmpty() {
			return stack.length === 0;
		}
	}
}

Conclusion

That’s it! I hope you enjoyed learning about data structures with a mighty morphin twist. Let me know your thoughts on this choice thus far and other paths that could’ve been considered.


Thanks again for listening in. Remember, I’m here to foster innovation through conversation. So if you’d like to continue this discussion or any topics previously discussed, join me at San Diego Tech Hub and go head first into the AllWebSD Group. It’s totally free. Just visit this link or click San Diego Tech Hub on the footer of AllWebSD.com. Thanks and Aloha!

Mighty Morphin Data Structures, Part 4

Introduction

The goal for this season of AllWebSD is to deconstruct lessons learned from going head first into data structures. It’s my hope that I can translate a complex topic into an easy-going format and I’ll do so by channeling my inner 10-year-old self. You see back in my day, I was a big fan of the original Mighty Morphin Power Rangers series. So I’m going to take snapshots from that show and brush it with some code.

Information on data structures are abundant. To be honest, you don’t have to invent this. And frankly, neither did I. Hence, the code is pre-baked and available on Github.

The additional and intentional challenge here is that this is in audio only. If you can follow along, awesome. But remember, the source code is on the repo. I’ll upload a video as well, just in case.

That said, are you ready? Alright then. It’s Morphin Time!




The Situation

Recall from our last episode that Rita threw her magic wand down from the moon and it looks like she sent Scorpina to do her bidding. And yikes, she’s 300 feet tall.

The Data Structure

It’s time to introduce our priority queue.

function createPriorityQueue() {
// Make two queues held in closure to determine priority.
  const highPriorityQueue = createQueue();
  const lowPriorityQueue = createQueue();

  return {
	// Set high priority to false by default. Ternary operator will determine if it's high/low priority.
    enqueue(item, isHighPriority = false) {
      const queue = isHighPriority ? highPriorityQueue : lowPriorityQueue;
      queue.enqueue(item);
    },
	// Make sure high priority queue is emptied first BEFORE attempting to dequeue from low priority queue.
    dequeue() {
      if (!highPriorityQueue.isEmpty()) {
        return highPriorityQueue.dequeue();
      }

      return lowPriorityQueue.dequeue();
    },
	// Similar to dequeue(), let's peek from the high priority queue first.
    peek() {
      if (!highPriorityQueue.isEmpty()) {
        return highPriorityQueue.peek();
      }

      return lowPriorityQueue.peek();
    },
    get length() {
      return highPriorityQueue.length + lowPriorityQueue.length;
    },
	// Conjunction of our 2 queues.
    isEmpty() {
      return highPriorityQueue.isEmpty() && lowPriorityQueue.isEmpty();
    }
  }
}

Conclusion

Scorpina is 300 feet tall. We can’t do this alone. We’ll need DinoZord power. Join me in the finale where we introduce MegaZord by way of a stack data structure.


Thanks again for listening in. Remember, I’m here to foster innovation through conversation. So if you’d like to continue this discussion or any topics previously discussed, join me at San Diego Tech Hub and go head first into the AllWebSD Group. It’s totally free. Just visit this link or click San Diego Tech Hub on the footer of AllWebSD.com. Thanks and Aloha!

Mighty Morphin Data Structures, Part 3

Introduction

The goal for this season of AllWebSD is to deconstruct lessons learned from going head first into data structures. It’s my hope that I can translate a complex topic into an easy-going format and I’ll do so by channeling my inner 10-year-old self. You see back in my day, I was a big fan of the original Mighty Morphin Power Rangers series. So I’m going to take snapshots from that show and brush it with some code.

Information on data structures are abundant. To be honest, you don’t have to invent this. And frankly, neither did I. Hence, the code is pre-baked and available on Github.

The additional and intentional challenge here is that this is in audio only. If you can follow along, awesome. But remember, the source code is on the repo. I’ll upload a video as well, just in case.

That said, are you ready? Alright then. It’s Morphin Time!




The Situation

Recall from our last episode that Rita just deployed the Putties and Goldar to Angel Grove. They’re causing havoc very quickly and it’s time to address these problems.

The Data Structure

It’s time to introduce our queue.  A linear data structure which obeys the principle of First In, First Out. And that’s exactly what we’ll need to consider granted what Rita just unloaded to the general public.

We’ll create our queue utilizing a function declaration which returns a JavaScript object.

function createQueue() {
	// Store our items in an array held in closure.
	const queue = [];

	return {
		// Use unshift(), to keep the collection of the array in order by adding items to the front.
		enqueue(x) {
			queue.unshift(x);
		},
		// Use pop(), to remove the final item from the array while maintain order.
		dequeue() {
			if (queue.length === 0) {
				return undefined;
			}
			return queue.pop();
		},
		// Return the next item that's ready to be removed.
		peek() {
			if (queue.length === 0) {
				return undefined;
			}
			return queue[queue.length - 1];
		},
		// Use a getter function to get the CURRENT queue's length.
		get length() {
			return queue.length;
		},
		isEmpty() {
			return queue.length === 0;
		}
	}
}

Conclusion

Oh no. What’s that in the sky? Rita’s wand is shooting down like a bolt of lightning. That’s not good. Let me know your thoughts on this choice thus far and other paths that could’ve been considered.


Thanks again for listening in. Remember, I’m here to foster innovation through conversation. So if you’d like to continue this discussion or any topics previously discussed, join me at San Diego Tech Hub and go head first into the AllWebSD Group. It’s totally free. Just visit this link or click San Diego Tech Hub on the footer of AllWebSD.com. Thanks and Aloha!