47
Linked List Linked List 고고고고고고고고 고고고고고고고고 서서서 , [email protected] 서서서서서 , 서서서서서서서서 2016 서 3 서

02 linked list_20160217_jintaekseo

Embed Size (px)

Citation preview

Page 1: 02 linked list_20160217_jintaekseo

Linked ListLinked List고급게임알고리즘고급게임알고리즘

서진택 , [email protected]동서대학교 , 디지털콘텐츠학부

2016 년 3 월

Page 2: 02 linked list_20160217_jintaekseo

Presentation Outline Linked List Logarithm Binary Search Tree B-tree

2

Page 3: 02 linked list_20160217_jintaekseo

Linked list a linear collection of data elements, called nodes

pointing to the next node by means of pointer. a data structure consisting of a group of nodes which

together represent a sequence.

can be used to implement several other common abstract data types, including lists (the abstract data type), stacks, queues.

the list elements can easily be inserted or removed without reallocation or reorganization of the entire structure.

3

Page 4: 02 linked list_20160217_jintaekseo

Advantages a dynamic data structure, which can grow and be

pruned, allocating and deallocating memory while the program is running.

Insertion and deletion node operations are easily implemented in a linked list.

Linear data structures such as stacks and queues are easily executed with a linked list.

the list elements can easily be inserted or removed without reallocation or reorganization of the entire structure.

4

Page 5: 02 linked list_20160217_jintaekseo

Disadvantages They have a tendency to use more memory due to 

pointers requiring extra storage space. Nodes in a linked list must be read in order from the

beginning as linked lists are inherently sequential access.

Nodes are stored incontiguously, greatly increasing the time required to access individual elements within the list.

5

Page 6: 02 linked list_20160217_jintaekseo

Terms Each record of a linked list is often called an 'element'

or 'node'. The field of each node that contains the address of the

next node is usually called the 'next link' or 'next pointer'. The remaining fields are known as the 'data', 'information', 'value', 'cargo', or 'payload' fields.

The 'head' of a list is its first node. The 'tail' of a list may refer either to the rest of the list after the head, or to the last node in the list.

6

head taildata next link

Page 7: 02 linked list_20160217_jintaekseo

Singly linked list Singly linked lists contain nodes which have a data

field as well as a 'next' field, which points to the next node in line of nodes.

Operations that can be performed on singly linked lists include insertion, deletion and traversal.

7

Page 8: 02 linked list_20160217_jintaekseo

Doubly linked list each node contains, besides the next-node link, a

second link field pointing to the 'previous' node in the sequence.

The two links may be called 'forward('s') and 'backwards', or 'next' and 'prev'('previous').

Many modern operating systems use doubly linked lists to maintain references to active processes, threads, and other dynamic objects.

A common strategy for rootkits to evade detection is to unlink themselves from these lists.

8

Page 9: 02 linked list_20160217_jintaekseo

Multiple linked list each node contains two or more link fields, each field

being used to connect the same set of data records in a different order (e.g., by name, by department, by date of birth, etc.).

9

Page 10: 02 linked list_20160217_jintaekseo

Practice: Tree Implement a node for a tree data structure. a node can have zero or more child nodes.

10

Page 11: 02 linked list_20160217_jintaekseo

Root – The top node in a tree. Child – A node directly connected to another node when

moving away from the Root. Parent – The converse notion of a child. Siblings – Nodes with the same parent. Descendant – A node reachable by repeated

proceeding from parent to child. Ancestor – A node reachable by repeated proceeding

from child to parent. Leaf – A node with no children. Internal node – A node with at least one child External node – A node with no children.

11

Page 12: 02 linked list_20160217_jintaekseo

Degree – Number of sub trees of a node. Edge – Connection between one node to another. Path – A sequence of nodes and edges connecting a

node with a descendant. Level – The level of a node is defined by 1 + (the

number of connections between the node and the root). Height of node – The height of a node is the number of

edges on the longest downward path between that node and a leaf.

Height of tree – The height of a tree is the height of its root node.

12

Page 13: 02 linked list_20160217_jintaekseo

when node '6' is concerned; the degree is 2, number of child nodes. the path from root is '2''7''6' node '6' is at level 3.

13 child

sibling

parent ancestor

descendent

Page 14: 02 linked list_20160217_jintaekseo

the height of a tree is 4.

14

leaf

Page 15: 02 linked list_20160217_jintaekseo

Circular Linked list In the last node of a list, the link field often contains

a null reference, a special value used to indicate the lack of further nodes.

A less common convention is to make it point to the first node of the list; in that case the list is said to be 'circular' or 'circularly linked'; otherwise it is said to be 'open' or 'linear'.

15

Page 16: 02 linked list_20160217_jintaekseo

Sentinel nodes In some implementations an extra 'sentinel' or

'dummy' node may be added before the first data record or after the last one.

