SlideShare a Scribd company logo
Linked ListLinked List
고급게임알고리즘고급게임알고리즘
서진택 , jintaeks@gmail.com
동서대학교 , 디지털콘텐츠학부
2016 년 3 월
Presentation Outline
 Linked List
 Logarithm
 Binary Search Tree
 B-tree
2
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
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
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
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
tail
data next link
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
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
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
Practice: Tree
 Implement a node for a tree data structure.
 a node can have zero or more child nodes.
10
 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
 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
 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
 the height of a tree is 4.
14
leaf
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
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
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
example: list handle
int 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'
consistently
intList.insert(listHandle, 99); // 1, 9, 99, 3, 5
for (int c : intList) {
std::cout << c << 'n';
}
return 0;
}
18
example
int 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'
consistently
intList.insert(listHandle, 99); // 1, 9, 99, 3, 5
for (int c : intList) {
std::cout << c << 'n';
}
return 0;
}
19
example
int 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'
consistently
intList.insert(listHandle, 99); // 1, 9, 99, 3, 5
for (int c : intList) {
std::cout << c << 'n';
}
return 0;
}
20
Singly linked list
struct KNode
{
int data;
KNode* next;
};
InsertAfter( KNode* node, KNode* newNode);
21
Singly linked list
RemoveAfter( KNode* node);
22
Practice: simple linked list
 implement a KLinkedList which uses KNode.
 KLinkedList must support below methods:
– InsertAfter()
– RemoveAfter()
23
Linked lists vs. dynamic arrays
24
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 = 10                3
); the
multiplication is repeated three times.
25
 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 log  2(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
 A full 3-ary tree can be used to visualize the
exponents of 3 and how the logarithm function relates
to them.
27
big O notation
 find node in a linked list.
– O(n)
 bubble sort.
– O(n2
)
 binary search.
– O(log(n))
28
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
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
 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
implementing binary search trees
typedef struct tree {
item_type item; // data item
struct tree* parent; // pointer to
parent
struct tree* left; // pointer to
left child
struct tree* right; // pointer to
right child
} tree;
32
searching in a tree
tree *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
finding minimum element in a tree
tree *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
traversing in a tree
void traverse_tree(tree *l)
{
if (l != NULL) {
traverse_tree(l->left);
process_item(l->item);
traverse_tree(l->right);
}
}
35
insertion in a tree
insert_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
deletion from a tree
37
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
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
 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 and  a2,
and all values in the rightmost subtree will be
greater than a2.40
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
42
rule B applied for '2'
only rule A applied
rule B applied for '6'
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
 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
 This process is continued until we reach a level with
only one node and it is not overfilled.
45
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 foundn";
}
}
46
References
 https://en.wikipedia.org/wiki/Linked_list
 https://en.wikipedia.org/wiki/B-tree
 Skiena, The Algorithm Design Manual
47

More Related Content

PPTX
Data Structures
PDF
Linked list (introduction) 1
PPTX
Tree data structure in java
PPT
Chapter 8: tree data structure
PPT
Trees - Data structures in C/Java
PPTX
Data Structure & Algorithms | Computer Science
PPTX
Binary tree and operations
PPT
Tree and Binary Search tree
Data Structures
Linked list (introduction) 1
Tree data structure in java
Chapter 8: tree data structure
Trees - Data structures in C/Java
Data Structure & Algorithms | Computer Science
Binary tree and operations
Tree and Binary Search tree

What's hot (20)

PPTX
Introduction to data structure ppt
PDF
Binary Search Tree
PPT
Ch13 Binary Search Tree
DOC
e computer notes - Binary search tree
PPT
Binary trees
PPTX
THREADED BINARY TREE AND BINARY SEARCH TREE
PPTX
Binary Search Tree (BST)
PPSX
data structure(tree operations)
PPTX
Binary tree and Binary search tree
PPT
Data structure
PPTX
Binary tree
PPTX
Tree - Data Structure
PPTX
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
PPTX
Binary Search Tree
PDF
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
PPTX
trees in data structure
PPT
Introductiont To Aray,Tree,Stack, Queue
PDF
Unit iv data structure-converted
PPT
PPT
Chapter 1( intro &amp; overview)
Introduction to data structure ppt
Binary Search Tree
Ch13 Binary Search Tree
e computer notes - Binary search tree
Binary trees
THREADED BINARY TREE AND BINARY SEARCH TREE
Binary Search Tree (BST)
data structure(tree operations)
Binary tree and Binary search tree
Data structure
Binary tree
Tree - Data Structure
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Binary Search Tree
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
trees in data structure
Introductiont To Aray,Tree,Stack, Queue
Unit iv data structure-converted
Chapter 1( intro &amp; overview)
Ad

