Sie sind auf Seite 1von 53

Advanced Data Structures

Chapter III:
Data Structures

FAP:UC-BCF

Contents
Pointers
Trees
- Binary Trees
- Traversals
- Binary Search Trees
- AVL Trees
- M-way Search Trees

FAP:UC-BCF

Static vs. Dynamic Allocation


Static Allocation:
- Binds memory space to variables at compile time.
- Deals with variables that are created statically (compile time).
Examples:
int i, j;
char s[10];
float k;
double d;

FAP:UC-BCF

Static vs. Dynamic Allocation


Dynamic Allocation:
- Binds memory spaces to variables during run-time.
- Deals with data structures whose length is only determined
during run-time.
- Deals with variables that are created and disposed during
execution.
Example: Pointers

FAP:UC-BCF

Pointer Variable
A variable used only to store the memory address of
another variable.
Used to access dynamic variables
Value of a pointer variable-address

FAP:UC-BCF

Pointer Variable
Example:
Charptr at addr
07770
0122222

Charptr at addr
07770

Dynamic Char Var at


Address 0122222
A

Dynamic Char Var at


Address 0122222
A

FAP:UC-BCF

Pointer Variable
Declaration:
Syntax: <data type> *<ptr var name>
where:
<ptr var name> is any user defined name.
<data type> is any simple C data type
Examples:
int *i;
float *f;
char *c;

FAP:UC-BCF

Pointer Variable
Another way:
Syntax:
typedef<data type> *<ptr type name>
typedef<ptr type name> <ptr var name>
Example:
typedef int *pointer;
main()
{ pointer i;
.
.
}
FAP:UC-BCF

Pointer Variable
Operations:
Function malloc()
- syntax: malloc(<size>)
- uses stdlib.h
- the function call returns a pointer to enough
storage for an object of <size> bytes. The storage
is not initialized.

FAP:UC-BCF

Pointer Variable
Example:
main()
{
int *p;
.
.
p = malloc(sizeof(int));
}

FAP:UC-BCF

10

Pointer Variable
The execution of malloc() would obtain a piece of
memory (dynamic variable or node) from the system
adequate to store an integer value and assigns its
address to the pointer p.
Accessing a dynamic variable:
Syntax: *<ptr var name>
the thing pointed to by <ptr var name>

FAP:UC-BCF

11

Pointer Variable
Example:
p = malloc(sizeof(int));
*p = 10;
printf(%d, *p);

Garbage

10

FAP:UC-BCF

12

Pointer Variable
Function free()

Destroy or de-allocate a dynamic variable to some other


data.
Making the allocated storage for the object pointed to by the
<ptr var name> available once again to the system.
Syntax: free(<ptr var name>)
Example:
free(p);

FAP:UC-BCF

13

Pointer Variable
Example:
p

p null

FAP:UC-BCF

10
Garbage

*p = 10
After executing free(p)

P = NULL

14

Dynamic Data Structures


We do not use pointers and dynamic variables by
themselves. Like structures that can be grouped to
become array of records, pointers and dynamic
variables are normally grouped into something called
as Link List.
Basic link list singly linked list

FAP:UC-BCF

15

Singly Linked List


A sequence of dynamic variables or nodes that are
linked together
Declaration:
struct <tag name>
{
<data type1> <field1>;
.
.
<data typen> <fieldn>;
}
Note: one of the fields must be a pointer type that would point to a
structure that is of the same type as the one that contains it.
FAP:UC-BCF

16

Singly Linked List


Example:
struct node
{
int data;
struct node *link;
}

data
FAP:UC-BCF

link
17

Singly Linked List


1

22

30

first

Note:
The linked list terminates with a node whose link field value is
NULL
NULL is a predefined constant pointer which means, pointing
to nothing.

FAP:UC-BCF

18

Singly Linked List


Declaration:
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
typedef struct node nodetype;
typedef nodetype *pointer;

FAP:UC-BCF

19

Singly Linked List


void create_single(pointer *first)
{
int num; pointer x,p;
*first = NULL;
do {
scanf(%d, &num);
if(num != -1)
{
x = malloc(sizeof(nodetype));
x -> data = num;
x -> link = NULL;
if(*first = NULL) *first = x;
else p -> link = x;
p = x;
}
}while(num != -1);
}
main()
{
pointer first;
create_single(&first); }

