Q2 CSD
Q2 CSD
void insert(Ball x) {
Node q = new Node(x);
if (root == null) {
root = q;
return;
}
Node f, p;
p = root;
f = null;
while (p != null) {
if (p.info.type == x.type) {
return;
}
if (x.type < p.info.type) {// edit here
f = p;
p = p.left;
} else {
f = p;
p = p.right;
}
}
if (x.type < f.info.type) {
f.left = q;
} else {
f.right = q;
}
}
//-----------------------------------
----------------------
void inOrder2(Node p, RandomAccessFile f) throws Exception {
if (p == null) {
return;
}
inOrder2(p.left, f);
if(p.info.price < 7 )
fvisit(p, f);
inOrder2(p.right, f);
}
--------------------
//xoa thang dau tien co 2 con , duyet theo in-order
int count = 0;
}
inOrder2(p.right, f);
}
if (p.left != null && p.right != null) // p has both left and right
children
{
Node q, fr, rp; // p's key will be replaced by rp's one
fr = null;
q = p.left;
rp = q;
while (rp.right != null) {
fr = rp;
rp = rp.right; // Find the right most node on the left sub-tree
}
p.info = rp.info;
if (fr == null) // rp is just a left son of p
{
p.left = rp.left;
} else {
fr.right = rp.left;
}
}
--------------------
-- xoa thang dau tien co 2 con va price < 7 va duyet theo breadth-order
------ Viet vao truoc fvisit(r, f);----------
if (r.left != null && r.right != null && count == 0 && r.info.price < 7) {
count++;
deleByCopy(r.info.type);
}
---
//------------------
//Duyet in-order , p co 2 con , xoay trai p
int count1 = 0;
void inOrder3(Node p, RandomAccessFile f) throws Exception {
if (p == null) {
return;
}
inOrder3(p.left, f);
if (p.left != null && p.right != null && count1 == 0) {
count1++;
rotateLeft(p);
}
inOrder3(p.right, f);
}
//-------------------------
//xoa thang lon thu (count)
int MaxAgeN(int n) {
Node p = root;
int max = -1;
Queue q = new Queue();
q.enqueue(root);
if (n == 1) {
while (!q.isEmpty()) {
p = (Node) q.dequeue();
if (p.info.color > max) {
max = p.info.color;
}
if (p.left != null) {
q.enqueue(p.left);
}
if (p.right != null) {
q.enqueue(p.right);
}
}
} else {
p = root;
int maxN = MaxAgeN(n - 1);
max = 0;
while (!q.isEmpty()) {
p = (Node) q.dequeue();
if (p.info.color > max && p.info.color < maxN) {
max = p.info.color;
}
if (p.left != null) {
q.enqueue(p.left);
}
if (p.right != null) {
q.enqueue(p.right);
}
}
}
return max;
}
if (p.left != null && p.right != null) // p has both left and right
children
{
Node q, fr, rp; // p's key will be replaced by rp's one
fr = null;
q = p.left;
rp = q;
while (rp.right != null) {
fr = rp;
rp = rp.right; // Find the right most node on the left sub-tree
}
p.info = rp.info;
if (fr == null) // rp is just a left son of p
{
p.left = rp.left;
} else {
fr.right = rp.left;
}
}
//----------------------------
//xoay trai thang parent cua thang lon nhat
void max2() {
if (isEmpty()) {
return;
}
Node p = root;
while (p.right != null) {
p = p.right;
}
rotateL(parent(p));
}
Node ch = par.right;
par.right = ch.left;
ch.left = par;
if (gr == null) {
root = ch;
} else if (gr.left == p) {
gr.left = ch;
} else if (gr.right == p) {
gr.right = ch;
}
return ch;
}
//Tim bo cua thang truyen vao
Node parent(Node x) {
Node p = root;
Node parent = null;
while (p != null) {
if (p.info.color == x.info.color) {
break;
}
parent = p;
if (p.info.color > x.info.color) {
p = p.left;
} else {
p = p.right;
}
}
return parent;
}
//--------------------
//xoay phai
//------------------------
xoay phai theo price
//----------------
//xoa node thu may
int count2 = 0;
preOrder4(p.left, f);
preOrder4(p.right, f);
}
---------------------------
//xoay trai void
public void rotateL(Node par) {
Node p = root;
Node gr = null;
while (p != null) {
if (p == par) {
break;
}
gr = p;
if (p.info.sound > par.info.sound) {
p = p.left;
} else {
p = p.right;
}
}
Node ch = par.right;
par.right = ch.left;
ch.left = par;
if (gr == null) {
root = ch;
} else if (gr.left == p) {
gr.left = ch;
} else if (gr.right == p) {
gr.right = ch;
}
}
//Tim thang node thu 2 theo breadth va lay no lam goc , xoa thang lon nhat trong
cay subtree do
-------------------------------
//tim thang thu 2 con co trai duyet theo breath
void breadth3(Node p, RandomAccessFile f) throws Exception {
int count3 = 0;
if (p == null) {
return;
}
Queue q = new Queue();
q.enqueue(p);
Node r;
while (!q.isEmpty()) {
r = q.dequeue();
if(r.left != null ){
count3++;
if(count3 == 2){
rotateRight(r);//
}
}
if (r.left != null) {
q.enqueue(r.left);
}
if (r.right != null) {
q.enqueue(r.right);
}
}
}
----------------------
public int getHeight(Node p) {
if (p == null) {
return 0;
}
return Math.max(getHeight(p.left), getHeight(p.right)) + 1;
}
-------------------//xoa thang cha cua thang node thu 4 duyet theo post-order
int count2 = 0;
Node parent(Node x) {
Node p = root;
Node parent = null;
while (p != null) {
if (p.info.wing == x.info.wing) {
break;
}
parent = p;
if (p.info.wing > x.info.wing) {
p = p.left;
} else {
p = p.right;
}
}
return parent;
}
/-----------------------------------------
int count = 0;
Node node3 = null;
void postOrder(Node p) {
if (p == null) {
return;
}
postOrder(p.left);
postOrder(p.right);
//logic
if (count == 4 && node3 == null) {
node3 = p;
}
count++;
}