Similar to 02 linked list_20160217_jintaekseo (20)

PDF
Introduction to Data Structure
PPTX
Advanced Data Structures and Algorithms.pptx
PPTX
LinkedLists important things DS peractie.pptx
PPTX
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
PPT
linked_lists.ppt linked_lists linked_lists
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
PDF
2. Introduction to Data Structure.pdf
PPTX
Linked Lists, Single Linked list and its operations
PPTX
Data Structure
PDF
Data structure
PPTX
Datastucture-Unit 4-Linked List Presentation.pptx
PPT
1 list datastructures
PPTX
csc211_lecture_21.pptx
PPT
ARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCE
PPTX
Data Structure and Algorithm Lesson 2.pptx
PPTX
datastructureppt-190327174340 (1).pptx
PDF
Static arrays are structures whose size is fixed at compile time and.pdf
PPT
Linked List
PPT
Link list using array in Data structure amd algorithms
PPTX
1.3 Linked List.pptx
Introduction to Data Structure
Advanced Data Structures and Algorithms.pptx
LinkedLists important things DS peractie.pptx
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
linked_lists.ppt linked_lists linked_lists
Data Structure and Algorithms by Sabeen Memon03.pptx
2. Introduction to Data Structure.pdf
Linked Lists, Single Linked list and its operations
Data Structure
Data structure
Datastucture-Unit 4-Linked List Presentation.pptx
1 list datastructures
csc211_lecture_21.pptx
ARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCE
Data Structure and Algorithm Lesson 2.pptx
datastructureppt-190327174340 (1).pptx
Static arrays are structures whose size is fixed at compile time and.pdf
Linked List
Link list using array in Data structure amd algorithms
1.3 Linked List.pptx
Ad

More from JinTaek Seo (20)

PPTX
Neural network 20161210_jintaekseo
PPTX
05 heap 20161110_jintaeks
PPTX
Hermite spline english_20161201_jintaeks
PPT
01 stack 20160908_jintaek_seo
PPTX
03 fsm how_toimplementai_state_20161006_jintaeks
PPTX
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
PPTX
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
PPTX
Beginning direct3d gameprogramming08_usingtextures_20160428_jintaeks
PPTX
Beginning direct3d gameprogramming07_lightsandmaterials_20161117_jintaeks
PPTX
Beginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeks
PPTX
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks
PPTX
Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks
PPTX
Beginning direct3d gameprogramming02_overviewofhalandcom_20160408_jintaeks
PPTX
Beginning direct3d gameprogramming01_thehistoryofdirect3dgraphics_20160407_ji...
PPTX
Beginning direct3d gameprogramming01_20161102_jintaeks
PPTX
Beginning direct3d gameprogramming03_programmingconventions_20160414_jintaeks
PPTX
Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks
PPTX
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
PPTX
Beginning direct3d gameprogrammingmath04_calculus_20160324_jintaeks
PPTX
Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks
Neural network 20161210_jintaekseo
05 heap 20161110_jintaeks
Hermite spline english_20161201_jintaeks
01 stack 20160908_jintaek_seo
03 fsm how_toimplementai_state_20161006_jintaeks
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming08_usingtextures_20160428_jintaeks
Beginning direct3d gameprogramming07_lightsandmaterials_20161117_jintaeks
Beginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeks
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks
Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks
Beginning direct3d gameprogramming02_overviewofhalandcom_20160408_jintaeks
Beginning direct3d gameprogramming01_thehistoryofdirect3dgraphics_20160407_ji...
Beginning direct3d gameprogramming01_20161102_jintaeks
Beginning direct3d gameprogramming03_programmingconventions_20160414_jintaeks
Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
Beginning direct3d gameprogrammingmath04_calculus_20160324_jintaeks
Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPT
Mechanical Engineering MATERIALS Selection
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Lecture Notes Electrical Wiring System Components
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Welding lecture in detail for understanding
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
composite construction of structures.pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Well-logging-methods_new................
PPT
Project quality management in manufacturing
PPTX
web development for engineering and engineering
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Mechanical Engineering MATERIALS Selection
additive manufacturing of ss316l using mig welding
Lecture Notes Electrical Wiring System Components
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
UNIT 4 Total Quality Management .pptx
Welding lecture in detail for understanding
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
composite construction of structures.pdf
573137875-Attendance-Management-System-original
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Well-logging-methods_new................
Project quality management in manufacturing
web development for engineering and engineering
bas. eng. economics group 4 presentation 1.pptx
Foundation to blockchain - A guide to Blockchain Tech
OOP with Java - Java Introduction (Basics)
UNIT-1 - COAL BASED THERMAL POWER PLANTS
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...