This convention simplifies and accelerates some list-handling algorithms, by ensuring that all links can be safely dereferenced and that every list (even one that contains no data elements) always has a "first" and "last" node.

16

Page 17: 02 linked list_20160217_jintaekseo

List handles Since a reference to the first node gives access to the

whole list, that reference is often called the 'address', 'pointer', or 'handle' of the list.

Algorithms that manipulate linked lists usually get such handles to the input lists and return the handles to the resulting lists.

In some situations, it may be convenient to refer to a list by a handle that consists of two links, pointing to its first and last nodes.

17

Page 18: 02 linked list_20160217_jintaekseo

example: list handleint main(){std::list<int> intList;

intList.assign({ 1, 3, 5 });

std::list<int>::iterator listHandle = intList.begin();listHandle++; // listHandle indicates node '3'

intList.insert(listHandle, 9); // 1, 9, 3, 5 and listHandle indicates '3' consistentlyintList.insert(listHandle, 99); // 1, 9, 99, 3, 5

for (int c : intList) { std::cout << c << '\n';}

return 0;}

18

Page 19: 02 linked list_20160217_jintaekseo

exampleint main(){std::list<int> intList;

intList.assign({ 1, 3, 5 });

std::list<int>::iterator listHandle = intList.begin();listHandle++; // listHandle indicates node '3'

intList.insert(listHandle, 9); // 1, 9, 3, 5 and listHandle indicates '3' consistentlyintList.insert(listHandle, 99); // 1, 9, 99, 3, 5

for (int c : intList) { std::cout << c << '\n';}

return 0;}

19

Page 20: 02 linked list_20160217_jintaekseo

exampleint main(){std::list<int> intList;

intList.assign({ 1, 3, 5 });

std::list<int>::iterator listHandle = intList.begin();listHandle++; // listHandle indicates node '3'

intList.insert(listHandle, 9); // 1, 9, 3, 5 and listHandle indicates '3' consistentlyintList.insert(listHandle, 99); // 1, 9, 99, 3, 5

for (int c : intList) { std::cout << c << '\n';}

return 0;}

20

Page 21: 02 linked list_20160217_jintaekseo

Singly linked liststruct KNode{ int data; KNode* next;};

InsertAfter( KNode* node, KNode* newNode);

21

Page 22: 02 linked list_20160217_jintaekseo

Singly linked listRemoveAfter( KNode* node);

22

Page 23: 02 linked list_20160217_jintaekseo

Practice: simple linked list implement a KLinkedList which uses KNode. KLinkedList must support below methods:

– InsertAfter()– RemoveAfter()

23

Page 24: 02 linked list_20160217_jintaekseo

Linked lists vs. dynamic arrays

24

Page 25: 02 linked list_20160217_jintaekseo

logarithm. In mathematics, the logarithm is the inverse operation

 to exponentiation. That means the logarithm of a number is the exponent

 to which another fixed value, the base, must be raised to produce that number.

In simple cases the logarithm counts repeated multiplication.

For example, the base 10 logarithm of 1000 is 3, as 10 to the power 3 is 1000 (1000 = 10 × 10 × 10 = 103); the multiplication is repeated three times.

25

Page 26: 02 linked list_20160217_jintaekseo

The logarithm of x to base b, denoted logb(x), is the unique real number y such that

by = x. For example, as 64 =

26, we have log2(64) = 6. The logarithm to

base 10 (that is b = 10) is called the common logarithm and has many applications in science and engineering.

26

Page 27: 02 linked list_20160217_jintaekseo

A full 3-ary tree can be used to visualize the exponents of 3 and how the logarithm function relates to them.

27

Page 28: 02 linked list_20160217_jintaekseo

big O notation find node in a linked list.

– O(n) bubble sort.

– O(n2) binary search.

– O(log(n))

28

Page 29: 02 linked list_20160217_jintaekseo

Practice: skill inventory with timer In morpg game, we maintains skill inventories. When a skill is used, there is a delay time so we must

wait to reuse the skill again. We maintains skill nodes using a linked list. On each frame move, we must calculate expiring times

of all activated skill nodes in the skill inventory. implement skill inventory with efficient algorithm.

– modify KNode and KLinkedList.

29

Page 30: 02 linked list_20160217_jintaekseo

Binary search tree Binary search requires that we have fast access to two

elements—specifically the median elements above and below the given node.

To combine these ideas, we need a “linked list” with two pointers per node.– This is the basic idea behind binary search trees.

A rooted binary tree is recursively defined as either being (1) empty, or (2) consisting of a node called the root, together with two rooted binary trees called the left and right subtrees, respectively.

30

Page 31: 02 linked list_20160217_jintaekseo

A binary search tree labels each node in a binary tree with a single key such that for any node labeled x, all nodes in the left subtree of x have keys < x while all nodes in the right subtree of x have keys > x.

