0% found this document useful (0 votes)
4 views

DataStructures

Data structure notes

Uploaded by

John Irving
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DataStructures

Data structure notes

Uploaded by

John Irving
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Data Structures:

The simplest data structures are:


Lists: Ordered collections of data
Dictionaries: Key -> value mappings
Sets: Unordered collections of unique data

Stacks:

All supported operations are O(1) by themselves. However, some tasks, like
getting to an item at the bottom of the stack have a higher time complexity because
they require multiple pop operations.
Stack operations are limited: no searching, no sorting, no random access
Stacks, like all abstract data types, can store items of any type. What makes
it a stack is the behavior of the operations, not the type of data it stores.
Stacks are often used in the real world for:
Function call management
Undo/redo functionality
Expression evaluation
Browser history

Linked Lists:
A collection of ordered items similar to a list (thus the name).
There are no indices in a Linked List. each node contains the data itself and
a reference to the next node in the list.
The reference is where the data is physically located, sort of like an
address.
Iterating a Link List requres starting at the head node and following the
next references until you reach the end.
Iterating is possible by way of the yield keyword, which acts like the
return statement, but is used to turn the called object into a generator function.
A generator function creates a new function object. When the new
function is called, it executes until it hits a yield statement. Then the function
pauses and returns the value of the field statement.
When the function is next called it resumes where it last
stopped.
ex) def create_message_generator():
yield "hi"
yield "there"
yield "friend"

gen = create_message_generator()
first = next(gen)
print(first) # prints: hi
second = next(gen)
print(second) # prints: there
third = next(gen)
print(third) # prints: friend

Trees:
Trees are kind of like linked lists in the sense that the root node simply
holds references to its child nodes, which in turn hold references to their
children. The difference between a Linked List and a Tree is that a tree's nodes
can have multiple children instead of just one.

A generic tree structure has the following rules:

Each node has a value and a list of "children"


Children can only have a single "parent"
Trees become more useful when ordered. One of the most common types of ordered tree
is the Binary Search Tree (BST)
A BST must:
1. Instead of unbounded list of children, each node has at most two
children
2. The left child's value must be less than its parent's value
3. The right child's value must be greater than the parent's value
4. No two nodes in the BST can have the same value
These restraints allows add, remove, fine, and update nodes more quickly.

An "inorder" traversal is the most intuitive way to visit all the nodes in a
tree.
It's called "inorder" because the current node is visited between its
children.
It results in an ordered list of the nodes in the tree. The following tree:
> 7
> 6
> 4
> 2
> 1

is visited in this order: [1, 2, 4, 6, 7]


A "postorder" traversal also visits all the nodes in a tree. It's called
"postorder" because the current node is visited after its children.
The tree from before is visited in this order: [1, 2, 6, 7, 4]
A "preorder"traversal is when the current node is visited first.
Inorder travesal example:
if self.left != None:
self.left.inorder(visited)
visited.append(self.val)
if self.right != None:
self.right.inorder(visited)

A red-black tree is a BST with an extra bit a logic to ensure that as nodes
are inserted and deleted, the tree remains relatively balanced.
Nodes are assigned a "color" bit. When the tree is modified the tree is
rearranged and repainted to restore the coloring properties that constrain how
unbalanced the tree can become in the worst case.
This accomplished by a few extra rules:
1. Each node is either red or black.
2. The root is black (this may be omitted.)
3. All Nil leaf nodes are black.
4. If a node is red, then both its children are black.
5. All paths from a single node go through the same number of
black nodes to reach any of its decendent NIL nodes.
Red black trees always have worst-case O(log(n)) insertions,
deletions, lookups and rearrangements

You might also like