Tải bản đầy đủ (.ppt) (91 trang)

Slide 4 data and structure in java

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (978.39 KB, 91 trang )

LECTURE 4
DATA
STRUCTURES
PHẦN 1
XÂY DỰNG MỘT SỐ CẤU TRÚC

Linked List

Stack

Queue

Tree
3
Linked List

Linked list là cấu trúc gồm các node liên kết với nhau
thông qua các mối liên kết. Node cuối linked list được đặt
là null để đánh dấu kết thúc danh sách.

Linked list giúp tiết kiệm bộ nhớ so với mảng trong các
bài toán xử lý danh sách.

Khi chèn/xoá một node trên linked list, không phải
dãn/dồn các phần tử như trên mảng.

Việc truy nhập trên linked list luôn phải tuần tự.
4
Linked List

Thể hiện Node thông qua lớp tự tham chiếu (self-


referential class)
class Node
{
private int data;
private Node nextNode;
// constructors and methods
}
15 10
5
Linked List

Một danh sách được quản lý bởi tham chiếu tới
node đầu và node cuối.
H D Q
firstNode lastNode

6
Cài đặt Linked List
// Dinh nghia mot node trong linked list
class Node
{ int data;
Node next;
Node(int value)
{
this(value, null);
}
Node(int value, Node node)
{
data = value;
next = node;

}
int getData() { return data; }
Node getNext() { return next; }
}
7
Cài đặt Linked List
// Dinh nghia lop LinkedList
class DanhSach
{
private Node first;
private Node last;
public DanhSach()
{ first = last = null; }
public boolean isEmpty()
{
return (first == null);
}

public void insertAtFront(int insertItem)
{
if ( isEmpty() )
first = last = new Node( insertItem );
else
first = new Node( insertItem, first );
}
// Dinh nghia mot node trong linked list

class Node
{ int data;
Node next;

Node(int value)
{
this(value, null);
//data = value;
//next = null;
}
Node(int value, Node node)
{
data = value;
next = node;
}
int getData() { return data; }
Node getNext() { return next; }
}
8
Cài đặt Linked List
public void insertAtBack( int insertItem )
{
if ( isEmpty() )
first = last = new Node( insertItem );
else
last = last.next= new Node( insertItem );
}
public int removeFromFront()
{
int removeItem = -1;
if ( ! isEmpty() )
{
removeItem = first.data;
if ( first == last )

first = last = null;
else
first = first.next;
}
return removeItem;
}
9
Cài đặt Linked List
public int removeFromBack()
{
int removeItem = -1;
if ( ! isEmpty() )
{
removeItem = last.data;
if ( first == last )
first = last = null;
else
{
Node current = first;
while ( current.next != last )
current = current.next;
last = current;
last.next =null;
}
}
return removeItem;
}
10
Cài đặt Linked List
public void print()

{
Node node = first;
while (node != null)
{
System.out.print(node.data + " ");
node = node.next;
}
System.out.println("\n");
}
}
11
Mô tả insertAtFront
7 11
firstNode
12
new ListNode
(a )
7 11
firstNode
12
new ListNode
(b)
12
Mô tả insertAtBack
12 7 11
firstNode lastNode
(a)
5
new ListNode
12 11

firstNode lastNode
(b)
5
new ListNode
7
13
Mô tả removeFromFront
firstNode lastNode
(a)
11
firstNode lastNode
(b)
removeItem
12
12
7
7
5
5
11
12
14
Mô tả removeFromBack
5
5
11
7
7
12
12

firstNode lastNode
(a)
firstNode lastNode
(b)
removeItem
current
11
15
Sử dụng Linked List
public class ListTest
{
public static void main( String args[] )
{
DanhSach list = new DanhSach();
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à một cấu trúc theo kiểu LIFO (Last In First
Out), phần tử vào sau cùng sẽ được lấy ra trước.


Hai thao tác cơ bản trên Stack

Chèn phần tử: Luôn chèn vào đỉnh Stack (push)

Lấy ra phần tử: Luôn lấy ra từ đỉnh Stack (pop)
17
Cài đặt Stack
public class Stack
{
private DanhSach stackList;
public Stack()
{
stackList = new DanhSach();
}
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ử dụng Stack
public 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 (Hàng đợi) là cấu trúc theo kiểu FIFO (First In
First Out), phần tử vào trước sẽ được lấy ra trước.

Hai thao tác cơ bản trên hàng đợi

Chèn phần tử: Luôn chèn vào cuối hàng đợi (enqueue)

Lấy ra phần tử: Lấy ra từ đầu hàng đợi (dequeue)
20
Cài đặt Queue
public class Queue
{
private DanhSach queueList;
public Queue()
{
queueList = new DanhSach ();
}

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ử dụng Queue
public 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à một cấu trúc phi tuyến (non-linear).


Mỗi node trên cây có thể có nhiều liên kết tới node khác.
Nút gốc
Nút lá
Nút trong
23
Binary Search Tree

Cây nhị phân là cây mà mỗi node không có quá 2 node
con.

Cây tìm kiếm nhị phân là cây nhị phân mà:

Giá trị các nút thuộc cây con bên trái nhỏ hơn giá trị của nút
cha.

Giá trị các nút thuộc cây con bên phải lớn hơn giá trị của nút
cha.

Duyệt cây nhị phân

Inorder traversal

Preorder traversal

Postorder traversal
24
Binary Search Tree

Ví dụ về Binary Search Tree
47

25 77
11 43 65 93
687 17 31 44
Cây con trái Cây con phải
25
Cài đặt Binary Search Tree
//Cai dat nút
public 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 );
}
}
}

×