0% found this document useful (0 votes)
20 views8 pages

Week 10

Uploaded by

Manikar Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Week 10

Uploaded by

Manikar Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Name : J shiva shankara vara prasad

Rollno: 23R25A1202
Section:IT-A
Week-10
1) B-tree insertion
class BTree
{
private int T;

public class Node


{
int n;
int key[] = new int[2 * T - 1];
Node child[] = new Node[2 * T];
boolean leaf = true;

public int Find (int k)


{
for (int i = 0; i < this.n; i++)
{
if (this.key[i] == k)
{
return i;
}
}
return -1;
};
}

public BTree(int t)
{
T = t;
root = new Node ();
root.n = 0;
root.leaf = true;
}

private Node root;

// split
private void split (Node x, int pos, Node y)
{
Node z = new Node ();
z.leaf = y.leaf;
z.n = T - 1;

for (int j = 0; j < T - 1; j++)


{
z.key[j] = y.key[j + T];
}
if (!y.leaf)
{
for (int j = 0; j < T; j++)
{
z.child[j] = y.child[j + T];
}
}
y.n = T - 1;

for (int j = x.n; j >= pos + 1; j--)


{
x.child[j + 1] = x.child[j];
}
x.child[pos + 1] = z;

for (int j = x.n - 1; j >= pos; j--)


{
x.key[j + 1] = x.key[j];
}
x.key[pos] = y.key[T - 1];
x.n = x.n + 1;
}

// insert key
public void insert (final int key)
{
Node r = root;

if (r.n == 2 * T - 1)
{
Node s = new Node ();
root = s;
s.leaf = false;
s.n = 0;
s.child[0] = r;
split (s, 0, r);
_insert (s, key);
}
else
{
_insert (r, key);
}
}
// insert node
final private void _insert (Node x, int k)
{

if (x.leaf)
{
int i = 0;

for (i = x.n - 1; i >= 0 && k < x.key[i]; i--)


{
x.key[i + 1] = x.key[i];
}
x.key[i + 1] = k;
x.n = x.n + 1;
}
else
{
int i = 0;

for (i = x.n - 1; i >= 0 && k < x.key[i]; i--)


{
};
i++;

Node tmp = x.child[i];


if (tmp.n == 2 * T - 1)
{
split (x, i, tmp);
if (k > x.key[i])
{
i++;
}
}
_insert (x.child[i], k);
}
}

public void display ()


{
display (root);
}

// Display the tree


private void display (Node x)
{
assert (x == null);
for (int i = 0; i < x.n; i++)
{
System.out.print (x.key[i] + " ");
}

if (!x.leaf)
{
for (int i = 0; i < x.n + 1; i++)
{
display (x.child[i]);
}
}
}
}
public class Main{
public static void main (String[]args)
{
BTree b = new BTree(1);
b.insert(5);
b.insert (6);
b.insert (7);
b.insert (8);
b.insert (12);
b.insert (13);
b.insert (14);
b.insert (4);
b.display ();
}
}

Output
B-tree search operation
//10.2 search operation on BTree
class BTreeNode {
int[] keys;
int t;
BTreeNode[] children;
int n;
boolean leaf;

public BTreeNode(int t, boolean leaf) {


this.t = t;
this.leaf = leaf;
keys = new int[2 * t - 1];
children = new BTreeNode[2 * t];
n = 0;
}

public BTreeNode search(int key) {


int i = 0;
while (i < n && key > keys[i])
i++;
if (keys[i] == key)
return this;
if (leaf)
return null;
return children[i].search(key);
}

public void insertNonFull(int key) {


int i = n - 1;
if (leaf) {
while (i >= 0 && key < keys[i]) {
keys[i + 1] = keys[i];
i--;
}
keys[i + 1] = key;
n++;
} else {
while (i >= 0 && key < keys[i])
i--;
i++;
if (children[i].n == 2 * t - 1) {
splitChild(i, children[i]);
if (key > keys[i])
i++;
}
children[i].insertNonFull(key);
}
}

public void splitChild(int i, BTreeNode y) {


BTreeNode z = new BTreeNode(y.t, y.leaf);
z.n = t - 1;
for (int j = 0; j < t - 1; j++)
z.keys[j] = y.keys[j + t];
if (!y.leaf) {
for (int j = 0; j < t; j++)
z.children[j] = y.children[j + t];
}
y.n = t - 1;
for (int j = n; j >= i + 1; j--)
children[j + 1] = children[j];
children[i + 1] = z;
for (int j = n - 1; j >= i; j--)
keys[j + 1] = keys[j];
keys[i] = y.keys[t - 1];
n++;
}

public void printInOrder() {


for (int i = 0; i < n; i++) {
if (!leaf)
children[i].printInOrder();
System.out.print(keys[i] + " ");
}
if (!leaf)
children[n].printInOrder();
}
}

public class BTree {


private BTreeNode root;
private int t;

public BTree(int t) {
this.t = t;
root = null;
}
public BTreeNode search(int key) {
return (root == null) ? null : root.search(key);
}

public void insert(int key) {


if (root == null) {
root = new BTreeNode(t, true);
root.keys[0] = key;
root.n = 1;
} else {
if (root.n == 2 * t - 1) {
BTreeNode newRoot = new BTreeNode(t, false);
newRoot.children[0] = root;
newRoot.splitChild(0, root);
int i = 0;
if (newRoot.keys[0] < key)
i++;
newRoot.children[i].insertNonFull(key);
root = newRoot;
} else {
root.insertNonFull(key);
}
}
}

public void printBTree() {


if (root != null)
root.printInOrder();
System.out.println();
}

public static void main(String[] args) {


BTree bTree = new BTree(3);
bTree.insert(10);
bTree.insert(20);
bTree.insert(5);
bTree.insert(6);
bTree.insert(12);
bTree.insert(30);

System.out.print("B-tree: ");
bTree.printBTree();
int searchKey = 6;
BTreeNode foundNode = bTree.search(searchKey);
if (foundNode != null)
System.out.println("Key " + searchKey + " found in the B-tree.");
else
System.out.println("Key " + searchKey + " not found in the B-tree.");
}
}
Output

You might also like