02 linked list_20160217_jintaekseo

  • 1. Linked ListLinked List 고급게임알고리즘고급게임알고리즘 서진택 , jintaeks@gmail.com 동서대학교 , 디지털콘텐츠학부 2016 년 3 월
  • 2. Presentation Outline  Linked List  Logarithm  Binary Search Tree  B-tree 2
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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 tail data next link
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. Practice: Tree  Implement a node for a tree data structure.  a node can have zero or more child nodes. 10
  • 11.  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
  • 12.  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
  • 13.  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
  • 14.  the height of a tree is 4. 14 leaf
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. example: list handle int 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' consistently intList.insert(listHandle, 99); // 1, 9, 99, 3, 5 for (int c : intList) { std::cout << c << 'n'; } return 0; } 18
  • 19. example int 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' consistently intList.insert(listHandle, 99); // 1, 9, 99, 3, 5 for (int c : intList) { std::cout << c << 'n'; } return 0; } 19
  • 20. example int 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' consistently intList.insert(listHandle, 99); // 1, 9, 99, 3, 5 for (int c : intList) { std::cout << c << 'n'; } return 0; } 20
  • 21. Singly linked list struct KNode { int data; KNode* next; }; InsertAfter( KNode* node, KNode* newNode); 21
  • 23. Practice: simple linked list  implement a KLinkedList which uses KNode.  KLinkedList must support below methods: – InsertAfter() – RemoveAfter() 23
  • 24. Linked lists vs. dynamic arrays 24
  • 25. 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 = 10                3 ); the multiplication is repeated three times. 25
  • 26.  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 log  2(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
  • 27.  A full 3-ary tree can be used to visualize the exponents of 3 and how the logarithm function relates to them. 27
  • 28. big O notation  find node in a linked list. – O(n)  bubble sort. – O(n2 )  binary search. – O(log(n)) 28
  • 29. 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
  • 30. 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
  • 31.  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
  • 32. implementing binary search trees typedef struct tree { item_type item; // data item struct tree* parent; // pointer to parent struct tree* left; // pointer to left child struct tree* right; // pointer to right child } tree; 32
  • 33. searching in a tree tree *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
  • 34. finding minimum element in a tree tree *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
  • 35. traversing in a tree void traverse_tree(tree *l) { if (l != NULL) { traverse_tree(l->left); process_item(l->item); traverse_tree(l->right); } } 35
  • 36. insertion in a tree insert_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
  • 37. deletion from a tree 37
  • 38. 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
  • 39. 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
  • 40.  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 and  a2, and all values in the rightmost subtree will be greater than a2.40
  • 41. 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
  • 42. 42 rule B applied for '2' only rule A applied rule B applied for '6'
  • 43. 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
  • 44.  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
  • 45.  This process is continued until we reach a level with only one node and it is not overfilled. 45
  • 46. 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 foundn"; } } 46

Editor's Notes

  • #5: the list elements can easily be inserted or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory.
  • #31: The order among “brother” nodes matters in rooted trees, so left is different from right. Figure 3.2 gives the shapes of the five distinct binary trees that can be formed on three nodes.
  • #32: This search tree labeling scheme is very special. For any binary tree on n nodes, and any set of n keys, there is exactly one labeling that makes it a binary search tree. The allowable labelings for three-node trees are given in Figure 3.2.
  • #38: There are three cases, illustrated in Figure 3.4. Leaf nodes have no children, and so may be deleted by simply clearing the pointer to the given node. The case of the doomed node having one child is also straightforward. There is one parent and one grandchild, and we can link the grandchild directly to the parent without violating the in-order labeling property of the tree. But what of a to-be-deleted node with two children? Our solution is to relabel this node with the key of its immediate successor in sorted order. This successor must be the smallest value in the right subtree, specifically the leftmost descendant in the right subtree (p). Moving this to the point of deletion results in a properlylabeled binary search tree, and reduces our deletion problem to physically removing a node with at most one child—a case that has been resolved above. The full implementation has been omitted here because it looks a little ghastly, but the code follows logically from the description above. The worst-case complexity analysis is as follows. Every deletion requires the cost of at most two search operations, each taking O(h) time where h is the height of the tree, plus a constant amount of pointer manipulation.
  • #40: Unlike self-balancing binary search trees, the B-tree is optimized for systems that read and write large blocks of data. B-trees are a good example of a data structure for external memory. It is commonly used in databases and filesystems.