Sie sind auf Seite 1von 2

1) Stack: Data Structure with a storage policy that abides by a) Can be implemented as an ArrayDeque or a Linked

LIFO (last in first out) list


a) Important Methods for a stack: i) ArrayDeque uses a resizable circular array, and
i) Boolean empty(): returns true if the stack is does not support indexed operations.
empty, otherwise false (1) ArrayDeque is the recommended
ii) E peek(): returns the object at the top of the implementation
stack without removing it. 4) Trees: can represent hierarchical organizations of
iii) E pop(): Returns the object at the top of the information (i.e. family tree, class hierarchy, etc.). Note,
stack and removes it. although there are numerous types of tree data
iv) E push(E obj): Pushes an item onto the top of structures, the scope of the notes focuses mainly on
the stack and return the item pushed. binary trees
b) Stacks can be implemented by using a vector, or a a) Trees are a recursive data structure because they
list utilize many recursive methods
i) Vector: is a poor choice for stack b) One of the reasons binary trees are useful, is
implementation, since Vector is a built in java because they are more efficient to search than an
API, and is extended to the stack class, therefore array or linked data structure O(log n) vs. O(n)
implementing all vector methods c) Tree Terminology:
ii) The easiest implementation is a List (specifically i) Root: the node at the top of the tree
and ArrayList) ii) Branches: the links from a node to its successors
(1) An underlying array requires reallocation of iii) Children: the successors of a node
space when the array becomes full iv) Parent: the predecessor of a node
(2) An underlying linked data structure requires v) Siblings: nodes that have the same parent
allocating storage for links vi) Leaf: a node that has no children (also known as
(3) All insertions and deletions occur at one external nodes)
end, and are constant time to perform the (1) Nonleaf nodes are known as internal nodes
action (O(1)). vii) Height of the tree: the number of nodes in the
2) Queue: Like a stack but a queue abides by FIFO (first in longest path from the root node to the leaf
first out) node
a) The queue behaves like a line: the next person is line d) A set of nodes T is a binary tree if either of the
is to person who has waited the longest and is next following is true
to be served, and new elements are places at the i) T is empty
end of the line ii) Its root node has two subtree, TL = left subtree,
b) There are three ways to implement a queue TR = right subtree, such that TL and TR are binary
i) Singly Linked List: Linked list implementation trees
requires more storage due to the extra space (1) T is greater than all values in TL and is less
required for the links than all values in TR
ii) Doubly Linked List: requires 1.5x the storage of a e) Huffman Tree: represents Huffman codes for
singly linked list characters that might appear in a text file.
iii) Circular Array: i) Huffman code uses different numbers of bits to
(1) when the circular array is filled to capacity, encode letters; more common characters use
it requires half the storage of a single linked fewer bits
list ii) Programs that need to compress files may use
(2) But a recently reallocated circular array is Huffman codes
half empty and require the same storage as f) A binary tree never has to be sorted because its
a single linked list elements are inserted and removed in a manner that
iv) Regardless of implementation: all operations maintains the binary trees order
are O(1) g) When searching a BST (binary search tree), each
(1) Although reallocating an array is O(n), it is probe has the potential to eliminate half the
amortized over n items, so the cost per item elements in the tree; yielding that searching can be
is O(1) O(log n)
3) Deque: allows insertions and removals from both ends of i) Worst case is O(n)
the queue h) Recursive Algorithm for searching a binary tree:

1
i) if the tree is empty ii) while new item is not at the root and new item
(1) return null (target is not found) is smaller than its parent
ii) else if the target matches the root node's data (1) swap the new item with its parent, moving
(1) return the data stored at the root node the new item up the heap
iii) else if the target is less than the root node's c) Removing an item from a heap
data i) Remove the item in the root node by replacing it
(1) return the result of searching the left with the last item in the heap (LIH)
subtree of the root ii) While item LIH has children and item LIH is
iv) else larger than either of its children
(1) return the result of searching the right (1) Swap item LIH with its smaller child, moving
subtree of the root LIH down the heap
i) Full binary tree: a binary tree where all nodes have d) Because a heap is a complete binary tree, it is
either 2 children or 0 children efficiently implemented using an array, rather than a
j) Perfect binary tree: is a full binary tree of height n linked data structure
with exactly 2n -1 nodes e) Finding the value in the heap from the array in which
it is stored.
i) For a node at position p
(1) L child position = 2p+1
Figure 2: Example of a perfect binary tree: n=3 and (2) R child position = 2p+2
2n-1 = 7 ii) For a child to find its parent (position c)
k) Complete binary tree: is a perfect binary tree (1) Position of parent = (c-1)/2
through level n-1 with some extra lead nodes at level f) The insertion and removal are O(log n)
n (the tree height), all toward the left 6) Priority Queue: a data structure in which only the
highest-priority item is accessible
a) In a priority queue, the smallest item always is
removed first
Figure 3: example of a complete binary tree i) A priority queue implements the characteristics
i) Tree Traversal: determining the nodes of a tree of a heap, and therefore yields that the insertion
and their relationship by walking through the and removal of a priority queue is O(log n)
tree in a prescribed order and visiting the nodes
as they are encountered
(1) Three types of traversals:
(a) Inorder: traverse TL, visit root node,
traverse TL
(b) Presorder: visit root node, Traverse TL,
traverse TR
(c) Postorder: Traverse TL, traverse TR, visit
root node

Figure 4: Sudocode to acquire eachs traversal


5) Heaps: a complete binary tree
a) has the following properties:
i) the value in the root is the smallest item in the
tree
ii) every nonempty subtree is a heap
b) inserting into a heap
i) insert the new item in the next position at the
bottom of the heap

Das könnte Ihnen auch gefallen