ericltw / notes

0 stars 1 forks source link

Data Structure #76

Open ericltw opened 4 years ago

ericltw commented 4 years ago

Introduction

What is Data Structure

解決問題的數據組織。


What is Algorithm

解決特定問題的系統方法,為計算過程的本質。


What is Program

用某種程式語言實現算法。


Types of Data Structure

資料結構分為兩種類型,primitive DS, Non-Primitive DS


Reference
ericltw commented 4 years ago

Recursion

Properties


Why we need recursion


Format of a 'Recursive Function'
SampleRecursion (parameter) {
    if (base case satisfied) {
        return some base value
    } else {
        SampleRecursion(modified parameter)
    }
}


Recursion vs Iteration
Recursion iteration
Space efficient No (system stack) Yes
Time efficient No (system stack, some push, pop operation) Yes
Ease of code Yes No


When to Use Recursion

如上方的其一條件不成立,則避免使用recursion。


Practical Uses of Recurision


Reference
ericltw commented 4 years ago

Algorithm Run Time Analysis

背景

算法的運行時間基於


Intro

透過對算法的輸入大小增加,排除其他因素,量化算法運行所花費的時間。

演算法有多快不是以秒衡量,是以步驟次數。


Why we need

判別給定算法的效率。


Asymptotic Bounding

介紹

用於表示演算法時間複雜度的方式。


Big-O

介紹

為一個演算法的時間複雜度的增長上限。

定義

f(n) is O(g(n)), if for some positive constants c and k, f(n) <= c * g(n) whenever n > k.


Big Omega

介紹

為一個演算法時間複雜度的增長下界。

定義

f(n) belongs to Ω(g(n)), if for some positive constants c and k, f(n) is >= c * g(n) whenever n > k.


Theta

介紹

同時為一個演算法時間複雜度的增長上限與下界。

定義

f(n) is Θ(g(n)), if for some positive constants c1, c2 and k, c1 g(n) <= f(n) and f(n) <= c2g(n) whenever n > k.

so f(n) is Θ(g(n)) 代表 f(n) is O(g(n)) and f(n) is Ω(g(n)).


Reference

ericltw commented 4 years ago

Array

Property


Definition

array是element的集合,每個element都由index所標識,這樣每個element的位置都可以透過index計算出來。


Types of Array


How is Array Represented in Memory


Create an 1D Array

流程


Time / Space Complexity of 1D Array

Time Complexity Space Complexity
Create an empty Array O(1) O(n) //
Insert a value O(1) O(1)
Traversing a given Array O(n) O(1)
Accessing given cell O(1) O(1)
Searching a given value O(n) O(1)
Deleting a given cell's value O(1) O(1)


Time / Space Complexity of 2D Array

Time Complexity Space Complexity
Create an empty Array O(1) O(mn)
Insert a value O(1) O(1)
Traversing a given Array O(mn) O(1)
Accessing given cell O(1) O(1)
Searching a given value O(mn) O(1)
Deleting a given cell's value O(1) O(1)


When to Use Array


When to Avoid Array


Reference

ericltw commented 4 years ago

Linked List

What is Linked List

為linear的資料結構,element是單獨的object。每個element(node)包含兩個item,data和下一個node的reference。linked list的重要特性是其大小是可變的。

  • linear data structure

    element以線性的方式組織,element一個接一個地連接。traversing of data很容易,可以一次性順序地訪問所有數據(traversed in a single run),如array, linked list, stack, queue。

  • non-linear data structure

    element不是以線性方式組織。不能一次性順序地訪問所有數據(traversed in a single run), 如:graph, tree。


Linked List vs Array

array linked list
separate object no yes
change variable size no yes
random access yes no


Components of Linked List


Type of Linked List


Why so many Types of Linked List


How is Linked List Represented in Memory

對比於array,linked list的儲存並非儲存在連續的記憶體位置,每個node分散在記憶體各處,透過node儲存的reference彼此連結。


Single of Linked List

Creation
Insertion
Traversal

loop from head to tail.

Search a value

loop from head to tail.

Deletion of node
Delete the Entire Single Linked List

將head, tail設為null。

Time Complexity
Time Complexity Space Complexity
Creation (one node) O(1) O(1)
Insertion O(n) O(1)
Searching O(n) O(1)
Traversing O(n) O(1)
Deletion of a node O(n) O(1)
Deletion of linked list O(1) O(1)


Circular Single Linked List

Time Complexity
Time Complexity Space Complexity
Creation (one node) O(1) O(1)
Insertion O(n) O(1)
Traversing O(n) O(1)
Searching O(n) O(1)
Deletion of a node O(n) O(1)
Deletion of linked list O(1) O(1)


Double Linked List

Time Complexity
Time Complexity Space Complexity
Creation (one node) O(1) O(1)
Insertion O(n) O(1)
Traversing O(n) O(1)
Searching O(n) O(1)
Deletion of a node O(n) O(1)
Deletion of linked list O(n) O(1)


Circular Double Linked List