31

Page 32: 02 linked list_20160217_jintaekseo

implementing binary search treestypedef struct tree {

item_type item; // data itemstruct tree* parent; // pointer to parentstruct tree* left; // pointer to left childstruct tree* right; // pointer to right child

} tree;

32

Page 33: 02 linked list_20160217_jintaekseo

searching in a treetree *search_tree(tree *l, item_type x){ if (l == NULL) return(NULL); if (l->item == x) return(l); if (x < l->item) return( search_tree(l->left, x) ); else return( search_tree(l->right, x) );}

33

Page 34: 02 linked list_20160217_jintaekseo

finding minimum element in a treetree *find_minimum(tree *t){ tree *min; // pointer to minimum if (t == NULL) return(NULL); min = t; while (min->left != NULL) min = min->left; return(min);}

34

Page 35: 02 linked list_20160217_jintaekseo

traversing in a treevoid traverse_tree(tree *l){ if (l != NULL) { traverse_tree(l->left); process_item(l->item); traverse_tree(l->right); }}

35

Page 36: 02 linked list_20160217_jintaekseo

insertion in a treeinsert_tree(tree **l, item_type x, tree *parent){ tree *p; /* temporary pointer */ if (*l == NULL) { p = malloc(sizeof(tree)); /* allocate new node */ p->item = x; p->left = p->right = NULL; p->parent = parent; *l = p; /* link into parent’s record */ return; } if (x < (*l)->item) insert_tree(&((*l)->left), x, *l); else insert_tree(&((*l)->right), x, *l);}

36

Page 37: 02 linked list_20160217_jintaekseo

deletion from a tree

37

Page 38: 02 linked list_20160217_jintaekseo

How good are binary search trees? Unfortunately, bad things can happen when building

trees through insertion. The data structure has no control over the order of

insertion. Consider what happens if the user inserts the keys in sorted order. The operations insert(a), followed by insert(b), insert(c), insert(d), . . . will produce a skinny linear height tree where only right pointers are used.

38

Page 39: 02 linked list_20160217_jintaekseo

B-tree In computer science, a B-tree is a self-balancing tree

data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time.

The B-tree is a generalization of a binary search tree in that a node can have more than two children.

39

Page 40: 02 linked list_20160217_jintaekseo

In B-trees, internal (non-leaf) nodes can have a variable number of child nodes within some pre-defined range. When data is inserted or removed from a node, its number of child nodes changes. In order to maintain the pre-defined range, internal nodes may be joined or split.

Each internal node of a B-tree will contain a number of keys. The keys act as separation values which divide its subtrees.

For example, if an internal node has 3 child nodes (or subtrees) then it must have 2 keys: a1 and a2. All values in the leftmost subtree will be less than a1, all values in the middle subtree will be between a1 anda2, and all values in the rightmost subtree will be greater than a2.

40

Page 41: 02 linked list_20160217_jintaekseo

Insertion If the node contains fewer than the maximum legal

number of elements, then there is room for the new element. Insert the new element in the node, keeping the node's elements ordered.

Otherwise the node is full, evenly split it into two nodes so:– A single median is chosen from among the leaf's elements

and the new element.– Values less than the median are put in the new left node and

values greater than the median are put in the new right node, with the median acting as a separation value.

– The separation value is inserted in the node's parent, which may cause it to be split, and so on(rule A). If the node has no parent (i.e., the node was the root), create a new root above this node (increasing the height of the tree)(rule B).

41

Page 42: 02 linked list_20160217_jintaekseo

42

rule B applied for '2'

only rule A applied

rule B applied for '6'

Page 43: 02 linked list_20160217_jintaekseo

Initial construction For example, if the leaf nodes have maximum size 4

and the initial collection is the integers 1 through 24, we would initially construct 4 leaf nodes containing 5 values each and 1 which contains 4 values:

suppose the internal nodes contain at most 2 values (3 child pointers).

43

Page 44: 02 linked list_20160217_jintaekseo

We build the next level up from the leaves by taking the last element from each leaf node except the last one.

Again, each node except the last will contain one extra value. In the example, suppose the internal nodes contain at most 2 values (3 child pointers).

44

Page 45: 02 linked list_20160217_jintaekseo

This process is continued until we reach a level with only one node and it is not overfilled.

45

Page 46: 02 linked list_20160217_jintaekseo

example: std::map#include <iostream>#include <map> int main(){ std::map<int,char> example = {{1,'a'},{2,'b'}}; auto search = example.find(2); if(search != example.end()) { std::cout << "Found " << search->first << " " << search->second << '\n'; } else { std::cout << "Not found\n"; }}

46

Page 47: 02 linked list_20160217_jintaekseo

References https://en.wikipedia.org/wiki/Linked_list https://en.wikipedia.org/wiki/B-tree Skiena, The Algorithm Design Manual

47