Professional Documents
Culture Documents
DSLK kp
A1 A2 A3 AN
DSLK vng
A1 A2 A3 AN
Cc Tc V
Khi to (init) Kim tra DSLK rng (IsEmpty) Xc nh kch thc (Size) Chn (Insert) Xa (Remove) Tm kim (Retrieve) Thay th (Replace) Duyt (Traverse)
A1
pHead
A2
A3
AN
typedef struct
{ NODE* pHead; NODE* pTail; } LIST;
DSLK n- Cc Tc V Khi to DS
void init (LIST &list){ list.pHead=NULL; list.pTail=NULL; }
DSLK n- Cc Tc V
To mt Node mi cho DS
NODE* getNode(T x){ NODE* p; p=new NODE; if (p==NULL) return NULL; p-> info = x; p-> link=NULL; return p; }
DSLK n- Cc Tc V
Chn mt phn t vo DS
Chn vo u (insertHead) Chn vo cui (insertTail) Chn sau phn t q (insertMid)
DSLK n- Cc Tc V
Chn vo u (insertHead)
pTail pHead
A1
(2) (1)
A2
A3
AN
x
newNode
DSLK n- Cc Tc V
Chn vo u (insertHead)
void insertHead(LIST &ds, NODE* newNode) { if (ds.pHead==NULL) //ds rng { ds.pHead = newNode; ds.pTail = ds.pHead; } else { newNode ->link = ds.pHead; ds.pHead = newNode; } }
DSLK n- Cc Tc V
Chn vo cui (insertTail)
pTail (2)
x
(1)
A1
pHead
A2
A3
AN
DSLK n- Cc Tc V
Chn vo cui (insertTail)
void insertTail(LIST &ds, NODE *newNode) { if (ds.pHead==NULL) { ds.pHead = newNode; ds.pTail = ds.pHead; } else { ds.pTail->link = newNode; ds.pTail = newNode; } }
DSLK n- Cc Tc V
Chn sau phn t q (insertMid)
q pTail
A1
pHead
A2
(2)
A3
(1)
AN
DSLK n- Cc Tc V
Chn sau phn t q (insertMid)
void insertMid(LIST &ds,NODE *q, NODE* newNode) { if ( q!=NULL) { newNode ->link = q-> link; q-> link = newNode; if(q == ds.pTail) ds.pTail = newNode; } else //chn vo u danh sch insertHead(ds, newNode); }
DSLK n- Cc Tc V
Tm mt phn t trong DS NODE * Retrieve(LIST ds, Data k) {
NODE *p; p = ds.pHead; while((p!= NULL)&&(p->info != x)) p = p->link; return p;
DSLK n- Cc Tc V
Duyt DS
void * Traverse(LIST ds) { NODE *p; p = ds.pHead; while(p!= NULL){ process(p); p = p->link; } }
10
DSLK n- Cc Tc V
Xo mt phn t
Xo phn t u Xo phn ft sau phn t q Xo phn t c kho k
DSLK n- Cc Tc V
Xo phn t u
pTail pHead
A1
A2
A3
AN
11
DSLK n- Cc Tc V
Xo phn t u
Data RemoveHead(LIST &ds) { NODE *p; Data x = NULLDATA; if ( ds.pHead != NULL) { p = ds.pHead; x = p->info; ds.pHead = ds.pHead->link; delete p; if(ds.pHead == NULL) ds.pTail = NULL; } return x; }
DSLK n- Cc Tc V
Xo phn ft sau phn t q
q
pHead
p
A2 A3
pTail
A1
AN
12
DSLK n- Cc Tc V
Xo phn t sau phn t q
void RemoveAfter (LIST &ds, NODE *q) { NODE *p; if ( q != NULL) { p = q ->link ; if ( p != NULL) { if(p == ds.pTail) ds.pTail = q; q->link = p->link; delete p; } } else RemoveHead(ds); }
DSLK n- Cc Tc V
Xo phn t c kho k int RemoveNode(LIST &ds, Data k) { NODE *p = ds.pHead; NODE *q = NULL; while( p != NULL) { if(p->info == k) break; q = p; p = p->link; } if(p == NULL) return 0; //Khng tm thy k if(q != NULL) { if(p == ds.pTail) ds.pTail = q; q->link = p->link; delete p; } else //p l phn t u ds { ds.pHead = p->link; if(ds.pHead == NULL) ds.pTail = NULL; } return 1; }
13
typedef struct DNode { T info; struct DNode* pPre; struct DNode* pNext; }DNODE;
14
15
16
17
STACK
21 56 31 29 179 52
Bottom_of_stack (this is where the stack started) Direction in which stack grows Empty/unfilled portion of stack
Danh sch hn ch Cc phn t c thm vo v ly ra nh stack Hin thc dng dslk hoc array
top
Stack Tc v
Init() Push() Pop() IsEmpty() IsFull()
typedef struct { T *theArray; int top; int size; }STACK;
void init (STACK &s, int size) s.size = size; s.theArray = new T[size]; s.top = -1; } {
18
Stack- Push()
void push(STACK &s, T x){ if (!isFull(s)){ s.top++; s.theArray[s.top]=x; } } bool isFull(STACK s) { if(s.top < s.size-1) return false; else return true; }
19
Stack-V d
21 56 31 29 179 52
pop()
21 56 31 29 179
push(2)
21 56 31 29 179 2
top()
21 56 31 29 179 2
top top
top
Return 52
Return 2
Queue
12 31 79 front 5 back 63 empty portion of the queue
Danh sch hn ch Chn vo mt u, ly ra u kia Hin thc dng dslk hoc array Linear and Circular Queues
20
Queue-Tc v
EnQueue()
Input: element to be enqueued Algorithm:
increment back by 1 add element to the empty location pointed to by back
Return: void
DeQueue()
Input: void Algorithm:
return the element pointed to by front increment front by 1
Queue V d
12 31 79 5 63 12 31 79 front 17 31 79 5 enqueue(17) 63 back 17 front back dequeue 31 79 5 63 front back dequque 79 5
63 17 52 front back
21
Queue V d
79 5 63 17 front back 52 79 dequeue 5 dequeue 17 52 front 23 back 63 17 52 23 front back 63 5 enqueue(23) 63 front 17 52 23 back dequeue
17
52
23
Circular Queue
22
Circular Queue
In enqueue:
back = (back +1) mod ARRAY_SIZE
In dequeue:
front = (front + 1) mod ARRAY_SIZE
Circular Queue
22 71 57 81 1 11 39 back front EnQueue -> Queue full: (back+1)%size == front 8 front back
23
Circular Queue
typedef struct {
int size; int front; int back; T* values;
} QUEUE;
Circular Queue
void init(QUEUE &q, int size) { q.size = size; q.values = new T[size]; q.front = 0; q.back = 0; }
24
Circular Queue
bool isFull(QUEUE q) { if( (q.back+1) % q.size == q.front) return true; else return false; } bool isEmpty(QUEUE q) { if(q.back == q.front) return true; else return false; }
Circular Queue
void enqueue(QUEUE &q,T x) { if(!isFull(q)) { q.back = (q.back+1) % q.size; q.values[q.back] = x; } } T dequeue(QUEUE &q) { if(!isEmpty(q)) { q.front = (q.front+1) % q.size; return values[q.front]; } return NULLDATA; }
25
CY
Path: n1->nk l mt chui cc nt n1 ->nk sao cho ni l cha ni+1, 1 <= i <=k root depth subtree
edge
C T2
E T4
G T1
I T3
K T5
leaf
CY
Cy nh phn l cy m mi nt c ti a hai nt con
root depth
C T2
E T4
G T1
I T3
K T5
leaf
26
CY NH PHN
CC PHP DUYT
PreOrder
Duyt gc Duyt cc cy con bn tri Duyt cy con bn phi
InOrder
Duyt cy con bn tri Duyt gc Duyt cy con bn phi
PostOrder
Duyt cy con bn tri Duyt cy con bn phi Duyt gc
27
CY NH PHN- CTDL
typedef struc TNode{ T info; TNode* left; TNode* right; }TNODE; TNODE *root=NULL;
CY NH PHN- Duyt
void PreOrder(TNODE *root){ if (root!=NULL){ process(root); PreOrder(roo->left); PreOrder(root->right); } } void InOrder(TNODE *root){ if (root!=NULL){ InOrder (root->left); process(root); InOrder (root->right); } } void PostOrder(TNODE *root){ if (root!=NULL){ PostOrder (root->left); PostOrder (root->right); process(root); } }
28
V D
Cho cy nh phn, mi nt c info (khng trng nhau) l mt s nguyn
m s nt trn cy Tnh tng cc info trn cy Cho bit t tin ca nt c info l x Cho bit info cc nt mc 3 Cho bit tng info trn cy Cho bit cc nt c tng info cc conl bi s ca info ca cha n Cho bit tng s nt l Cho bit tng s nt bc mt Cho bit cy c b suy bin khng
Hi-p
Hi a b Hi
29
BST-Retrieval
TNODE* Retrieval(TNODE* root, T x){ if (root != NULL) { If (root->info==x) return root; if (x < root->info) return Retrieveal (root->left, x); else return Retrieveal (root->right, x); } return NULL; }
30
BST-Retrieval
BST-Insert
To BSTvi cc info: 5 9 7 3 8 12 6 4 20
31
BST-Insert
BST-Insert
32
BST-Insert
BST-Insert
int Insert(TNODE*& root, T x) { if (root != NULL) { if (root->info==x) return 0; if (x < root ->info) return Insert(root ->left, x); else return Insert(root ->right, x); } root = new TNODE; If (root==NULL) return -1 root ->right = NULL; root ->left = NULL: root ->info = x; return 1; }
33