Time Complexity
Time Complexity Space Complexity
Creation (one node) O(1) O(1)
Insertion O(n) O(1)
Traversing O(n) O(1)
Searching O(n) O(1)
Deletion of a node O(n) O(1)
Deletion of linked list O(n) O(1)


Time Complexity (Array vs Linked List)

Array Linked List
Create (empty array / one node) O(1) O(1)
Insertion at 1st position O(1) O(1)
Insertion at last position O(1) O(1)
Insertion at nth position O(1) O(n)
Delete from 1st position O(1) O(1)
Delete from last position O(1) O(n) / O(1)
Delete from nth position O(1) O(n)
Searching in unsorted data O(n) O(n)
Searching in sorted data O(log n) O(n)
Accessing nth element O(1) O(n)
Traversing O(n) O(n)
Deleting entire array / linked list O(1) O(1) / O(n)


Reference

ericltw commented 4 years ago

Stack

Property of Stack


Why We Need Stack


Common Operatios


Implementation Options of Stack

  1. Array

    • Pros
      • easy to implement
    • Cons
      • Fixed Size
  2. Linked List

    • Pros
      • variable size
    • Cons
      • moderate to implement


Array Implementation

Time Complexity
Time Complexity Space Complexity
Create Stack O(1) O(n)
Push O(1) O(1)
Pop O(1) O(1)
Peek O(1) O(1)
isEmpty O(1) O(1)
isFull O(1) O(1)
Delete Stack O(1) O(1)


Linked List Implementation

Time Complexity
Time Complexity Space Complexity
Create Stack O(1) O(1)
Push O(1) O(1)
Pop O(1) O(1)
Peek O(1) O(1)
isEmpty O(1) O(1)
Delete Stack O(1) O(1)


When to Use


When to Avoid


Reference

ericltw commented 4 years ago

Queue

Property of Queue


Why we Need Queue


Common Operations


Implementation Options of Queue

  1. Array
    • Linear Queue
    • Circular Queue
  2. Linked List
    • Linear Queue


Array Implementation (linear queue)

Time Complexity
Time Complexity Space Complexity
CreateQueue O(1) O(n)
Enqueue O(1) O(1)
Dequeue O(1) O(1)
Peek O(1) O(1)
isEmpty O(1) O(1)
isFull O(1) O(1)
DeleteQueue O(1) O(1)


Array Implementation (circular queue)

Why Circular Queue

deQueue的操作造成blank cell linear queue (array implementation).

Time Complexity
Time Complexity Space Complexity
CreateQueue O(1) O(n)
Enqueue O(1) O(1)
Dequeue O(1) O(1)
Peek O(1) O(1)
isEmpty O(1) O(1)
isFull O(1) O(1)
DeleteQueue O(1) O(1)


Linked List Implementation (linear queue)

Time Complexity
Time Complexity Space Complexity
CreateQueue O(1) O(1)
Enqueue O(1) O(1)
Dequeue O(1) O(1)
Peek O(1) O(1)
isEmpty O(1) O(1)
DeleteQueue O(1) O(1)


Array vs Linked List Implementation

linked list對比array更佳節省空間(不須事先宣告),且不會有滿空間的情況。


When to Use


When to Avoid


Reference

ericltw commented 4 years ago

Binary Tree

Properties of Tree


Why We Need Tree

linked list對比array更節省空間,但在insertion, deletion增加了運算時間,時間複雜度由O(1) 變為O(n),searching的時間複雜度也維持於O(n)。tree的目的解決linked list insertion, deletion, 和searching時間複雜度的問題。


Tree Terminologies


What is Binary Tree


Why we need learn Binary Tree


Types of Binary Tree


Tree Representation


Binary Tree - Common Operations


Traversing all nodes of Binary Tree (Linked-List)


Search for value un Binary Tree (Linked-List)

if root == null
    return error message
else
    do a level order traversal
        if value found
            return success message
    return unsuccessful message


Insert value in Binary Tree (Linked-List)

if root is blank
    insert new node at root
else
    do a level order traversal and find the first blank space
    insert in that blank place


Delete value from Binary Tree (Linked-List)

search for the node to be deleted
find deepest node in the tree
copy deepest node's data in current node
delete deepest node


Delete Binary Tree (Linked-List)

root = null;


Linked List Implementation

Time Complexity
Time Complexity Space Complexity
Createion of Tree (blank) O(1) O(1)
Insertion of value in tree O(n) O(n)
Deletion of value from tree O(n) O(n)
Searching for a value in tree O(n) O(n)
Traversing a tree O(n) O(n)
Deleting entire tree O(1) O(1)


Array Implementation // TODO

Reference

ericltw commented 4 years ago

Binary Search Tree (BST)

What is BST

Binary Search Tree為Binary Tree,所有的node滿足以下property:


Why we need BST

BST的目的是優化linked list searching, insertion, deletion的時間複雜度,由O(n)優化為O(log n)。


Common Operations of BST


Creation of BST

initial Root with null.


Searching in BST

