Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Data Structures Cheat Sheet, Cheat Sheet of Java Programming

Cheat Sheet on Java Data Structures: Array, Hash Map, Binary Tree, Depth First Search, and so on

Typology: Cheat Sheet

2019/2020

Uploaded on 10/09/2020

sadayappan
sadayappan ๐Ÿ‡บ๐Ÿ‡ธ

4.5

(15)

246 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Data Structures Cheat Sheet
by Ieternalleo via cheatography.com/45716/cs/13401/
Array
Defi๎˜โ€
nit๎˜ioโ€
n
- Stores data elements based on an
sequen๎˜tial, most commonly 0 based,
index. - Based on [tuple๎˜s]
(๎˜htt๎˜p:/๎˜/en.wi๎˜kip๎˜edi๎˜a.o๎˜rg/๎˜wik๎˜i/T๎˜uple) from
set theory. - They are one of the
oldest, most commonly used data
struct๎˜ures.
Deta๎˜i
ls
- Optimal for indexing; bad at
searching, inserting, and deleting
(except at the end). - Linear arrays, or
one dimens๎˜ional arrays, are the most
basic. - Are static in size, meaning that
they are declared with a fixed size. -
Dynamic arrays are like one
dimens๎˜ional arrays, but have reserved
space for additional elements. - If a
dynamic array is full, it copies it's
contents to a larger array. - Two
dimens๎˜ional arrays have x and y
indices like a grid or nested arrays.
Big-
O
effici๎˜
ency
- Indexing: Linear array: O(1), Dynamic
array: O(1) - Search: Linear array: O(n),
Dynamic array: O(n) - Optimized
Search: Linear array: O(log n),
Dynamic array: O(log n) - Insertion:
Linear array: n/a Dynamic array: O(n)
Linked List
Defi๎˜โ€
nit๎˜ioโ€
n
- Stores data with nodes that point to
other nodes. - Nodes, at its most ba sic
it has one datum and one reference
(another node). - A linked list _chains_
nodes together by pointing one node's
reference towards another node.
Deta๎˜i
ls
- Designed to optimize insertion and
deletion, slow at indexing and
searching. - Doubly linked list has
nodes that reference the previous
node. - Circ๎˜ularly linked list is simple
linked list whose tail, the last node,
references the head, the first node. -
Stack, commonly implem๎˜ented with
linked lists but can be made from
arrays too. - Stacks are last in, first
out (LIFO) data struct๎˜ures. - Made with
a linked list by having the head be the
only place for insertion and removal. -
Queu๎˜es, too can be implem๎˜ented with
a linked list or an array. - Queues are a
first in, first out (FIFO) data structure.
- Made with a doubly linked list that
only removes from head and adds to
tail.
Big-
O
effici๎˜
ency
- Indexing: Linked Lists: O(n) - Search:
Linked Lists: O(n) - Optimized Search:
Linked Lists: O(n) - Insertion: Linked
Lists: O(1)
Hash Map
Defi๎˜โ€
nit๎˜ioโ€
n
- Stores data with key value pairs. -
Hash functi๎˜ons accept a key and
return an output unique only to that
specific key. - This is known as
hash๎˜ing, which is the concept that an
input and an output have a one-to-one
corres๎˜pon๎˜dence to map inform๎˜ation. -
Hash functions return a unique
address in memory for that data.
Deta๎˜i
ls
- Designed to optimize searching,
insertion, and deletion. - Hash
collis๎˜ions are when a hash function
returns the same output for two distinct
inputs. - All hash functions have this
problem. - This is often
accomm๎˜odated for by having the hash
tables be very large. - Hashes are
important for associ๎˜ative arrays and
database indexing.
Big-
O
effici๎˜
ency
- Indexing: Hash Tables: O(1) -
Search: Hash Tables: O(1) - Insertion:
Hash Tables: O(1)
Binary Tree
Defi๎˜
nit๎˜i
on
- Is a tree like data structure where
every node has at most two children. -
There is one left and right child node.
By Ieternalleo
cheatography.com/ieternalleo/
Published 6th November, 2017.
Last updated 6th November, 2017.
Page 1 of 3.
Sponsored by CrosswordCheats.com
Learn to solve cryptic crosswords!
http://crosswordcheats.com
pf3

Partial preview of the text

Download Java Data Structures Cheat Sheet and more Cheat Sheet Java Programming in PDF only on Docsity!

by Ieternalleo via cheatography.com/45716/cs/13401/