FAP:UC-BCF

20

Trees
Finite set of one or more nodes such that:

There is a specially designated node called root


The remaining nodes are partitioned into n>=0
disjoints sets T1, T2, Tn, where each set is a tree
called subtrees of the root.

FAP:UC-BCF

21

Trees
Terminology:

Parent: immediate root of a node


Siblings: children of the same parent
Ancestors of a node: all nodes along the path from the root
to that node
Descendants of a node: all nodes of the subtrees of a node
Degree of a node: number of subtrees of a node
Root node: no parents or ancestors
Leaf/terminal node: no children or descendants node with a
degree = 0
Nonleaf/nonterminal node: has at least one child or
descendant node with degree <> 0

FAP:UC-BCF

22

Trees
Terminology:

Degree of a tree = max[degree of nodes]


Level/generation of a node: 1 + length of its path from the
root
Height/depth of a tree: total number of levels
Weight of a tree: number of leaf nodes
Forest: set of disjoint trees

FAP:UC-BCF

23

Binary Trees
Finite set of nodes which is either empty or
consisting of a root with at most two branches to
disjoint binary trees called the left subtree and the
right subtree.
A

FAP:UC-BCF

24

Binary Trees

2i-1 : maximum number of nodes on level l of a binary


tree.
2k-1: maximum number of nodes in a binary tree of
heigh k.
Skewed binary tree: tree that either have 0 left
subtrees or 0 right subtrees.
Full binary tree of depth k: a binary tree of depth k
with 2k-1 nodes.

FAP:UC-BCF

25

Tree Representations
Using Arrays
- static/sequential representation
0

13

Array size needed will be 2k-1, k is the height of a given tree


FAP:UC-BCF

26

Tree Representations
Using linked lists

12

FAP:UC-BCF

15

25

27

Binary Search Tree


A binary tree:
All identifiers in the left subtree are less than the
identifier in the root.
All identifiers in the right subtree are greater than
the identifier in the root.
The left and right subtrees are also binary search
trees.

FAP:UC-BCF

28

Binary Search Tree


Examples:

10
8

15

13

20

FAP:UC-BCF

29

Binary Search Tree


Algorithm: Insertion
If the tree is empty, then insert the new item in the
trees root node.
If the roots key matches the new items key then
skip insertion.
If the new items key is smaller than the roots key,
then insert the new item in the roots left subtree.
Otherwise insert the new item in the roots right
subtree.

FAP:UC-BCF

30

Binary Search Tree


Algorithm: Searching
If the tree is empty, then the target key is not in
the tree.
If the target key is found in the root item then the
target key is found in the root item.
If the target key is smaller than the roots key then
search the left subtree, otherwise search the right
subtree.

FAP:UC-BCF

31

Tree Traversal
Traversals:

Preorder:

Inorder

Data Left Tree Right Tree


Left Tree- Data Right Tree

Postorder

FAP:UC-BCF

Left Tree Right Tree - Data

32

Tree Traversal
Give the preorder, inorder and postorder traversals:
Tree #1

Tree #2
A

FAP:UC-BCF

33

Counting Trees
Create the corresponding counting trees with the
following traversals:
1.

2.
3.
4.

Pre: IAMHEDBCFL
Post: EHDMALFCBI
In: AHEMDICFLB
Pre: ABDGCEHIF
In: DGBAHEICF
Post: CBFEGDA
In: CBAEFDG
Post: FABG/+CD - ^*
In: F/AGB*+^C-D

FAP:UC-BCF

34

AVL Trees

Introduced by Adelson-Velskii and Landis in 1962

Height-balanced binary search trees

Binary search trees in which the heights of the


subtrees of each node differ by at most one level.
Insertion of deletion of nodes causes the search tree
to be rebalanced.

FAP:UC-BCF

35

