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
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
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
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
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.
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.
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.
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.
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.