bai8 collections)
Embed Size (px)
TRANSCRIPT
Lp trnh Java c bn
Cao c Thng - Trn Minh Tun [email protected], [email protected]
1
Bi 8. Collections Cu trc d liu trong Java Linked List Stack v Queue Tree
Collections Framework Danh sch (List) Tp hp (Set) Bng nh x (Map)
Bi tp2
Cu trc d liu Cu trc d liu l cch t chc d liu gii quyt vn . Mt s cu trc d liu ph bin: Mng (Array) Danh sch lin kt (Linked List) Ngn xp (Stack) Hng i (Queue) Cy (Tree)
3
Linked List Linked list l cu trc gm cc node lin kt vi nhau thng qua cc mi lin kt. Node cui linked list c t l null nh du kt thc danh sch. Linked list gip tit kim b nh so vi mng trong cc bi ton x l danh sch. Khi chn/xo mt node trn linked list, khng phi dn/dn cc phn t nh trn mng. Vic truy nhp trn linked list lun phi tun t.4
Linked List Th hin Node thng qua lp t tham chiu (self-referential class)class Node { private int data; private Node nextNode; // constructors and methods ... }
1 5
1 05
Linked List Mt linked list c qun l bi tham chiu ti node u v node cui.f s o ir tN de la tN e s od
H
D
.. .
Q
6
Ci t Linked List// Dinh nghia mot node trong linked list class ListNode { int data; ListNode nextNode; ListNode(int value) { this(value, null); } ListNode(int value, ListNode node) { data = value; nextNode = node; } int getData() { return data; } ListNode getNext() { return nextNode; }7
}
Ci t Linked List// Dinh nghia lop LinkedList public class LinkedList { private ListNode firstNode; private ListNode lastNode; public LinkedList() { firstNode = lastNode = null; } public void insertAtFront(int insertItem) { if ( isEmpty() ) firstNode = lastNode = new ListNode( insertItem ); else firstNode = new ListNode( insertItem, firstNode ); }8
Ci t Linked Listpublic void insertAtBack( int insertItem ) { if ( isEmpty() ) firstNode = lastNode = new ListNode( insertItem ); else lastNode = lastNode.nextNode = new ListNode( insertItem ); }
public int removeFromFront() { int removeItem = -1; if ( ! isEmpty() ) { removeItem = firstNode.data; if ( firstNode == lastNode ) firstNode = lastNode = null; else firstNode = firstNode.nextNode; } return removeItem; }
9
Ci t Linked Listpublic int removeFromBack() { int removeItem = -1; if ( ! isEmpty() ) { removeItem = lastNode.data; if ( firstNode == lastNode ) firstNode = lastNode = null; else { ListNode current = firstNode; while ( current.nextNode != lastNode ) current = current.nextNode; lastNode = current; current.nextNode = null; } } return removeItem; }
10
Ci t Linked Listpublic boolean isEmpty() { return (firstNode == null); } public void print() { ListNode node = firstNode; while (node != null) { System.out.print(node.data + " "); node = node.nextNode; } System.out.println("\n"); }
}
11
M t insertAtFront(a)
fi st d r No e 7 n L st d ew i No e 12 11
(b)
fi st d r No e 7 n L st d ew i No e 12 11
12
M t insertAtBack(a)
fi st d r No e
la tN e ne L tN de s od w is o
1 2(b)
7
1 1
5
fi st d r No e
la tN e ne L tN de s od w is o
1 2
7
1 1
5
13
M t removeFromFront(a)
f st od ir N e
l st d a No e
1 2(b)
7
11
5 l st d a No e
f st od ir N e
1 2 re v te mo eI m
7
11
5
14
M t removeFromBack(a)
fi st d r No e
la tN e s od
12(b)
7 cu re r nt
1 1
5 la tN e s od
fi st d r No e
12
7
1 1
5
r o I m em ve te15
S dng Linked Listpublic class ListTest { public static void main( String args[] ) { LinkedList list = new LinkedList(); list.insertAtFront( 5 ); list.insertAtFront( 7 ); list.insertAtBack( 9 ); list.insertAtBack( 8 ); list.insertAtBack( 4 ); list.print(); list.removeFromFront(); list.removeFromBack(); list.print();
}
}
16
Stack Stack l mt cu trc theo kiu LIFO (Last In First Out), phn t vo sau cng s c ly ra trc. Hai thao tc c bn trn Stack Chn phn t: Lun chn vo nh Stack (push) Ly ra phn t: Lun ly ra t nh Stack (pop)
17
Ci t Stackpublic class Stack { private LinkedList stackList; public Stack() { stackList = new LinkedList(); } public void push( int value ) { stackList.insertAtFront( value ); }
}
public int pop() { return stackList.removeFromFront(); } public boolean isEmpty() { return stackList.isEmpty(); } public void print() { stackList.print(); }
18
S dng Stackpublic class StackTest { public static void main(String[] args) { Stack stack = new Stack(); stack.push(5); stack.push(7); stack.push(4); stack.push(8); stack.print(); stack.pop(); stack.pop(); }
stack.print();
}
19
Queue Queue (Hng i) l cu trc theo kiu FIFO (First In First Out), phn t vo trc s c ly ra trc. Hai thao tc c bn trn hng i Chn phn t: Lun chn vo cui hng i (enqueue) Ly ra phn t: Ly ra t u hng i (dequeue)
20
Ci t Queuepublic class Queue { private LinkedList queueList; public Queue() { queueList = new LinkedList(); }
public void enqueue( int value ) { queueList.insertAtBack( value ); }public int dequeue() { return queueList.removeFromFront(); } public boolean isEmpty() { return queueList.isEmpty(); } public void print() { queueList.print(); }
}
21
S dng Queuepublic class QueueTest { public static void main(String[] args) { Queue queue = new Queue(); queue.enqueue(5); queue.enqueue(7); queue.enqueue(4); queue.enqueue(8); queue.print(); queue.dequeue(); queue.dequeue(); queue.print();
}
}
22
Tree Tree l mt cu trc phi tuyn (non-linear). Mi node trn cy c th c nhiu lin kt ti node khc.Nt gc
Nt trong
Nt l23
Binary Search Tree Cy nh phn l cy m mi node khng c qu 2 node con. Cy tm kim nh phn l cy nh phn m: Gi tr cc nt thuc cy con bn tri nh hn gi tr ca nt cha. Gi tr cc nt thuc cy con bn phi ln hn gi tr ca nt cha.
Duyt cy nh phn Inorder traversal Preorder traversal Postorder traversal24
Binary Search Tree V d v Binary Search Tree
4 7Cy con tri Cy con phi
2 5 1 1 7 1 7 4 3 3 4 1 4 6 5 6 8
7 7 9 3
25
Ci t Binary Search Treepublic class TreeNode { int data; TreeNode leftNode, rightNode; public TreeNode( int nodeData ) { data = nodeData; leftNode = rightNode = null; } public void insert( int value ) { if ( value < data ) { if (leftNode == null) leftNode = new TreeNode(value); else leftNode.insert( value ); } else if ( value > data ) { if ( rightNode == null ) rightNode = new TreeNode(value); else rightNode.insert( value ); } } 26
}
Ci t Binary Search Treepublic class Tree { private TreeNode root; public Tree() { root = null; } public void insertNode( int insertValue ) { if ( root == null ) root = new TreeNode( insertValue ); else root.insert( insertValue ); } public void preorderTraversal() { preorder( root ); }
27
Ci t Binary Search Treepublic void inorderTraversal() { inorder( root ); } public void postorderTraversal() { postorder( root ); } private void preorder( TreeNode node ) { if ( node == null ) return; System.out.print( node.data + " " ); preorder( node.leftNode ); preorder( node.rightNode ); }28
Ci t Binary Search Treeprivate void inorder( TreeNode node ) { if ( node == null ) return; inorder( node.leftNode ); System.out.print( node.data + " " ); inorder( node.rightNode ); }
}
private void postorder( TreeNode node ) { if ( node == null ) return; postorder( node.leftNode ); postorder( node.rightNode ); System.out.print( node.data + " " ); }29
S dng Binary Search Treepublic class TreeTest { public static void main( String[] args ) { Tree tree = new Tree(); int value; for ( int i = 1; i