AVL Trees
Insertion
1.
Insert the node at the proper point.
2.
Starting from the point of insertion, take the first node whose
subtree heights differ by more than one level as A.
3.
If the new node is inserted in the left subtree of A, take the
left child of A as B; if inserted in the right subtree of A, take
the right child of A as B.
4.
If the new node is inserted in the left subtree of B, take the left
child of B as C; if inserted in the right subtree of B, take the
right child of B as C.
5.
Take the inorder traversal of A, B, and C.
6.
Take the middle node as the parent of the two other nodes.
7.
Adjust other nodes if necessary.
FAP:UC-BCF

36

AVL Trees
Deletion
1.
Delete the node.
2.
If the deleted node is a non-terminal node, the immediate
predecessor (immediate successor) replaces the deleted node.
3.
Starting from the deepest level, take the first node whose
subtree heights differ by more than one level as A.
4.
Take the right child of A as B if it has more descendants than
the left child; otherwise, take the left child of A as B.
5.
Take the right child of B as C if it has more descendants than
the left child; otherwise, take the left child of B as C. If equal
prioritize LL over LR or RR over RL.
6.
Take the inorder traversal of A, B, and C.
7.
Take the middle node as the parent of the two other nodes.
8.
Adjust other nodes.
9. FAP:UC-BCF
Rebalance if needed.
37

AVL Trees
Examples: Create AVL trees with the following insertion and
deletion operations. Each number is independent of the other
numbers.
1. Ins: M, T, O, Q, S, R, W, V, Y, U, C, G, E, X
2. Ins: 10, 20, 30, 40, 50, 60, 70
3. Ins: 13, 20, 15, 17, 19, 18, 23, 22, 25, 21, 8, 5, 6, 4, 3, 2
Del: 13, 22, 19, 6, 18
4. Ins: 15, 20, 25, 10, 30, 35, 23, 28, 27, 22, 29, 33
Del: 10
Ins: 24, 21, 26
Del: 22, 23, 21, 28, 27, 20, 25, 26
FAP:UC-BCF

38

AVL Trees
14

20

Del: 35, 14, 5, 11, 15, 17, 20


Ins: 14, 10, 13, 28
Del: 1, 67, 9
Ins: 35, 15, 20
FAP:UC-BCF

17

15

11

35

18

21

67

19

39

M-Way Search Trees


#keys values in a given file = # of nodes that will be
needed by an AVL tree

1M records 1M nodes are needed

Solution: To lessen the number of nodes, is by


allowing the nodes to store more than one key value.
This leads to shorter in height and smaller number of
comparisons.
M-way or Multiway Search Trees: A kind of search
tree in which each node can store 1 to M-1 values 1 to
M links to subtrees.
FAP:UC-BCF

40

M-Way Search Trees


Example:
50
30

10

FAP:UC-BCF

60 90

55 56

70 80

120

41

M-Way Search Trees


Types:
B-Tree

Bayer, McCreight and Kaufman in 1972


Balanced M-Way search tree in which all leaf nodes are at
the same level
Properties

An interior or nonleaf node with N values should contain N+1


links
Root node contains at least 1 value and at most M-1 values
All non-root contain at least M/2 - 1 values and at most M-1
values

FAP:UC-BCF

42

M-Way Search Trees


B-Tree:
Insertion:
1.
2.

3.

Insert the value in the leaf node where it should belong.


If the number of values in any node exceeds the maximum,
that is, the node overflows, the middle value is extracted and
is moved to the parent node (if not existing, create one with a
new label) and among the remaining values, the lower half
will remain in the node and the upper half will be stored in a
new node with a new label. In case there are two middle
values, extract the smaller value. Labeling new nodes is done
from left to right from the lowest level going up.
Split the other nodes if necessary
FAP:UC-BCF

43

M-Way Search Trees


B-Tree:
Deletion:
1.
2.

3.

Delete the value.


If deletion was made from a nonterminal node, the immediate
predecessor replaces the deleted value. The immediate
predecessor of a node is the largest value in the nodes left
subtree while the immediate successor of a node is the
smallest value in the nodes right subtree.
For each node that underflows, that is, a node where the
number of values is less than the minimum do:
If the nearest right sibling has more than the minimum number
of values then
FAP:UC-BCF

44

M-Way Search Trees