BST_Search(root, value)
    if (root is null)
        return null
    else if (root == value)
        return root
    else if (value < root)
        BST_Search(root.left, value)
    else if (value > root)
        BST_Search(root.right, value)


Traversal of BST


Inserting a node in BST

Cases
BST_Insert(currentNode, valueToInsert)
    if (current is null)
        create a node, insert 'valueToInsert' in it
    else if (valueToInsert <= currentNode's value)
        currentNode.right = BST_Insert(currentNode.right, valueToInsert)
    else
        currentNode.left = BST_Insert(currentNode.left, valueToInsert)
    return currentNode


Deletion of a node from BST

Cases
Cases 1 - Node to be deleted is leaf node
deleteNodeOfBST(root, valueToBeDeleted)
    if (root == null) return null;
    if (valueToBeDeleted < root's value)
        root.left = deleteNodeOfBST(root.left, valueTOBeDeleted)
    else if (valueToBeDeleted > root's value)
        root.right = deleteNodeOfBST(root.right, valueToBeDeleted)
    else //current node is the node to be deleted
        if root have both children, then find minimum element from right subtree (case#3)
            replace current node with minimum node from the right subtree and delete minimum node from right
        else if nodeToBeDeleted has only left child (case#2)
            root = root.Left()
        else if nodeToBeDeleted has only right child (case#2)
            root = root.Right()
        else root = null
    return root;


Deletion of entire BST

root = null;


Time Complexity

Time Complexity Space Complexity
Creation of Tree O(1) O(1)
Searching of Value O(log n) O(log n)
Traversing Tree O(n) O(n)
Insertion of Node O(log n) O(log n)
Deletion of Node O(log n) O(log n)
Deleting entire Tree O(1) O(1)


Reference

ericltw commented 4 years ago

AVL Tree

Why learn AVL Tree

基於給定的incoming data,BST將歪斜成長,導致insertion, searching, deleting的時間複雜度從理想的O(log n)變為O(n)。AVL tree試圖解決這個問題,透過'Rotation'的概念。


What is AVL Tree

AVL tree為balanced Binary Search Tree,任何node的left subtree和right subtree高度最多相差1。如任何left subtree和right subtree高度相差超過1,都會經過rotation以恢復此屬性。empty height 被視為-1。


Common Operations of AVL Tree


Create a AVL Tree / Search a node / Traverse all nodes

same as binary search tree.


Insert a Node

Cases
Left-Left Condition
rightRotate(currentDisbalancedNode)
    newRoot = currentDisbalancedNode.Left
    currentDisbalancedNode.Left =  currentDisbalancedNode.Left.Right
    newRoot.Right = currentDisbalancedNode
    currentDisbalancedNode.Height = caculateHeight(currentDisbalancedNode)
    newRoot.Height = caculateHeight(newRoot)
Left-Right Condition
leftRotate(currentDisbalancedNode'sLeftChild)
    newRoot = currentDisbalancedNode'sLeftChild.Right
    currentDisbalancedNode'sLeftChild.Right = currentDisbalancedNode'sLeftChild.Right.Left
    newRoot.left = currentDisbalancedNode'sLeftChild
    currentDisbalancedNode'sLeftChild.Height = caculateHeight(currentDisbalancedNode'sLeftChild)
    newRoot.Height = caculateHeight(newRoot)
    return newRoot;
Right-Right Condition
Right-Left Condition
Algorithm // TODO


Deletion of node from AVL Tree

Cases
Algorithm // TODO


Time Complexity

Time Complexity Space Complexity
Creation of AVL Tree O(1) O(1)
Insertion of node O(log n) O(log n)
Deletion of node O(log n) O(log n)
Searching a value O(log n) O(log n)
Traversing entire AVL Tree O(n) O(n)
Deleting entire AVL Tree O(1) O(1)


BST vs AVL

Worst case Worst case
BST AVL
Creation of Tree O(1) O(1)
Insertion of node O(n) O(log n)
Deletion of node O(n) O(log n)
Searching of value O(n) O(log n)
Traversing entire Tree O(n) O(n)
Deleting entire Tree O(1) O(1)


Reference

ericltw commented 4 years ago

Binary Heap

What is Binary Heap


Why we need Binary Heap


Types of Binary Heap


Common Operations


Implementation Options


Insertion of Heap

insertValueInHeap(value)
    if (tree does not exist)
        return error message
    else
        insert 'value' in first unused cell of array
        sizeOfHeap ++
        heapifyBottomToTop(sizeOfHeap)


ExtractMin from Heap

extractMin()
    if (tree does not exist)
        return error message
    else
        extract 1st cell of array
        promote last element to first
        sizeOfHeap --
        heapifyTopToBottom(1)


Time & Space Complexity (array)

Time Complexity Space Complexity
createHeap O(1) O(n)
peekTopOfHeap O(1) O(1)
sizeOfHeap O(1) O(1)
insertValueInHeap O(log n) O(log n)
extractMin / extractMax O(log n) O(log n)
deleteHeap O(1) O(1)


Why Avoid Reference based implementaton

extractMin中取得最後一個node需要O(n)。


Reference