Skip to content

Commit 7d83a9d

Browse files
committed
adding imformation about trees, stacks and queues
1 parent 3595ab9 commit 7d83a9d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

chapters/principles_of_code/building_blocks.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Building blocks
22

3+
INCOMPLETE!
4+
35
When it comes to programming, there are certain features that will be used again and again, so it's a good idea to mention how they work and where they are used.
46

57
### Variable and Types
@@ -81,7 +83,19 @@ So this is a book about algorithms, and I have already mentioned that I have a s
8183

8284
### Trees
8385

84-
### Lists
86+
Trees are interesting data structures to say the least. It is completely filled with *nodes*, and each node has *children* nodes within it. This means that it is naturally *recursive*. It has nodes within nodes within nodes. Because of this, it is often easier to deal with trees recursively; however, sometimes it is appropriate to transform them into other data structures for later. We'll see more of these structures growing in the loamy soil of this archive. they might seem imposing at first, but you get used to them.
8587

8688
### Stacks and Queues
8789

90+
Stacks and Queues are two sides of the same coin in computer science. They are both simple data structures that hold multiple elements, but allow you to use a single element at a time. The biggest difference between the two structures is the order in which you can access the elements in the data structure.
91+
92+
In *stacks*, data follows *Last In, First Out* (LIFO), which basically means that whichever element you put in last will be the first element you take out. It acts exactly like a stack in real life. If you put a book on a stack of other books, the first book you will look at when sifting through the stack will be the book you just put on the stack.
93+
94+
In *Queues*, data follows *First In, First Out* (FIFO), which means that whichever element you put in first will be the first element you take out. Imagine a queue of people. It would be unfair if the first person in line for groceries were not the first person to receive attention once the attendant finally shows up.
95+
96+
For the most part, though, queues and stacks are treated the same way. There must be a way to:
97+
1. look at the first element (`top()`)
98+
2. to remove the first element (`pop()`)
99+
3. to push elements onto the data structure (`push()`)
100+
101+
The notation for this depends on the language you are using. Queues, for example, will often use `dequeue()` instead of `pop()` and `front()` instead of `top()`. You will see the language-specific details in the source code under the algorithms in this book, so for now it's simple important to know what stacks and queues are and how to access elements held within them.

0 commit comments

Comments
 (0)