a. Move the parent value into the underflowing node and move
the smallest value in the nearest right sibling into the parent
node.
b. Adjust other nodes if necessary
Else
if the nearest left sibling has more than the minimum number of
values then,
a. Move the parent value into the underflowing node and move
the largest value in the nearest left sibling into the parent node.
b. Adjust other nodes if necessary
Else
a. Merge the values of the underflowing node, the values of the
nearest right sibling (if non-existent, nearest left sibling), and
their parent node into the leftmost node.
b. Adjust node if necessary
FAP:UC-BCF

45

M-Way Search Trees


B+Tree

Similar to B tree except that the interior or nonleaf nodes


contain only keys and links; and leaf nodes which contain
keys and addresses are linked in key sequence to facilitate
rapid sequential accessing
Properties similar to that of B Tree.

FAP:UC-BCF

46

M-Way Search Trees


B+ Tree:
Insertion:
1.
2.

3.

4.

Insert the value in a leaf node where it should belong.


If the number of values in a leaf node overflows, the M/2 th
value is replicated in the parent node and among the
remaining values, the first M/2 values will remain in the
node while the remaining nodes will be stored in a new node
with a new label.
If the number of values in a nonleaf overflows, splitting is
done in the sam way that is done with B-Trees.
Split the other nodes if necessary.

FAP:UC-BCF

47

M-Way Search Trees


B+ Tree:
Deletion:
1.
2.

3.

Delete the value from the leaf node.


Delete the value also from the nonterminals where it exists
and replace it with a copy of the immediate predecessor.
For each underflowing node
if nonterminal then
follow the procedure for B-Trees
Else
if the nearest right sibling has more than the minimum then
a. move the smallest value in the nearest right sibling into the
underflowing node.
b. Update nonleaf nodes
c. Adjust other nodes if necessary
FAP:UC-BCF

48

M-Way Search Trees


Else
if the nearest left sibling has more than the minimum number of
values then
a. Move the largest value in the nearest left sibling into the
underflowing node
b. Update the nonleaf nodes
c. Adjust otherother nodes
Else
a. Merge the values of the underflowing node with the values of
its nearest right sibling (if non-existent, nearest left sibling)
maintaining the leftmost node label.
b. Update nonleaf nodes

FAP:UC-BCF

49

M-Way Search Trees


B*Tree

Similar to B-Trees except that every node in a B* tree is at


least 2/3 full rather than half-full.
Idea is to reduce the frequency of splitting
When a node overflows, its nearest siblings are checked for
possible accommodation of an overflow value.
If not possible, rather than it being split, a redistribution of
values takes place
Properties:

Interior or nonleaf node with N values contains N+1 links


The root node contains at least 1 value and at most 2(2M-2)/3
values
All non-root nodes contain at least (2M-1)/3 - 1 values and at
most M-1 values.

FAP:UC-BCF

50

M-Way Search Trees


B* Tree:
Insertion:
1.
2.

3.

Insert the value in the leaf node where it should belong.


If the root node overflows, splitting is done in the same way
as in B-Trees.
For each non-root that overflows do
if the nearest left sibling is not full then
a. Move the parent value into the nearest left sibling and
move the smallest value in the overflowing node into the
parent node
b. Adjust nodes if necessary
Else
FAP:UC-BCF

51

M-Way Search Trees


Else
if the nearest right sibling is not full then
a. Move the parent value into the nearest right sibling and
move the largest value in the overflowing node into the
parent node.
b. Adjust nodes if necessary.
Else
a. Take the values of the overflowing node and of its left
(right, if not existing) sibling and their parent value and
distribute then into three nodes with (2M-2)/3, (2M1)/3 , and (2M)/3 values respectively. Old labels will
remain with the leftmost nodes.
b. Adjust nodes if necessary.
FAP:UC-BCF

52

M-Way Search Trees


B* Tree:
Deletion:
1.
2.

3.

Delete the value


If deletion was made from a nonterminal node, then the
immediate predecessor replaces the deleted value.
For each underflowing node do:
check the nearest two siblings (nearest right sibling first) for a
value that can be acquired
if there exists such a value then
a. Rotate values as necessary
b. Adjust nodes if necessary
Else
Merge the values of the underflowing node and of its sibling and their
parent value into the leftmost node.
FAP:UC-BCF

53

Das könnte Ihnen auch gefallen