ที่เราพูดถึงก่อนหน้านั้น...
TRANSCRIPT
BST ทเราพดถงกอนหนานนสามารถมลกไดสงสดเพยงแคสอง node แตถาเรายอมใหลกของ node ใน tree มไดมากกวาสอง เราเรยก tree ลกษณะนวา multiway tree1 ในบทนเราจะมาท าความรจก 2-3-4 Tree ซงเปน multiway tree ทสามารถมลกไดสงสด 4 node และในแตละ node สามารถมขอมลไดสงสด 3 ขอมล หลงจากจบบทเรยนนแลวผอานจะไดทราบถง o นยาม และโครงสรางของ 2-3-4 Tree o การน าขอมลเขาส 2-3-4 Tree o การคนหาขอมลใน 2-3-4 Tree o การแบง (split) node เพอรองรบการน าขอมลเขา 9.1 นยาม ตวเลข 2, 3, และ 4 ใน 2-3-4 Tree เปนตวบงบอกถงจ านวนของ node ทแตละ node สามารถมได ดงขอก าหนดน
o Node ทมขอมลภายในเพยง 1 ขอมลสามารถมลกไดแค 2 node o Node ทมขอมลภายใน 2 ขอมลสามารถมลกไดแค 3 node o Node ทมขอมลภายใน 3 ขอมล (ซงเปนจ านวนสงสด) มลกได 4 node
พดงาย ๆ กคอ node ทไมใช leaf node สามารถมลกไดมากกวาจ านวนของขอมลทมอยภายใน node อก 1 node (leaf node ไมมลก ตามขอก าหนดของ tree อยแลว) 9.2 โครงสรางของ 2-3-4 Tree โครงสรางของ 2-3-4 Tree ตองรกษาไวซ งขอก าหนดของ BST คอ ขอมลทมคานอยกวาจะอยทางดานซายของ และขอมลทมคามากกวาจะอยทางดานขวา แตจะมขอก าหนดเพมข นดงน
o ลกทกตวของ node ในต าแหนง 0 จะมคานอยกวาขอมล ณ ต าแหนง 0 o ลกทกตวของ node ในต าแหนง 1 จะมคานอยกวาขอมล ณ ต าแหนง 1 แตจะมคา
มากกวาขอมล ณ ต าแหนง 0 o ลกทกตวของ node ในต าแหนง 2 จะมคานอยกวาขอมล ณ ต าแหนง 2 แตจะมคา
มากกวาขอมล ณ ต าแหนง 1 o ลกทกตวของ node ในต าแหนง 3 จะมคามากกวาขอมล ณ ต าแหนง 2
โดยเราไดก าหนดใหขอมลทอยภายใน node มต าแหนงเรมตนท 0 ไปจนถง 2 (0, 1, 2) และลกของ node แตละ node มต าแหนงตงแต 0 ถง 3 (0, 1, 2, 3) ดงภาพตวอยางท 9.1
1 Multiway tree ทนยมใชและพดกนมากในหนงสอโครงสรางขอมลโดยทวไปคอ B-Tree ซง 2-3-4 Tree ทเราพดถง
ในบทนกเปน B-Tree ชนดหนง
2-3-4 Tree บทท 9
240
ภาพท 9.1 โครงสรางของ 2-3-4 Tree เมอมลกตามขอก าหนด
ใน 2-3-4 Tree เราไมตองกงวลเรองขอมลทซ ากนเนองจากวาเปนขอก าหนด (ตามลกษณะขอบงคบของการน าขอมลเขา)
ภาพท 9.2 ความเปนไปไดของ node เมอมจ านวนของขอมลทากบ 1, 2, หรอ 3
0 1 2
2
30 50 70
20 10 90 80
1 0
40 60
3
มลกได 4 node
0 1 2
50
30 70 60
1 0
0 1 2
2
30 50
20 10 80 70 60
1 0
40
มลกได 2 node
มลกได 3 node ลกทตอเชอมกบ node 0 จะมคานอยกวาขอมลท
ต าแหนง 0
ลกของ Node 3 ทกตวจะมคามากกวาขอมลใน
ต าแหนงท 2
A
X Y A B
W X Y
A B C
W X Y Z
2-node
3-node
4-node
บทท 9 2-3-4 Tree
241
9.3 การน าขอมลเขา การน าขอมลเขาส 2-3-4 Tree นนจะตองน าขอมลเขาท leaf node เสมอและมขอควรค านงทส าคญคอ ขอมลทน าเขาถาจ าเปนทจะตองไปอยใน node ทเตมแลว เชน น าขอมล 55 เขาส tree ในรปดานลางของภาพท 9.1 เราจ าเปนทจะตองหาวธการทเมอน าขอมลเขาแลว เรายงรกษาโครงสรางของ 2-3-4 Tree ไวได ซงวธการทเหมาะสมทสดกคอการแบง node 9.3.1 การแบง node (split) หลงจากทเราหาต าแหนงของขอมลทตองการน าเขาไดแลว ประกอบกบ node ทหาต าแหนงเจอเปน node ทเตมแลว เราตองท าการแบง node ออกเปนสวน ๆ เพอรกษาโครงสรางของ 2-3-4 Tree ใหเปนไปตามขอก าหนด ซงในการแบง node นนเราตองค านงถง node ทเปน root และ node อน ๆ ทไมใช root เราจะมาดกนถงวธการแบง node ทไมใช root2 กอน ซงมขอก าหนดดงน
o สราง node ใหมส าหรบรองรบขอมลในต าแหนงท 2 ของ node ทถกแบง o ยายขอมลในต าแหนงท 2 เขาส node ใหม o ยายขอมลในต าแหนงท 1 เขาส node แมของ node ทถกแบง o ขอมลในต าแหนง 0 อยทเดม o ยกเลกการเชอมตอของ node ลกทงสองของ node ทถกแบง พรอมกบน าเอาลกทง
สองไปเชอมกบ node ใหมแทน
ภาพท 9.3 2-3-4 Tree กอนการน าขอมลเขา
ขอมลทเราตองน าเขา (49) นนโดยขอก าหนดของ BST แลวจะตองอยใน node ทม (41, 46, 52) แตเราท าไมไดเพราะวา node นเตมแลวดงนนเราจงตองแบง node นออกเปนสอง node ท าการยาย 46 เขาส node ทอยดานบน (parent) น า 52 เขาส node ใหม (node ทถกแบง) หลงจากนนกน า 49 เขาส node ทอยดานลางของ node ใหมน (ภาพท 9.4 แสดงต าแหนงทถกตองตามความสมพนธแตผดเงอนไขของ 2-3-4 tree และภาพท 9.5 แสดง 2-3-4 tree ทถกตอง)
2 ดภาพประกอบท 9.3 และ 9.4
0 1 2
31
37
52 46 41
44 43 48 56
2-3-4 Tree บทท 9
242
ภาพท 9.4 ต าแหนงของ 49 เนองมาจากความสมพนธของขอมล (เปนการน าขอมลเขาทผดเงอนไข)
ภาพท 9.4 ต าแหนงทถกตองของ 49 ของ 2-3-4 Tree หลงจากการโยกยายขอมลและแบง node
ตวอยางการแบง node ทเหนเปนการแบงทเกดข นท node ๆ เดยวเทานน ในการคนหา node ส าหรบการน าขอมลเขานนเราอาจเจอ node ทมขอมลอยเตมมากกวาหนง node ซงจะท าให เกดการแบง node เพมข นหลายทใน tree
ขอสงเกตของการแบง node นนเราจะเหนวาส งทเกดข นกคอ 1) เราไดแบง node 4 node ออกเปน 2 node พรอมกบยายขอมลไปส node ดานบน (parent node) และ 2) ยายขอมลไปทางขวา ส าหรบการแบง node ทเปน root นนมความยงยากเพมข นอกเลกนอย3 ดงน
o ตองสราง node ใหมแทน root o ตองสราง node ใหมส าหรบเกบขอมลของ node ทถกแบง
3 ดภาพประกอบท 9.5 และ 9.6
0
1
2
31 46
37
41
node ใหม
44 43 49 48 56
46 เลอนข น
52
52 เลอนขวา (ขอมลมากจะ
อยทางขวา)
41 อยทเดม (ขอมลนอยจะอยทางซาย)
49 เขาสต าแหนงท
เหมาะสม
46
0 1 2
31
37
49 41
44 43 48 56
52
3
49 ตองอยในต าแหนงนตามขอก าหนดของ BST แตการกระท านผดเงอนไขของ 2-3-4 tree ทบอกวา ขอมลมไดสงสด
แค 3 ตว
บทท 9 2-3-4 Tree
243
o ยายขอมลในต าแหนง 2 ไปยง node ใหม o ยายขอมลในต าแหนง 1 ไปยง root ใหม o สวนขอมลในต าแหนง 0 กยงคงอยทเดม
ภาพท 9.6 2-3-4 Tree กอนการแบง root
ภาพท 9.7 2-3-4 Tree หลงจากการแบง root และการน าขอมลเขา
สงทเกดข นเมอเราแบง root กคอ เราตองสราง node ใหมสองตวโดยทตวแรกจะเปน root ใหมทเกบขอมลในต าแหนงท 1 ของ root เกาไว ตวทสองจะเปนตวเกบขอมลทมคามากทสดของ root เกา (นอยอยซาย มากอยขวา) เมอได node ทงสองแลวเราตองท าการตอเชอม node เหลานใหม โดยเราจะให root ใหมเปน parent ของทงสอง node 9.4 Java code ส าหรบ 2-3-4 Tree
ในการออกแบบโครงสรางการเกบขอมลของ 2-3-4 Tree ของเรานนกคลาย ๆ กบการออกแบบโครงสรางของ tree ทเราไดศกษากนในบททเจด แตกจะมสวนอน ๆ ทท าใหการใชงานของ code ท าไดงายขน กอนทเราจะมาดกนถง method หลก ๆ ทใชในการน าขอมลเขาส 2-3-4 Tree ของเรา ส งส าคญอนดบแรกทตองท าความเขาใจกคอ class Node
0 1 2
6 4
36 24 13
node ทถกแบง
17 15 30 26 41
ขอมล 20 ทตองน าเขา
36 เลอนขวา 0
1
2
6 4
24
root ใหมทสรางข น
20 17 15 30 26 41
เลอน 24 ขน
36 13
node ใหมทสรางข น 13 อยทเดม
น า 20 เขาสต าแหนงทเหมาะสม
2-3-4 Tree บทท 9
244
class Node {
private static final int ORDER = 4;
private int nums; //items in node
private Node parent; //parent node
private Node []children; //array to hold children
private Comparable []dataList; //array to hold items
//node can hold maximum items of 3
//node can have at most 4 children
public Node() {
children = new Node[ORDER];
dataList = new Comparable[ORDER - 1];
parent = null;
nums = 0;
}
... (code อน ๆ ดทายบท)
ใน class Node เรามตวแปรทส าคญอยดงน คอ parent ใชเปนตวบอกถงความเปนลกของ node ใด ๆ children เปนทเกบลก node ของ node ใด ๆ (links) dataList เปนทเกบขอมลทอยใน node นน ๆ และ method ทส าคญในการน าขอมลเขากคอ method insert() ซงเปน method ทตองน าขอมลเขาใหเหมาะสมกบต าแหนงทขอมลนนควรไปอย เราก าหนดใหการน าขอมลเขาส node เปนการน าขอมลเขาแบบจดเรยงจากนอยไปหามาก วธการนจะชวยใหเราสามารถแบง node ได งายขนถาหากวาการน าขอมลเขาส tree เจอ node ทเตมแลว ในการหาต าแหนงทขอมลจะตองถกน าไปเกบนน เราตองค านงถงสถานะของ node วาเปนอยางไร ถา node ไมมขอมลอยเลยการน าเขากเกดข นทต าแหนงแรกสด แตถา node มขอมลอยเรากตองหาต าแหนงดวยการเปรยบเทยบขอมลใหมกบขอมลทมอยใน node จนกวาจะเจอ ซงในขณะทเราท าการเปรยบเทยบ เรากท าการยายขอมลไปดวยเพอใหการน าเขาท าไดทนททเราเจอต าแหนงทเหมาะสม
ภาพท 9.8 การน าขอมลเขาส node แบบจดเรยง
//insert data into this node (ascending order)
//called by: Tree234.insert()
public int insert(Comparable key) {
nums++; //correct its count
//start from last item of this node, examine
//its value against key until appropriate location
//is found
for(int i = dataList.length-1; i >= 0; i--) {
if(dataList[i] != null) {
//if key to insert is less than data in
//this cell, shift right
if(key.compareTo(dataList[i]) < 0)
dataList[i+1] = dataList[i];
//otherwise, insert new key
//and returns its index
else {
ยาย 68 ไปทางขวา
2 1 0
ขอมลทตองการน าเขาส node 68 41
54
บทท 9 2-3-4 Tree
245
dataList[i+1] = key;
return i+1;
}
}
}
//all cells are null, insert at first location
dataList[0] = key; //insert new key
return 0; //return its index
}
ในการน าขอมลเขาส node นนเราตงสมมตฐานวา node ยงสามารถรบขอมลไดอก (การตรวจสอบวา node เตมท าใน method insert() ของ class Tree234) ดงนนส งทเราตองท ากคอยายขอมลใหไปอยในต าแหนงทเหมาะสมส าหรบการน าขอมลใหมเขา ดงทไดกลาวมาแลว
Method ทท าหนาทในการน าขอมลเขาส 2-3-4 Tree ของเราอกตวหนงซ งเปนตวทส าคญไมนอยไปกวา insert() ของ class Node กคอ insert() ของ class Tree234 ซงมหนาตาดงน
//inserts item into tree
public void insert(Comparable item) {
Node curNode = root;
while(true) {
//cannot insert here, it's full
//split the node, get its parent
//and go to lower level
if(curNode.isFull()) {
split(curNode);
curNode = curNode.getParent();
curNode = getNextChild(curNode, item);
}
//found a place to insert, do so and exit
else if(curNode.isLeaf()) {
curNode.insert(item);
return;
}
//node is not full and is not a leaf,
//we go to lower level
else
curNode = getNextChild(curNode, item);
}
}
สงทเราท าตองท ากอนการน าขอมลเขากคอ การตรวจสอบวาสามารถน าขอมลเขาไดหรอไม (ทต าแหนงของ node ปจจบน) ซงในการตรวจสอบสงเราจะพบกคอ
o Node เตม o Node ไมเตม และไมใช leaf node o Node ไมเตม และเปน leaf node - น าขอมลเขาได
กรณทงายทสดกคอ กรณสดทาย น าขอมลเขาไดเลยโดยเรยกใช method insert() ของ class Node สวนกรณทสอง เราตองลงไปอกระดบหนงใน tree เพอหาต าแหนงทสามารถน าขอมลเขาไดดวยการเรยกใช method getNextChild() ซง method ตวนจะสงคาของ node ผานมาทางตวแปร curNode คาดงกลาวจะเปนคาทบอกวา node เปน leaf node หรอเปน node ทเตม ถา node เตมเรากตองแบง node ดงทไดอธบายไวกอนหนาน code ทชวยในการแบง node มดงน
//splits node: n
private void split(Node n) {
//assume that node is full
//node = [item0, item1, item2] and we
//concern only item1 and item2 since
//item0 stays where it is
Comparable item1, item2;
Node parent, child2, child3;
Node newNode = new Node();
int index;
2-3-4 Tree บทท 9
246
//save last two items from this node
item2 = n.removeLargest();
item1 = n.removeLargest();
//disconnect children of both nodes
child2 = n.disConnect(2);
child3 = n.disConnect(3);
//node to split is at root
if(n == root) {
root = new Node(); //make new root
parent = root; //the parent is root
root.connect(0, n); //connect n to parent
}
//node to split is not root, so
//we get its parent
else
parent = n.getParent();
//take care of parent:
//insert item1 to parent node
index = parent.insert(item1);
int nums = parent.getItems();
//shift parent's connections one child to the right,
//then connect node: newNode to parent
for(int i = nums - 1; i > index; i--) {
Node temp = parent.disConnect(i);
parent.connect(i+1, temp);
}
parent.connect(index+1, newNode);
//take care of node: newNode by
//first insert item2 (removed from parent earlier)
//into newNode, then connect child2 to 0
//and child3 to 1
newNode.insert(item2);
newNode.connect(0, child2);
newNode.connect(1, child3);
}
ขนตอนแรกทเราตองท ากคอเกบขอมลสองต าแหนงทายสดทอยใน node ไวในตวแปร item2 และ item1 หลงจากนนเรากตดการเชอมตอขอมลทงสอง แตเราตองเกบขอมลนไวส าหรบการเชอมตอกบ node ใหมทเราตองสรางขน เมอไดส งทตองการแลวเรากตรวจสอบวา เราตองแบง node ในกรณไหน node ทเปน root หรอ node ทวไป ถาเปน root เรากสราง root ใหมพรอมทงก าหนดให node ใหมเปน parent ของ root เดมแตเปน node ทวไปเรากเพยงแตหา parent ของ node น หลงจากนนเรากท าการน าขอมลเขาส node ในต าแหนงทเหมาะสมทงในกรณของ root และไมใช root พรอมทง node ใหม Method อน ๆ กเปน method ทชวยใหการน าขอมลเขาเปนไปไดอยางราบรน (ด code ทายบท) ส งส าคญอกอยางหนงกคอการแสดงผลของขอมลทอยใน tree เราเลอกทจะแสดงผลดวยการใช Level-Order Traversal เปนตวชวยในการแสดงผล //display data in tree (Breadth-First)
public void display() {
Queue<Node> q = new Queue<Node>();
Node cur = root;
while(cur != null) {
cur.display();
for(int i = 0; i < cur.getMaxNode(); i++) {
if(cur.getChild(i) != null)
q.put(cur.getChild(i));
}
cur = (q.isEmpty()) ? null : q.get();
}
}
เราจะใช tree ตวทเหนนเปนขอมลทดสอบของเรา
บทท 9 2-3-4 Tree
247
ภาพท 9.9 2-3-4 Tree ทใชในการทดสอบ
หลงจากทเราไดทดลอง run ดวยโปรแกรมทดสอบดานลางน
1: /**
2: Testing module for 2-3-4 Tree
3: */
4:
5: class Test234Tree {
6: public static void main(String[] args) {
7: Tree234 t = new Tree234();
8:
9: //insert some data
10: t.insert(50);
11: t.insert(40);
12: t.insert(60);
13: t.insert(30);
14: t.insert(70);
15: t.insert(10);
16: t.insert(80);
17: t.insert(20);
18: t.insert(90);
19: t.insert(55);
20:
21: t.display();
22: }
23: }
ผลลพธทเราไดคอ [50]
[30]
[70]
[10 20]
[40]
[55 60]
[80 90]
จะเหนไดวาผลลพธทเราไดเปนไปตามขอมลทเรามอยใน tree ของเรา ผอานอาจใชวธการอนในการแสดงขอมลของ tree กไดเราเลอกทจะใช Level-Order Traversal เปนตวก าหนดการแสดงขอมลกเพราะเราคดวาโครงสรางของ 2-3-4 Tree เออใหการแสดงผลวธนท าไดงายทสด เราไดน าเอา code ทงหมดมาไว ณ ทน
1: /**
2: Simple Node's structure for 2-3-4 Tree
3: */
4:
5: class Node {
6: private static final int ORDER = 4;
7: private int nums; //items in node
8: private Node parent; //parent node
9: private Node []children;//array to hold children
10: private Comparable []dataList;//array to hold items
11:
20 10
70
50
30
40 60 55 90 80
2-3-4 Tree บทท 9
248
12: //node can hold maximum items of 3
13: //node can have at most 4 children
14: public Node() {
15: children = new Node[ORDER];
16: dataList = new Comparable[ORDER - 1];
17: parent = null;
18: nums = 0;
19: }
20:
21: //connect a child to this node
22: public void connect(int id, Node child) {
23: children[id] = child; //keep this child at
24: if(child != null) //location: id and make
25: child.parent = this; //this node its parent
26: }
27:
28: //disconnect a child from this node
29: public Node disConnect(int id) {
30: Node temp = children[id];
31: children[id] = null;
32: return temp;
33: }
34:
35: //get a child of this node
36: public Node getChild(int id) {
37: return children[id];
38: }
39:
40: //get parent of this node
41: public Node getParent() {
42: return parent;
43: }
44:
45: //return true if this node is a leaf node
46: public boolean isLeaf() {
47: return children[0] == null ? true : false;
48: }
49:
50: //return items in this node
51: public int getItems() {
52: return nums;
53: }
54:
55: //return the node at index i
56: public Comparable getData(int i) {
57: return dataList[i];
58: }
59:
60: //return maximum number of nodes
61: public int getMaxNode() {
62: return ORDER;
63: }
64:
65: //return true if this node is full
66: public boolean isFull() {
67: return (nums == ORDER - 1) ? true : false;
68: }
69:
70: //return index i if this node contains key
71: //called by: Tree234.find()
72: public int find(Comparable key) {
73: for(int i = 0; i < ORDER - 1; i++) {
74: if(dataList[i] == null)
75: break;
76: else if(dataList[i].equals(key))
77: return i;
78: }
79: return -1;
80: }
81:
82: //insert data into this node (ascending order)
83: //called by: Tree234.insert()
84: public int insert(Comparable key) {
85: nums++; //correct its count
บทท 9 2-3-4 Tree
249
86:
87: //start from last item of this node, examine
88: //its value against key until appropriate location
89: //is found
90: for(int i = dataList.length-1; i >= 0; i--) {
91: if(dataList[i] != null) {
92: //if key to insert is less than data in
93: //this cell, shift right
94: if(key.compareTo(dataList[i]) < 0)
95: dataList[i+1] = dataList[i];
96: //otherwise, insert new key
97: //and return its index
98: else {
99: dataList[i+1] = key;
100: return i+1;
101: }
102: }
103: }
104: //all cells are null, insert at first location
105: dataList[0] = key; //insert new key
106: return 0; //return its index
107: }
108:
109: //remove largest Data
110: //called by: Tree234.split()
111: public Comparable removeLargest() {
112: //assume node is not empty, retrieve largest one
113: Comparable largest = dataList[nums-1];
114: dataList[nums-1] = null; //set it to null
115: nums--; //correct its count
116: return largest; //return it
117: }
118:
119: //display data in this node
120: //format: [x x x x]
121: public void display() {
122: System.out.printf("[%s", dataList[0].toString());
123: for(int i = 1; i < nums; i++) {
124: System.out.print(" " + dataList[i].toString());
125: }
126: System.out.println("]");
127: }
128: }
ส าหรบ code ของ Tree234.java มดงน
1: /**
2: Structure for 2-3-4 Tree
3: */
4:
5: import java.util.LinkedList;
6:
7: class Tree234 {
8: private Node root; //root of tree
9:
10: public Tree234() {
11: root = new Node();
12: }
13:
14: //returns index of item found
15: public int find(Comparable key) {
16: //node for traversing the tree
17: Node curNode = root;
18: int id; //index of item found
19:
20: while(true) {
21: //call Node.find() for given key
22: //if value returns not equal to -1, we
23: //found it, so we return its index in that node
24: if((id = curNode.find(key)) != -1)
25: return id;
26: //dead end, no item
27: else if(curNode.isLeaf())
2-3-4 Tree บทท 9
250
28: return -1;
29: //not yet found, look somewhere else
30: else
31: curNode = getNextChild(curNode, key);
32: }
33: }
34:
35: //inserts item into tree
36: public void insert(Comparable item) {
37: Node curNode = root;
38: while(true) {
39: //cannot insert here, it's full
40: //split the node, get its parent
41: //and go to lower level
42: if(curNode.isFull()) {
43: split(curNode);
44: curNode = curNode.getParent();
45: curNode = getNextChild(curNode, item);
46: }
47: //found a place to insert, do so and exit
48: else if(curNode.isLeaf()) {
49: curNode.insert(item);
50: return;
51: }
52: //node is not full and is not a leaf,
53: //we go to lower level
54: else
55: curNode = getNextChild(curNode, item);
56: }
57: }
58:
59: //splits node: n
60: private void split(Node n) {
61: //assume that node is full
62: //node = [item0, item1, item2] and we
63: //concern only item1 and item2 since
64: //item0 stays where it is
65: Comparable item1, item2;
66: Node parent, child2, child3;
67: Node newNode = new Node();
68: int index;
69:
70: //save last two items from this node
71: item2 = n.removeLargest();
72: item1 = n.removeLargest();
73: //disconnect children of both nodes
74: child2 = n.disConnect(2);
75: child3 = n.disConnect(3);
76:
77: //node to split is at root
78: if(n == root) {
79: root = new Node(); //make new root
80: parent = root; //the parent is root
81: root.connect(0, n); //connect n to parent
82: }
83: //node to split is not root, so
84: //we get its parent
85: else
86: parent = n.getParent();
87:
88: //take care of parent:
89: //insert item1 to parent node
90: index = parent.insert(item1);
91: int nums = parent.getItems();
92: //shift parent's connections one child to the right,
93: //then connect node: newNode to parent
94: for(int i = nums - 1; i > index; i--) {
95: Node temp = parent.disConnect(i);
96: parent.connect(i+1, temp);
97: }
98: parent.connect(index+1, newNode);
99:
100: //take care of node: newNode by
101: //first insert item2 (removed from parent earlier)
บทท 9 2-3-4 Tree
251
102: //into newNode, then connect child2 to 0
103: //and child3 to 1
104: newNode.insert(item2);
105: newNode.connect(0, child2);
106: newNode.connect(1, child3);
107: }
108:
109: //returns next approriate child of node
110: private Node getNextChild(Node n, Comparable value) {
111: int i;
112: //assume that node is not empty, not full, and not a leaf
113: int nums = n.getItems();
114:
115: for(i = 0; i < nums; i++) {
116: //if search value is less than this node,
117: //return left child
118: if(value.compareTo(n.getData(i)) < 0)
119: return n.getChild(i);
120: }
121: //otherwise return right child
122: return n.getChild(i);
123: }
124:
125: //display data in tree (Breadth-First)
126: public void display() {
127: Queue<Node> q = new Queue<Node>();
128: Node cur = root;
129: while(cur != null) {
130: cur.display();
131: for(int i = 0; i < cur.getMaxNode(); i++) {
132: if(cur.getChild(i) != null)
133: q.put(cur.getChild(i));
134: }
135: cur = (q.isEmpty()) ? null : q.get();
136: }
137: }
138: }
139:
140: /**
141: Internal class for level-order traversal
142: */
143:
144: class Queue<T> {
145: //create list to hold data
146: protected LinkedList<T> list;
147:
148: //default constructor
149: //calling LinkedList's constructor
150: public Queue() {
151: list = new LinkedList<T>();
152: }
153:
154: //insert item via LinkeList's addLast()
155: public void put(T item) {
156: list.addLast(item);
157: }
158:
159: //remove item via LinkedList's remove()
160: public T get() {
161: //cannot remove if empty
162: if(isEmpty())
163: return null;
164: //return item removed
165: return list.remove();
166: }
167:
168: //call LinkedList's isEmpty()
169: public boolean isEmpty() {
170: return list.isEmpty();
171: }
172: }
2-3-4 Tree บทท 9
252
สรป ในบทนเราไดพดถงโครงสรางหลกของ 2-3-4 Tree ซงหมายถงการน าขอมลเขา การแบง node ถาการน าขอมลเขาท าไมได เพราะ node เตมรวมไปถงการแสดงผลของ 2-3-4 Tree ดวยการใชการเขาหาแบบ Level-Order โดยรามแลวเราไดพดถง 2-3-4 Tree มขอมล (key) และ node มากกวา Binary Tree 2-3-4 Tree มขอมลไดสงสดสามตว และมลกไดไมเกนสตว ขอมลทางดานซายจะนอยกวาขอมลทางดานขวา การน าขอมลเขาส 2-3-4 Tree ท าไดท leaf node และ leaf node จะอยในระดบเดยวกน การน าขอมลเขาส 2-3-4 Tree ณ node ทเตมแลวจะตองท าการแบง node การแบง node จะเปนไปไดสองกรณคอ ใน root และใน node ทไมใช root การแบง root จะตองสราง node ใหมสอง node สวนการแบง node อนจะสรางเพยง node
เดยว แบบฝกหด 1. จงเปลยนให 2-3-4 Tree เกบขอมลทเกยวกบนกศกษา (แทนการเกบ Comparable) เชน
นกศกษาคนหนงอาจมขอมลดงน ช อ นามสกล ทอย เบอรโทรศพท และ email เปนตน ให เขยนโปรแกรมทดสอบทสามารถน าขอมลเขา คนหาขอมล และแสดงผลขอมล
2. จงเขยน method แสดงผลของขอมลทมอยใน 2-3-4 Tree โดยเปลยนมาใชการเขาแบบ
in-order, pre-order, และ post-order ใหเขยนโปรแกรมทดสอบ 3. จงเขยน method ทใช Depth-First Traversal ในการแสดงผลของ 2-3-4 Tree 4. จงเขยน method ส าหรบการลบขอมลทก าหนดใหจากผใชออกจาก 2-3-4 Tree 5. จงเขยนโปรแกรมทท าการเปลยน 2-3-4-Tree ใหเปน Red-Black Tree 6. จงปรบปรง 2-3-4 tree ใหเปน B-tree ทสามารถรองรบจ านวน node ตามความตองการ
ของผใช