View
78
Download
5
Category
Preview:
DESCRIPTION
Programiranje JavaPMFVladimr Kurbalija
Citation preview
NIZOVI I LISTE
Strukture podataka i algoritmi 1
Osnovne strukture podataka
Nizovi
Java nizovi
Operacije sa nizovima
Povezane liste
Java povezane liste
Operacije sa povezanim listama
Nizovi
Niz je sekvenca indeksiranih elemenata.
Svaki element niza ima fiksni i jedinstveni indeks. Opseg indeksa ide od donje granice do gornje granice:
Veliina niza (broj elemenata) je fiksirana u trenutku kreiranja niza.
Svakom elementu niza je mogue efikasno pristupiti preko indeksa, u konstantnom vremenu O(1).
elementi
indeksi low+1
a
low+2 high1 high low high2
Nnizovi u Javi
Elementi niza u Javi su ili primitivnog tipa ili objekti
neke postojee klase.
Java niz veliine n ima donju granicu 0 a gornju
granicu n1.
Java niz je u sutini objekat. On se dinamiki alocira sa new T[n].
elementi
n1 length 0 1 n2
a n T[]
class tag
Java nizovi elemenata primitivnog tipa
Primer kreiranja, inicijalizovanja i iteriranja kroz niz
celih brojeva:
int[] primes = {2, 3, 5, 7, 11, 13}; for (int i = 0; i < primes.length; i++)
System.out.println(primes[i]);
primes
5 length 0 1 4
2 3 7 11 6 int[]
class tag 2
5 13
3
Java nizovi objekata (1)
Pretpostavimo da objekat Date ima polja y, m, d.
Kreiranje niza Date objekata:
Date[] hols = new Date[3];
length 0 hols 3 Date[]
class tag 1 2
Java nizovi objekata (2)
Kreiranje Date objekata kao elemenata niza:
hols[0] = new Date(2002, 1, 1);
hols[1] = new Date(2001, 5, 1);
hols[2] = new Date(2001, 12, 25);
length 0 1 hols 3 Date[]
class tag 2
1 1 2002 Date
5 1 2001 Date
y m d
12 25 2001 Date
y m d class tag
y m d class tag
class tag
8 0 1 7
a
2 3 4 5 6 9
Podnizovi
Podniz je sekvenca uzastopnih elemenata koja formira deo veeg niza.
Notacija: neka je a[lr] podniz koji se sastoji od elemenata a[l], , a[r].
Napomena: Notacija za podnizove koja je koriena ovde nije podrana u Javi.
Veliina podniza a[lr] je r l + 1.
Podniz a[69] Podniz a[13]
Operacije sa nizovima
Ubacivanje elementa
Brisanje elementa
Traenje elementa
Analiza performansi
Ubacivanje (1)
Problem: Za dat (pod)niz a[leftright], potrebno je ubaciti vrednost val na poziciju a[ins]. Ako je potrebno, pomeriti elemente udesno da se napravi mesto za novi element.
Algoritam:
Ubacivanje vrednosti val na indeks ins u a[leftright] (gde je left ins right):
1. Kopirati podniz a[insright1] u a[ins+1right]. 2. Kopirati vrednost val u a[ins]. 3. Kraj.
Ubacivanje (2)
Animacija:
To insert val at index ins in a[leftright]
(where left ins right):
1. Copy a[insright1] into a[ins+1right].
2. Copy val into a[ins].
3. Terminate.
The
left = 0 1 2 3 4 5 6 = right
cat sat on the mouse mat
fat 1
a
val ins
To insert val at index ins in a[leftright]
(where left ins right):
1. Copy a[insright1] into a[ins+1right].
2. Copy val into a[ins].
3. Terminate.
The
left = 0 1 2 3 4 5 6 = right
cat cat sat on the mouse
fat 1
a
val ins
To insert val at index ins in a[leftright]
(where left ins right):
1. Copy a[insright1] into a[ins+1right].
2. Copy val into a[ins].
3. Terminate.
The
left = 0 1 2 3 4 5 6 = right
fat cat sat on the mouse
fat 1
a
val ins
To insert val at index ins in a[leftright]
(where left ins right):
1. Copy a[insright1] into a[ins+1right].
2. Copy val into a[ins].
3. Terminate.
The
left = 0 1 2 3 4 5 6 = right
fat cat sat on the mouse
fat 1
a
val ins
Ubacivanje analiza
Analiza (brojimo operacije kopiranja):
Neka je n = right left + 1 veliina niza.
Korak 2: 1 kopiranje.
Korak 1: izmeu 0 i n kopiranja, recimo n/2 kopiranja u proseku.
Prosean br. kopiranja = n/2 + 1
Vremenska kompleksnost O(n).
Brisanje (1)
Problem: Za dat (pod)niz a[leftright], obrisati vrednost a[del]. Ako je potrebno, pomeriti elemente ulevo da bi se popunila praznina. (Podrazumevamo left del right.)
Algoritam:
Brisanje vrednosti na indeksu del u a[leftright] (gde je left del right):
1. Kopirati podniz a[del+1right] u a[delright1]. 2. Oznaiti elemenat a[right] kao slobodan. 3. Kraj.
Brisanje (2)
Animacija:
To delete the value at index del in a[leftright]
(where left del right):
1. Copy a[del+1right] into a[delright1].
2. Make a[right] unoccupied.
3. Terminate.
The
left = 0 1 2 3 4 5 6 = right
fat cat sat on the mouse
1
a
del
To delete the value at index del in a[leftright]
(where left del right):
1. Copy a[del+1right] into a[delright1].
2. Make a[right] unoccupied.
3. Terminate.
The
left = 0 1 2 3 4 5 6 = right
cat sat on the mouse mouse
1
a
del
To delete the value at index del in a[leftright]
(where left del right):
1. Copy a[del+1right] into a[delright1].
2. Make a[right] unoccupied.
3. Terminate.
The
left = 0 1 2 3 4 5 6 = right
cat sat on the mouse
1
a
del
To delete the value at index del in a[leftright]
(where left del right):
1. Copy a[del+1right] into a[delright1].
2. Make a[right] unoccupied.
3. Terminate.
The
left = 0 1 2 3 4 5 6 = right
cat sat on the mouse
1
a
del
Brisanje analiza
Analiza (brojimo operacije kopiranja):
Neka je n = right left + 1 veliina niza.
Korak 1: izmeu 0 i n1 kopiranja.
Prosean br. kopiranja = (n 1)/2
= n/2 1/2
Vremenska kompleksnost O(n).
Viedimenzioni nizovi
Nema ugraene podrke u Javi
Koristimo niz nizova
Primer:
int [][] x = new int[3][5]
kreira 4 objekta!
Implementacija viedimenzionih nizova
Memorija je jednodimenzioni niz
Dva pristupa:
Voen vrstama (C, C++)
Voen kolonama (Fortran)
Povezane liste (1)
Povezana lista se sastoji od sekvence vorova povezanih vezama, uz dodatak zaglavlja.
Svaki vor (osim poslednjeg) ima sledbenika, i svaki vor (osim prvog) ima prethodnika.
Svaki vor sadri jedan element (objekat ili vrednost primitivnog tipa), plus vezu ka sledbeniku i/ili prethodniku.
ant bat cat
ant bat cat
zaglavlje null vor element veza
Povezane liste (2)
Veliina povezane liste je broj vorova.
Prazna povezana lista nema vorova.
U povezanoj listi:
Moemo manipulisati pojedinanim elementima.
Moemo manipulisati vezama, i tako menjati
redosled elemenata i strukturu liste! (ovo je
nemogue raditi sa nizovima.)
Jednostruko povezane liste (1)
Jednostruko povezana lista (JPL) se sastoji od sekvence vorova koji su povezani vezama u samo jednom smeru.
Svaki vor JPL sadri jedan elemenat, plus vezu ka voru sledbeniku (ili null ako vor nema sledbenika).
Zaglavlje JPL sadri vezu ka prvom voru (ili null ako je JPL prazna).
pig dog rat cat
dog
Jednostruko povezane liste (2)
Java klasa koja implementira vor JPL:
public class SLLNode {
Object element;
SLLNode succ;
public SLLNode (Object elem, SLLNode succ) {
this.element = elem;
this.succ = succ;
}
}
Jednostruko povezane liste (3)
Java klasa koja implementira zaglavlje JPL:
public class SLL {
SLLNode first;
public SLL () {
// Pravi praznu JPL.
this.first = null;
}
}
Ostali metodi slede
ant bat cat first
Prolazak kroz JPL (1)
Metod (u klasi SLL) koji ispisuje elemente JLP od prvog do poslednjeg:
public void printFirstToLast () { // Stampa sve elemente od prvog do poslednjeg. for (SLLNode curr = this.first;
curr != null; curr = curr.succ)
System.out.println(curr.element);
}
Demonstracija:
ant bat cat first
curr
ant bat cat first
curr
ant bat cat first
curr
ant bat cat first
curr
Prolazak kroz JPL (2)
public void printFirstToLast1 () {
// Stampa sve elemente od prvog do posl.
SLLNode curr = this.first;
while (curr != null){
System.out.println(curr.element);
curr = curr.succ;
}
}
Prolazak kroz JPL (3)
public void printFirstToLastRek (SLLNode curr) {
// Stampa sve elemente od prvog do posl. rekurzivno.
if (curr != null) {
System.out.println(curr.element);
printFirstToLastRek(curr.succ);
}
}
Prolazak kroz JPL (4)
Metod (u klasi SLL) koji ispisuje elemente JLP od
poslednjeg do prvog (rekurzivno):
public void printLastToFirstRek (SLLNode curr) {
// Stampa sve elemente od posl. do prvog rekurzivno.
if (curr != null) {
printLastToFirstRek(curr.succ);
System.out.println(curr.element);
}
}
Prolazak kroz JPL (5)
public void printLastToFirst () {
// Stampa sve elemente od posl. do prvog iterativno.
SLLNode old, next, curr = first;
old = null;
while (curr != null) {
next = curr.succ;
curr.succ = old;
old = curr;
curr = next;
}
Prolazak kroz JPL (5) - nastavak
curr = old;
old = null;
while (curr != null) {
System.out.println(curr.element);
next = curr.succ;
curr.succ = old;
old = curr;
curr = next;
}
}
Operacije sa JPL (1)
Metod (u klasi SLL) koji brie prvi vor JPL:
public void deleteFirst () {
// Brie prvi vor JPL. if (this.first != null)
this.first = this.first.succ;
}
Demonstracija:
ant bat cat first ant bat cat first
Operacije sa JPL (2)
Metod (u klasi SLL) koji brie drugi vor JPL: public void deleteSecond () {
// brie drugi vor JPL.
if (this.first != null &&
this.first.succ != null) { SLLNode second = this.first.succ; this.first.succ = second.succ;
} }
Demonstracija:
ant bat cat first ant bat cat first
second
ant bat cat first
second
Operacije sa JPL (3)
Metod (u klasi SLL) koji zamenjuje prvi i drugi
vor JPL:
public void swapFirstTwo () { // zamenjuje prvi i drugi vor JPL.
if (this.first != null &&
this.first.succ != null) { SLLNode second = this.first.succ; this.first.succ = second.succ; second.succ = this.first; this.first = second;
} }
Demonstracija: ant bat cat first ant bat cat first
second
ant bat cat first
second
ant bat cat first
second
ant bat cat first
second
Ubacivanje
Problem: Ubaciti novi element na datu poziciju u listi.
etiri sluaja:
1) Ubacivanje u praznu listu;
2) Ubacivanje pre prvog vora neprazne liste (na poetak);
3) Ubacivanje iza poslednjeg vora neprazne liste (na kraj);
4) Ubacivanje izmeu vorova neprazne liste (u sredinu).
Za ubacivanje je potrebno imati pokaziva na vor
koji e prethoditi novom voru (pokaziva na vor iza
kojeg e biti ubaen novi).
Ubacivanje u JPL (1)
Algoritam:
Ubaciti elem na datu poziciju u JPL koja poinje sa first:
1. Kreirati novi objekat ins sa elementom elem i vezom null. 2. Ako je potrebno ubaciti novi vor na poetak ili u praznu listu: 2.1. Postaviti vezu od ins na first. 2.2. Postaviti first na ins. 3. Ako je potrebno ubaciti novi vor iza vora na koji pokazuje pred: 3.1. Postaviti vezu od ins na vezu od pred. 3.2. Postaviti vezu od pred na ins. 4. Kraj.
Ubacivanje u JPL(2)
Demonstracija (ubacivanje na poetak JPL):
bat cat first
To insert elem at a given point in the SLL headed by first:
1. Make ins a link to a newly-created node with element
elem and successor null.
2. If the insertion point is before the first node:
2.1. Set node inss successor to first.
2.2. Set first to ins.
3. If the insertion point is after the node pred:
3.1. Set node inss successor to node preds successor.
3.2. Set node preds successor to ins.
4. Terminate.
ant
bat cat first
ins
To insert elem at a given point in the SLL headed by first:
1. Make ins a link to a newly-created node with element
elem and successor null.
2. If the insertion point is before the first node:
2.1. Set node inss successor to first.
2.2. Set first to ins.
3. If the insertion point is after the node pred:
3.1. Set node inss successor to node preds successor.
3.2. Set node preds successor to ins.
4. Terminate.
ant
bat cat first
ins
To insert elem at a given point in the SLL headed by first:
1. Make ins a link to a newly-created node with element
elem and successor null.
2. If the insertion point is before the first node:
2.1. Set node inss successor to first.
2.2. Set first to ins.
3. If the insertion point is after the node pred:
3.1. Set node inss successor to node preds successor.
3.2. Set node preds successor to ins.
4. Terminate.
ant
bat cat first
ins
To insert elem at a given point in the SLL headed by first:
1. Make ins a link to a newly-created node with element
elem and successor null.
2. If the insertion point is before the first node:
2.1. Set node inss successor to first.
2.2. Set first to ins.
3. If the insertion point is after the node pred:
3.1. Set node inss successor to node preds successor.
3.2. Set node preds successor to ins.
4. Terminate.
ant
bat cat first
To insert elem at a given point in the SLL headed by first:
1. Make ins a link to a newly-created node with element
elem and successor null.
2. If the insertion point is before the first node:
2.1. Set node inss successor to first.
2.2. Set first to ins.
3. If the insertion point is after the node pred:
3.1. Set node inss successor to node preds successor.
3.2. Set node preds successor to ins.
4. Terminate.
Ubacivanje u JPL (3)
Demonstracija (ubacivanje u sredinu JPL):
dog fox first
To insert elem at a given point in the SLL headed by first:
1. Make ins a link to a newly-created node with element
elem and successor null.
2. If the insertion point is before the first node:
2.1. Set node inss successor to first.
2.2. Set first to ins.
3. If the insertion point is after the node pred:
3.1. Set node inss successor to node preds successor.
3.2. Set node preds successor to ins.
4. Terminate.
pred
dog fox first
To insert elem at a given point in the SLL headed by first:
1. Make ins a link to a newly-created node with element
elem and successor null.
2. If the insertion point is before the first node:
2.1. Set node inss successor to first.
2.2. Set first to ins.
3. If the insertion point is after the node pred:
3.1. Set node inss successor to node preds successor.
3.2. Set node preds successor to ins.
4. Terminate.
eel pred ins
dog fox first
To insert elem at a given point in the SLL headed by first:
1. Make ins a link to a newly-created node with element
elem and successor null.
2. If the insertion point is before the first node:
2.1. Set node inss successor to first.
2.2. Set first to ins.
3. If the insertion point is after the node pred:
3.1. Set node inss successor to node preds successor.
3.2. Set node preds successor to ins.
4. Terminate.
eel pred ins
dog fox first
To insert elem at a given point in the SLL headed by first:
1. Make ins a link to a newly-created node with element
elem and successor null.
2. If the insertion point is before the first node:
2.1. Set node inss successor to first.
2.2. Set first to ins.
3. If the insertion point is after the node pred:
3.1. Set node inss successor to node preds successor.
3.2. Set node preds successor to ins.
4. Terminate.
eel pred ins
dog fox first
To insert elem at a given point in the SLL headed by first:
1. Make ins a link to a newly-created node with element
elem and successor null.
2. If the insertion point is before the first node:
2.1. Set node inss successor to first.
2.2. Set first to ins.
3. If the insertion point is after the node pred:
3.1. Set node inss successor to node preds successor.
3.2. Set node preds successor to ins.
4. Terminate.
eel
Ubacivanje u JPL (4)
Java metod (u klasi SLL):
public void insert (Object elem, SLLNode pred) {
// Ubacuje elem na datu poziciju u JPL, ili posle cvora // pred, ili pre prvog cvora ako je pred null. SLLNode ins = new SLLNode(elem, null);
if (pred == null) {
ins.succ = this.first;
this.first = ins;
} else {
ins.succ = pred.succ;
pred.succ = ins;
}
}
Ubacivanje u JPL (na poetak)
public void insertFirst (Object elem) {
// Ubacuje elem na pocetak liste
SLLNode ins = new SLLNode(elem, null);
ins.succ = this.first;
this.first = ins;
}
Ubacivanje u JPL (na kraj)
public void insertLast (Object elem) {
// Ubacuje elem na kraj liste
SLLNode ins = new SLLNode(elem, null);
if (first == null) {
first = ins;
} else {
SLLNode temp = first;
while (temp.succ != null)
temp = temp.succ;
temp.succ = ins;
}
}
Brisanje
Problem: Obrisati zadati vor iz liste.
etiri sluaja:
1) Brisanje jedinog vora;
2) Brisanje prvog, ali ne i poslednjeg vora;
3) Brisanje poslednjeg ali ne i prvog vora;
4) Brisanje vora u sredini.
Potrebno je da algoritam pronae vor koji
prethodi voru kojeg briemo.
Brisanje iz JLP(1)
Algoritam:
Brisati vor na koji pokazuje del iz JPL koja poinje sa first:
1. Neka succ pokazuje na vor koji sledi iza del. 2. Ako del = first: 2.1. Postaviti first na succ. 3. Inae (ako del first): 3.1. Neka pred pokazuje na prethodnika od vora del. 3.2. Postaviti vezu od pred na succ. 4. Kraj.
Ali nemamo vezu od vora del do njegovog prethodnika, tako da u koraku 3.1 traimo prethodnika od poetka liste (petlja)!
Brisanje iz JLP (2)
Demonstracija (brisanje prvog vora):
To delete node del from the SLL headed by first:
1. Let succ be node dels successor.
2. If del = first:
2.1. Set first to succ.
3. Otherwise (if del first):
3.1. Let pred be node dels predecessor.
3.2. Set node preds successor to succ.
4. Terminate.
ant bat cat first
del
To delete node del from the SLL headed by first:
1. Let succ be node dels successor.
2. If del = first:
2.1. Set first to succ.
3. Otherwise (if del first):
3.1. Let pred be node dels predecessor.
3.2. Set node preds successor to succ.
4. Terminate.
ant bat cat first
del succ
To delete node del from the SLL headed by first:
1. Let succ be node dels successor.
2. If del = first:
2.1. Set first to succ.
3. Otherwise (if del first):
3.1. Let pred be node dels predecessor.
3.2. Set node preds successor to succ.
4. Terminate.
ant bat cat first
del succ
To delete node del from the SLL headed by first:
1. Let succ be node dels successor.
2. If del = first:
2.1. Set first to succ.
3. Otherwise (if del first):
3.1. Let pred be node dels predecessor.
3.2. Set node preds successor to succ.
4. Terminate.
ant bat cat first
garbage
Brisanje iz JLP(3)
Demonstracija (brisanje unutranjeg ili poslednjeg
vora):
To delete node del from the SLL headed by first:
1. Let succ be node dels successor.
2. If del = first:
2.1. Set first to succ.
3. Otherwise (if del first):
3.1. Let pred be node dels predecessor.
3.2. Set node preds successor to succ.
4. Terminate.
dog eel fox first
del
To delete node del from the SLL headed by first:
1. Let succ be node dels successor.
2. If del = first:
2.1. Set first to succ.
3. Otherwise (if del first):
3.1. Let pred be node dels predecessor.
3.2. Set node preds successor to succ.
4. Terminate.
dog eel fox first
del succ
To delete node del from the SLL headed by first:
1. Let succ be node dels successor.
2. If del = first:
2.1. Set first to succ.
3. Otherwise (if del first):
3.1. Let pred be node dels predecessor.
3.2. Set node preds successor to succ.
4. Terminate.
dog eel fox first
del succ pred
To delete node del from the SLL headed by first:
1. Let succ be node dels successor.
2. If del = first:
2.1. Set first to succ.
3. Otherwise (if del first):
3.1. Let pred be node dels predecessor.
3.2. Set node preds successor to succ.
4. Terminate.
dog eel fox first
del succ pred
To delete node del from the SLL headed by first:
1. Let succ be node dels successor.
2. If del = first:
2.1. Set first to succ.
3. Otherwise (if del first):
3.1. Let pred be node dels predecessor.
3.2. Set node preds successor to succ.
4. Terminate.
dog eel fox first
garbage
Brisanje iz JLP (4)
Analiza:
Neka je n duina JPL.
Korak 3.1 mora posetiti sve vorove od poetka
do predhodnika vora koji se brie. Poseuje
izmeu 0 i n1 vorova.
Prosean broj poseenih vorova = (n 1)/2
Vremenska kompleksnost O(n).
43
Brisanje iz JLP(5)
Java metod (u klasi SLL):
public void delete (SLLNode del) {
// Obrisati cvor na koji pokazuje del iz JPL. SLLNode succ = del.succ;
if (del == this.first) {
this.first = succ;
} else {
SLLNode pred = this.first;
while (pred.succ != del)
pred = pred.succ;
pred.succ = succ;
}
}
Traenje elementa u JPL
public SLLNode find (Object elem) {
// Pronalazi elem u JPL, vraca null ako elem ne postoji
SLLNode curr = first;
while (curr != null && !curr.element.equals(elem))
curr = curr.succ;
return curr;
}
Traenje elementa u JPL rekurzivno
public SLLNode findRek (Object elem) {
// Pronalazi elem u JPL rekurzivno, vraca null
// ako elem ne postoji
return Try(first, elem);
}
public SLLNode Try (SLLNode curr, Object elem) {
if (curr != null)
if (curr.element.equals(elem))
return curr;
else
return Try(curr.succ, elem);
else
return null;
}
Brisanje odreenog elementa
public boolean findAndDelete (Object elem) {
// Pronalazi elem u JPL i brise ga
SLLNode temp = find (elem);
if (temp != null) {
delete(temp);
return true;
} else
return false;
}
Brisanje odreenog elementa bolje
public boolean findAndDelete1 (Object elem) {
// Pronalazi elem u JPL i brise ga
SLLNode temp;
if (first != null)
if (first.element.equals(elem)) {
first = first.succ;
return true;
} else if (first.succ != null) {
temp = first;
while (!temp.succ.element.equals(elem) &&
temp.succ.succ != null)
temp = temp.succ;
if (temp.succ.element.equals(elem)) {
temp.succ = temp.succ.succ;
return true;
}
}
return false;
}
Ureene liste
Elementi su nekog uporedivog tipa podataka (npr. int).
public class SLLNodeS {
int element;
SLLNodeS succ;
public SLLNodeS (int elem, SLLNodeS succ) {
this.element = elem;
this.succ = succ;
}
}
Ubacivanje u ureenu JPL
Elemente ubacujemo tako da lista ostane rastua ili
opadajua
Pronalazimo mesto za novi element i ubacujemo ga
Ako vor sa datim elementom ve postoji:
Ignoriemo novi element (ne dozvoljavamo ponavljanje)
Ubacujemo novi element (dozvoljavamo ponavljanje)
Uvedemo polje duplikat u kome se broje duplikati. Ne
ubacimo novi vor ve samo poveamo polje duplikat
za jedan
Ubacivanje u ureenu JPL
public void findAndInsert (int elem) {
// ubacuje elem na odgovarajuce mesto
// u uredjenoj listi
SLLNodeS ins = new SLLNodeS(elem, null);
if (first == null)
first = ins;
else if (elem = elem) {
ins.succ = temp.succ;
temp.succ = ins;
} else
temp.succ.succ = ins;
}
}
Traenje elementa u ureenoj JPL
public SLLNodeS find (int elem) {
// Pronalazi elem u uredjenoj JPL,
// vraca null ako elem ne postoji
SLLNodeS curr = first;
while (curr != null && curr.element < elem)
curr = curr.succ;
if (curr.element == elem)
return curr;
else
return null;
}
Ureene liste ostale operacije
Prolazak kroz listu identino kao u JPL
Brisanje elementa identino kao u JPL
Metod findAndDelete koristi metod find od
ureene JPL
Metod findAndDelete1 je mogue optimizovati da
ne trai element do kraja liste, nego samo do mesta
gde se moe nai elemenat za izbacivanje. Koristiti
ureenost liste. Za samostalnu vebu...
Recommended