Array Defiโ€ nitioโ€ n

  • Stores data elements based on an sequential, most commonly 0 based, index. - Based on [tuples] (http://en.wikipedia.org/wiki/Tuple) from set theory. - They are one of the oldest, most commonly used data structures. Detai ls
  • Optimal for indexing; bad at searching, inserting, and deleting (except at the end). - Linear arrays , or one dimensional arrays, are the most basic. - Are static in size, meaning that they are declared with a fixed size. - Dynamic arrays are like one dimensional arrays, but have reserved space for additional elements. - If a dynamic array is full, it copies it's contents to a larger array. - Two dimensional arrays have x and y indices like a grid or nested arrays. Big- O effici ency
  • Indexing: Linear array: O(1), Dynamic array: O(1) - Search: Linear array: O(n), Dynamic array: O(n) - Optimized Search: Linear array: O(log n), Dynamic array: O(log n) - Insertion: Linear array: n/a Dynamic array: O(n) Linked List Defiโ€ nitioโ€ n
  • Stores data with nodes that point to other nodes. - Nodes, at its most basic it has one datum and one reference (another node). - A linked list chains nodes together by pointing one node's reference towards another node. Detai ls
  • Designed to optimize insertion and deletion, slow at indexing and searching. - Doubly linked list has nodes that reference the previous node. - Circularly linked list is simple linked list whose tail , the last node, references the head , the first node. - Stack , commonly implemented with linked lists but can be made from arrays too. - Stacks are last in, first out (LIFO) data structures. - Made with a linked list by having the head be the only place for insertion and removal. - Queues , too can be implemented with a linked list or an array. - Queues are a first in, first out (FIFO) data structure.
  • Made with a doubly linked list that only removes from head and adds to tail. Big- O effici ency
  • Indexing: Linked Lists: O(n) - Search: Linked Lists: O(n) - Optimized Search: Linked Lists: O(n) - Insertion: Linked Lists: O(1) Hash Map Defiโ€ nitioโ€ n
  • Stores data with key value pairs. - Hash functions accept a key and return an output unique only to that specific key. - This is known as hashing , which is the concept that an input and an output have a one-to-one correspondence to map information. - Hash functions return a unique address in memory for that data. Detai ls
  • Designed to optimize searching, insertion, and deletion. - Hash collisions are when a hash function returns the same output for two distinct inputs. - All hash functions have this problem. - This is often accommodated for by having the hash tables be very large. - Hashes are important for associative arrays and database indexing. Big- O effici ency
  • Indexing: Hash Tables: O(1) - Search: Hash Tables: O(1) - Insertion: Hash Tables: O(1) Binary Tree Defi niti on
  • Is a tree like data structure where every node has at most two children. - There is one left and right child node. By Ieternalleo cheatography.com/ieternalleo/ Published 6th November, 2017. Last updated 6th November, 2017. Page 1 of 3. Sponsored by CrosswordCheats.com Learn to solve cryptic crosswords!

by Ieternalleo via cheatography.com/45716/cs/13401/

Binary Tree (cont) Detai ls

  • Designed to optimize searching and sorting. - A degenerate tree is an unbalanced tree, which if entirely one- sided is a essentially a linked list. - They are comparably simple to implement than other data structures. - Used to make binary search trees. - A binary tree that uses comparable keys to assign which direction a child is. - Left child has a key smaller than it's parent node. - Right child has a key greater than it's parent node. - There can be no duplicate node. - Because of the above it is more likely to be used as a data structure than a binary tree. An AVL Tree is a balanced binary search tree. -The process for inserting or deleting is the same as in a regular(unbalanced) BST , except you have to rebalance after each operation. A node in an AVL tree is balanced if its balance factor is either -1,0, or 1 Big- O effici ency
  • Indexing: Binary Search Tree: O(log n) - Search: Binary Search Tree: O(log n) - Insertion: Binary Search Tree: O(log n) The balance factor of a node is the height of its right subtree minus the height of its left subtree Search Basics Breadth First Search Defiโ€ nitioโ€ n
  • An algorithm that searches a tree (or graph) by searching levels of the tree first, starting at the root. - It finds every node on the same level, most often moving left to right. - While doing this it tracks the children nodes of the nodes on the current level. - When finished examining a level it moves to the left most node on the next level. - The bottom-right most node is evaluated last (the node that is deepest and is farthest right of it's level). Detai ls
  • Optimal for searching a tree that is wider than it is deep. - Uses a queue to store information about the tree while it traverses a tree. - Because it uses a queue it is more memory intensive than depth first search. - The queue uses more memory because it needs to stores pointers Big- O effici ency
  • Search: Breadth First Search: O(|E| + |V|) - E is number of edges - V is number of vertices Depth First Search Defi niti on
  • An algorithm that searches a tree (or graph) by searching depth of the tree first, starting at the root. - It traverses left down a tree until it cannot go further. - Once it reaches the end of a branch it traverses back up trying the right child of nodes on that branch, and if possible left from the right children. - When finished examining a branch it moves to the node right of the root then tries to go left on all it's children until it reaches the bottom. - The right most node is evaluated last (the node that is right of all it's ancestors). Depth First Search (cont) Detai ls
  • Optimal for searching a tree that is deeper than it is wide. - Uses a stack to push nodes onto. - Because a stack is LIFO it does not need to keep track of the nodes pointers and is therefore less memory intensive than breadth first search. - Once it cannot go further left it begins evaluating the stack. Big- O effici ency
  • Search: Depth First Search: O(|E| + |V|) - E is number of edges - V is number of vertices Breadth First Search Vs. Depth First Search
  • The simple answer to this question is that it depends on the size and shape of the tree.
  • For wide, shallow trees use Breadth First Search
  • For deep, narrow trees use Depth First Search Nuances:
  • Because BFS uses queues to store information about the nodes and its children, it could use more memory than is available on your computer. (But you probably won't have to worry about this.)
  • If using a DFS on a tree that is very deep you might go unnecessarily deep in the search. See xkcd for more information.
  • Breadth First Search tends to be a looping algorithm.
  • Depth First Search tends to be a recursive algorithm. Efficient Sorting Basics By Ieternalleo cheatography.com/ieternalleo/ Published 6th November, 2017. Last updated 6th November, 2017. Page 2 of 3. Sponsored by CrosswordCheats.com Learn to solve cryptic crosswords!