IT Data Structures Chapter 3
IT Data Structures Chapter 3
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
FAP:UC-BCF
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
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()
FAP:UC-BCF
13
Pointer Variable
Example:
p
p null
FAP:UC-BCF
10
Garbage
*p = 10
After executing free(p)
P = NULL
14
FAP:UC-BCF
15
16
data
FAP:UC-BCF
link
17
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
FAP:UC-BCF
19
FAP:UC-BCF
20
Trees
Finite set of one or more nodes such that:
FAP:UC-BCF
21
Trees
Terminology:
FAP:UC-BCF
22
Trees
Terminology:
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
FAP:UC-BCF
25
Tree Representations
Using Arrays
- static/sequential representation
0
13
26
Tree Representations
Using linked lists
12
FAP:UC-BCF
15
25
27
FAP:UC-BCF
28
10
8
15
13
20
FAP:UC-BCF
29
FAP:UC-BCF
30
FAP:UC-BCF
31
Tree Traversal
Traversals:
Preorder:
Inorder
Postorder
FAP:UC-BCF
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
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
17
15
11
35
18
21
67
19
39
40
10
FAP:UC-BCF
60 90
55 56
70 80
120
41
FAP:UC-BCF
42
3.
43
3.
44
45
FAP:UC-BCF
46
3.
4.
FAP:UC-BCF
47
3.
48
FAP:UC-BCF
49
FAP:UC-BCF
50
3.
51
52
3.
53