Date post: | 24-Feb-2018 |
Category: |
Documents |
Upload: | romeofatima |
View: | 224 times |
Download: | 1 times |
of 53
7/25/2019 IT Data Structures Chapter 3
1/53
FAP:UC-BCF 1
dvanced Data Structures
Chapter III:
Data Structures
7/25/2019 IT Data Structures Chapter 3
2/53
FAP:UC-BCF 2
Contents
PointersTrees
- Binary Trees
- Traversals- Binary Search Trees
- AVL Trees
- M-way Search Trees
7/25/2019 IT Data Structures Chapter 3
3/53
FAP:UC-BCF 3
Static vs. Dynamic Allocation
Static Allocation:- Binds memory space to variables at compile time.- Deals with variables that are created statically (compile time).
Examples:
i nt i , j ;
char s[ 10] ;
f l oat k;
doubl e d;
7/25/2019 IT Data Structures Chapter 3
4/53FAP:UC-BCF 4
Static vs. Dynamic Allocation
Dynamic Allocation:- Binds memory spaces to variables during run-time.- Deals with data structures whose length is only determinedduring run-time.
- Deals with variables that are created and disposed duringexecution.
Example: Pointers
7/25/2019 IT Data Structures Chapter 3
5/53FAP:UC-BCF 5
Pointer Variable
A variable used only to store the memory address ofanother variable.
Used to access dynamic variables
Value of a pointer variable-address
7/25/2019 IT Data Structures Chapter 3
6/53FAP:UC-BCF 6
Pointer Variable
Example:
Charptr at addr Dynamic Char Var at
07770 Address 0122222
Charptr at addr Dynamic Char Var at
07770 Address 0122222
0122222 A
A
7/25/2019 IT Data Structures Chapter 3
7/53FAP:UC-BCF 7
Pointer Variable
Declaration:
Syntax: *
where:
is any user defined name. is any simple C data type
Examples:
i nt *i ;f l oat *f ;
char *c;
7/25/2019 IT Data Structures Chapter 3
8/53FAP:UC-BCF 8
Pointer Variable
Another way:
Syntax:
typedef *
typedef Example:
t ypedef i nt *poi nt er ;
mai n( ){ poi nt er i ;
.
.
}
7/25/2019 IT Data Structures Chapter 3
9/53
FAP:UC-BCF 9
Pointer Variable
Operations:
Function malloc()
- syntax: malloc()- uses stdlib.h
- the function call returns a pointer to enough
storage for an object of bytes. The storageis not initialized.
7/25/2019 IT Data Structures Chapter 3
10/53
FAP:UC-BCF 10
Pointer Variable
Example:
mai n( )
{
i nt *p;
.
.
p = mal l oc( si zeof ( i nt ) ) ;
}
7/25/2019 IT Data Structures Chapter 3
11/53
FAP:UC-BCF 11
Pointer Variable
The execution of malloc() would obtain a piece ofmemory (dynamic variable or node) from the systemadequate to store an integer value and assigns itsaddress to the pointer p.
Accessing a dynamic variable:
Syntax: *
the thing pointed to by
7/25/2019 IT Data Structures Chapter 3
12/53
FAP:UC-BCF 12
Pointer Variable
Example:
p = mal l oc( si zeof ( i nt ) ) ;
*p = 10;
pr i nt f ( %d, *p) ;
Gar bage
10
p
p
7/25/2019 IT Data Structures Chapter 3
13/53
FAP:UC-BCF 13
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
available once again to the system. Syntax: free()
Example:
f r ee( p) ;
7/25/2019 IT Data Structures Chapter 3
14/53
FAP:UC-BCF 14
Pointer Variable
Example:
10p *p = 10
pGar bage
Af t er execut i ng f r ee( p)
nul lp P = NULL
7/25/2019 IT Data Structures Chapter 3
15/53
FAP:UC-BCF 15
Dynamic Data Structures
We do not use pointers and dynamic variables bythemselves. Like structures that can be grouped tobecome array of records, pointers and dynamicvariables are normally grouped into something called
asLink List.
Basic link list singly linked list
7/25/2019 IT Data Structures Chapter 3
16/53
FAP:UC-BCF 16
Singly Linked List
A sequence of dynamic variables or nodes that arelinked together
Declaration:
st r uct
{
;
.
.
;
}
Note: one of the fields must be a pointer type that would point to astructure that is of the same type as the one that contains it.
7/25/2019 IT Data Structures Chapter 3
17/53
FAP:UC-BCF 17
Singly Linked List
Example:
st r uct node
{
i nt dat a;
st r uct node *l i nk;
}
dat a l i nk
7/25/2019 IT Data Structures Chapter 3
18/53
FAP:UC-BCF 18
Singly Linked List
Note:
The linked list terminates with a node whose link field value isNULL
NULL is a predefined constant pointer which means, pointingto nothing.
1 22 30
f i r s t
7/25/2019 IT Data Structures Chapter 3
19/53
FAP:UC-BCF 19
Singly Linked List
Declaration:#i ncl ude
st r uct node
{
i nt dat a;st r uct node *l i nk;
};
t ypedef st r uct node nodet ype;
t ypedef nodet ype *poi nt er ;
7/25/2019 IT Data Structures Chapter 3
20/53
FAP:UC-BCF 20
Singly Linked List
voi d cr eat e_si ngl e( poi nt er *f i r st )
{ i nt num; poi nt er x, p;*f i r st = NULL;
do { scanf ( %d, &num) ;
i f ( num ! = - 1)
{ x = mal l oc( si zeof ( nodet ype) ) ;
x - > dat a = num;
x - > l i nk = NULL;
i f ( *f i r st = NULL) *f i r st = x;
el se p - > l i nk = x;
p = x;}
}whi l e( num ! = - 1) ;
}
mai n( )
{ poi nt er f i r st ;
cr eat e_si ngl e( &f i r st ) ; }
7/25/2019 IT Data Structures Chapter 3
21/53
FAP:UC-BCF 21
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>=0disjoints sets T1, T2, Tn, where each set is a tree
called subtrees of the root.
7/25/2019 IT Data Structures Chapter 3
22/53
FAP:UC-BCF 22
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 rootto 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
7/25/2019 IT Data Structures Chapter 3
23/53
FAP:UC-BCF 23
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
7/25/2019 IT Data Structures Chapter 3
24/53
FAP:UC-BCF 24
Binary Trees
Finite set of nodes which is either empty or
consisting of a root with at most two branches todisjoint binary trees called the left subtree and theright subtree.
A
B
A
B
7/25/2019 IT Data Structures Chapter 3
25/53
FAP:UC-BCF 25
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 ofheigh k.
Skewed binary tree: tree that either have 0 leftsubtrees or 0 right subtrees.
Full binary tree of depth k: a binary tree of depth kwith 2k-1 nodes.
7/25/2019 IT Data Structures Chapter 3
26/53
FAP:UC-BCF 26
Tree Representations
Using Arrays
- static/sequential representation
0
1 2
3 4 5 6
913
Array size needed will be 2k-1, k is the height of a given tree
7/25/2019 IT Data Structures Chapter 3
27/53
FAP:UC-BCF 27
Tree Representations
Using linked lists
1
12 15
5 8 25
7/25/2019 IT Data Structures Chapter 3
28/53
FAP:UC-BCF 28
Binary Search Tree
A binary tree:
All identifiers in the left subtree are less than theidentifier in the root.
All identifiers in the right subtree are greater thanthe identifier in the root.
The left and right subtrees are also binary searchtrees.
7/25/2019 IT Data Structures Chapter 3
29/53
FAP:UC-BCF 29
Binary Search Tree
Examples:
10
8 15
13 204
K
B Q
E
D J
7/25/2019 IT Data Structures Chapter 3
30/53
FAP:UC-BCF 30
Binary Search Tree
Algorithm: Insertion
If the tree is empty, then insert the new item in thetrees root node.
If the roots key matches the new items key thenskip 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 rightsubtree.
7/25/2019 IT Data Structures Chapter 3
31/53
FAP:UC-BCF 31
Binary Search Tree
Algorithm: Searching
If the tree is empty, then the target key is not inthe tree.
If the target key is found in the root item then thetarget key is found in the root item.
If the target key is smaller than the roots key thensearch the left subtree, otherwise search the right
subtree.
7/25/2019 IT Data Structures Chapter 3
32/53
FAP:UC-BCF 32
Tree Traversal
Traversals:
Preorder: Data Left Tree Right Tree
Inorder Left Tree- Data Right Tree
Postorder Left Tree Right Tree - Data
7/25/2019 IT Data Structures Chapter 3
33/53
FAP:UC-BCF 33
Tree Traversal
Give the preorder, inorder and postorder traversals:Tree #1 Tree #2
C E G
F
DB
A
D
F H Z
GE
B C
A
7/25/2019 IT Data Structures Chapter 3
34/53
FAP:UC-BCF 34
Counting Trees
Create the corresponding counting trees with the
following traversals:1. Pre: IAMHEDBCFL
Post: EHDMALFCBI
In: AHEMDICFLB2. Pre: ABDGCEHIF
In: DGBAHEICF
3. Post: CBFEGDAIn: CBAEFDG
4. Post: FABG/+CD - ^*
In: F/AGB*+^C-D
7/25/2019 IT Data Structures Chapter 3
35/53
FAP:UC-BCF 35
AVL Trees
Introduced by Adelson-Velskii and Landis in 1962
Height-balanced binary search trees
Binary search trees in which the heights of thesubtrees of each node differ by at most one level.
Insertion of deletion of nodes causes the search treeto be rebalanced.
7/25/2019 IT Data Structures Chapter 3
36/53
FAP:UC-BCF 36
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 theleft child of A as B; if inserted in the right subtree of A, takethe 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 theright 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.
7/25/2019 IT Data Structures Chapter 3
37/53
FAP:UC-BCF 37
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 equalprioritize 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. Rebalance if needed.
7/25/2019 IT Data Structures Chapter 3
38/53
FAP:UC-BCF 38
AVL Trees
Examples: Create AVL trees with the following insertion and
deletion operations. Each number is independent of the othernumbers.
1. Ins: M, T, O, Q, S, R, W, V, Y, U, C, G, E, X
2. Ins: 10, 20, 30, 40, 50, 60, 703. 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
7/25/2019 IT Data Structures Chapter 3
39/53
FAP:UC-BCF 39
AVL Trees14
20
7
19
6721181542
351793
5
1161
Del: 35, 14, 5, 11, 15, 17, 20Ins: 14, 10, 13, 28
Del: 1, 67, 9Ins: 35, 15, 20
7/25/2019 IT Data Structures Chapter 3
40/53
FAP:UC-BCF 40
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 byallowing the nodes to store more than one key value.This leads to shorter in height and smaller number ofcomparisons.
M-way or Multiway Search Trees: A kind of searchtree in which each node can store 1 to M-1 values 1 to
M links to subtrees.
7/25/2019 IT Data Structures Chapter 3
41/53
FAP:UC-BCF 41
M-Way Search Trees
Example:
50
60 9030
70 80 12055 5610
7/25/2019 IT Data Structures Chapter 3
42/53
FAP:UC-BCF 42
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+1links
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-1values
7/25/2019 IT Data Structures Chapter 3
43/53
FAP:UC-BCF 43
M-Way Search Trees
B-Tree:
Insertion:1. Insert the value in the leaf node where it should belong.
2. If the number of values in any node exceeds the maximum,
that is, the node overflows, the middle value is extracted andis moved to the parent node (if not existing, create one with anew label) and among the remaining values, the lower halfwill remain in the node and the upper half will be stored in a
new node with a new label. In case there are two middlevalues, extract the smaller value. Labeling new nodes is donefrom left to right from the lowest level going up.
3. Split the other nodes if necessary
7/25/2019 IT Data Structures Chapter 3
44/53
FAP:UC-BCF 44
M-Way Search Trees
B-Tree:
Deletion:1. Delete the value.
2. If deletion was made from a nonterminal node, the immediate
predecessor replaces the deleted value. The immediatepredecessor of a node is the largest value in the nodes leftsubtree while the immediate successor of a node is thesmallest value in the nodes right subtree.
3. For each node that underflows, that is, a node where thenumber of values is less than the minimum do:
If the nearest right sibling has more than the minimum numberof values then
7/25/2019 IT Data Structures Chapter 3
45/53
FAP:UC-BCF 45
M-Way Search Trees
a. Move the parent value into the underflowing node and movethe smallest value in the nearest right sibling into the parentnode.
b. Adjust other nodes if necessary
Else
if the nearest left sibling has more than the minimum number ofvalues then,a. Move the parent value into the underflowing node and movethe largest value in the nearest left sibling into the parent node.
b. Adjust other nodes if necessary
Elsea. Merge the values of the underflowing node, the values of thenearest right sibling (if non-existent, nearest left sibling), andtheir parent node into the leftmost node.
b. Adjust node if necessary
7/25/2019 IT Data Structures Chapter 3
46/53
FAP:UC-BCF 46
M-Way Search Trees
B+Tree
Similar to B tree except that the interior or nonleaf nodescontain only keys and links; and leaf nodes which contain
keys and addresses are linked in key sequence to facilitaterapid sequential accessing
Properties similar to that of B Tree.
7/25/2019 IT Data Structures Chapter 3
47/53
FAP:UC-BCF 47
M-Way Search Trees
B+ Tree:
Insertion:1. Insert the value in a leaf node where it should belong.
2. If the number of values in a leaf node overflows, the M/2 th
value is replicated in the parent node and among theremaining values, the firstM/2 values will remain in thenode while the remaining nodes will be stored in a new nodewith a new label.
3. If the number of values in a nonleaf overflows, splitting isdone in the sam way that is done with B-Trees.
4. Split the other nodes if necessary.
7/25/2019 IT Data Structures Chapter 3
48/53
FAP:UC-BCF 48
M-Way Search Trees
B+ Tree:
Deletion:1. Delete the value from the leaf node.
2. Delete the value also from the nonterminals where it existsand replace it with a copy of the immediate predecessor.
3. For each underflowing node
if nonterminal then
follow the procedure for B-Trees
Elseif the nearest right sibling has more than the minimum then
a. move the smallest value in the nearest right sibling into theunderflowing node.
b. Update nonleaf nodes
c. Adjust other nodes if necessary
7/25/2019 IT Data Structures Chapter 3
49/53
FAP:UC-BCF 49
M-Way Search Trees
Else
if the nearest left sibling has more than the minimum number ofvalues then
a. Move the largest value in the nearest left sibling into theunderflowing node
b. Update the nonleaf nodesc. 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
7/25/2019 IT Data Structures Chapter 3
50/53
FAP:UC-BCF 50
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.
7/25/2019 IT Data Structures Chapter 3
51/53
FAP:UC-BCF 51
M-Way Search Trees
B* Tree:
Insertion:1. Insert the value in the leaf node where it should belong.
2. If the root node overflows, splitting is done in the same way
as in B-Trees.3. 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 andmove the smallest value in the overflowing node into theparent node
b. Adjust nodes if necessary
Else
7/25/2019 IT Data Structures Chapter 3
52/53
FAP:UC-BCF 52
M-Way Search Trees
Else
if the nearest right sibling is not full thena. Move the parent value into the nearest right sibling andmove the largest value in the overflowing node into theparent 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 anddistribute then into three nodes with (2M-2)/3, (2M-1)/3 , and (2M)/3 values respectively. Old labels willremain with the leftmost nodes.
b. Adjust nodes if necessary.
7/25/2019 IT Data Structures Chapter 3
53/53
FAP:UC-BCF 53
M-Way Search Trees
B* Tree:
Deletion:1. Delete the value
2. If deletion was made from a nonterminal node, then theimmediate predecessor replaces the deleted value.
3. For each underflowing node do:
check the nearest two siblings (nearest right sibling first) for avalue that can be acquired
if there exists such a value then
a. Rotate values as necessary
b. Adjust nodes if necessary
ElseMerge the values of the underflowing node and of its sibling and their
parent value into the leftmost node.