hello everyone i got this assignment.We have this piece of code and we must make a search for a key.if the key exist it returns true if not false.Plus we must insert a key in the class.if it is already in there we say hey its already in and we don t put it again
ok first of all this is what our professor gave us
and this is what i madepackage askisi2; import java.util.*; public class mtree { protected class tnode { public int k1; public int k2; public int k3; public int np; public tnode c0; public tnode c1; public tnode c2; public tnode c3; public tnode goneas; tnode () { np = 0; c0 = null; c1 = null; c2 = null; c3 = null; goneas=null; } } protected tnode root; protected int size; public mtree() { root=null; size=0; } public int treeSize() { return size; }
public boolean key_search(int key) { tnode node = root; while (node != null) { if (key < node.c1) { node = node.c1; } else if (key == node.c1) { return true; } else if ((node.keys == 1) || (key < node.key2)) { node = node.c2; } else if (key == node.c2) { return true; } else if ((node.keys == 2) || (key < node.key3)) { node = node.c3; } else if (key == node.key3) { return true; } else { node = node.c4; } } return false; } public void inTree(int key) { tnode node = root; if (root == null) { root = new mtree(key); size++; return; } if (node.keys == 3) { tnode par = new tnode(node.key2); tnode left = node; tnode right = new tnode(par, node.key3); par.c1 = left; par.c2 = right; if (node.c1 != null) { par.c2.c1 = node.c3; par.c2.c2 = node.child4; par.c2.c1.goneas = right; par.c2.c2.goneas = right; } par.c1.keys = 1; par.c1.key2 = 0; par.c1.key3 = 0; par.c1.c3 = null; par.c1.c4 = null; par.c1.goneas = par; root = par; node = root; } while (node.c1 != null) { if (key < node.key1) { node = node.c1; } else if (key == node.key1) { // found the same key as trying to insert - do nothing System.Out.println("iparxei to key pou dwsate") return; } else if ((node.keys == 1) || (key < node.key2)) { node = node.c2; } else if (key == node.key2) { System.Out.println("iparxei to key pou dwsate") return; } else if ((node.keys == 2) || (key < node.key3)) { node = node.c3; } else if (key == node.key3) { System.Out.println("iparxei to key pou dwsate") return; } else { node = node.c4; } if (node.keys == 3) { tnode left = node; tnode par = node.parent; tnode right = new tnode(node.key3); right.c1 = node.c3; right.c2 = node.c4; if (par.keys == 1) { if (node.key2 < par.key1) { par.key2 = par.key1; par.key1 = node.key2; par.c3 = par.c2; par.c2 = right; par.c2.goneas = par; par.c1 = left; par.c1.goneas = par; } else { par.key2 = node.key2; par.c2 = left; par.c2.goneas = par; par.c3 = right; par.c3.goneas = par; } } else { if (node.key2 < par.key1) { par.key3 = par.key2; par.key2 = par.key1; par.key1 = node.key2; par.c4 = par.c3; par.c3 = par.c2; par.c2 = right; par.c2.goneas = par; par.c1 = left; par.c1.goneas = par; } else if (node.key2 > par.key2) { par.key3 = node.key2; par.c3 = left; par.c3.goneas = par; par.c4 = right; par.c4.goneas = par; } else { par.key3 = par.key2; par.key2 = node.key2; par.c4 = par.c3; par.c2 = left; par.c2.goneas = par; par.c3 = right; par.c3.goneas = par; } } par.keys++ node.keys = 1; node.key2 = 0; node.key3 = 0; node.c3 = null; node.c4 = null; node = par; } } if (node.keys == 1) { if (key < node.key1) { node.key2 = node.key1; node.key1 = key; } else { node.key2 = key; } } else { if (key < node.key1) { node.key3 = node.key2; node.key2 = node.key1; node.key1 = key; } else if (key > node.key2) { node.key3 = key; } else { node.key3 = node.key2; node.key2 = key; } } node.keys++; size++; } }