Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Харківський національний університет радіоелектроніки

Лабораторна робота №2
З дисципліни «Алгоритми та структури даних»

Виконав студент групи ПЗПІ-20-2 Захаров Ю.Ю.


Перевірив ассистент кафедри ПІ Терещенко Г.Ю.
Тема: бінарні дерева та операції над ними.
Мета: навчитися створювати алгоритми для балансування бінарних
дерев.
Код програми:
using System;

public class Node


{
public int key, height;
public Node left, right;

public Node(int d)
{
key = d;
height = 1;
}
}

public class BBSTree


{

Node root;

int height(Node N)
{
if (N == null)
return 0;

return N.height;
}

int max(int a, int b)


{
return (a > b) ? a : b;
}

Node rightRotate(Node y)
{
Node x = y.left;
Node T2 = x.right;

x.right = y;
y.left = T2;

y.height = max(height(y.left),
height(y.right)) + 1;
x.height = max(height(x.left),
height(x.right)) + 1;

return x;
}
Node leftRotate(Node x)
{
Node y = x.right;
Node T2 = y.left;

y.left = x;
x.right = T2;

x.height = max(height(x.left),
height(x.right)) + 1;
y.height = max(height(y.left),
height(y.right)) + 1;

return y;
}

int getBalance(Node N)
{
if (N == null)
return 0;

return height(N.left) - height(N.right);


}

Node insert(Node node, int key)


{

if (node == null)
return (new Node(key));

if (key < node.key)


node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
else
{
return node;

node.height = 1 + max(height(node.left),
height(node.right));

int balance = getBalance(node);

if (balance > 1 && key < node.left.key)


return rightRotate(node);

if (balance < -1 && key > node.right.key)


return leftRotate(node);

if (balance > 1 && key > node.left.key)


{
node.left = leftRotate(node.left);
return rightRotate(node);
}
if (balance < -1 && key < node.right.key)
{
node.right = rightRotate(node.right);
return leftRotate(node);
}

return node;

void preOrder(Node node)


{
if (node != null)
{
Console.Write(node.key + " ");

preOrder(node.left);

preOrder(node.right);
}
}

void inOrder(Node node)


{
if (node != null)
{

inOrder(node.left);

Console.Write(node.key + " ");

inOrder(node.right);
}
}

void postOrder(Node node)


{

if (node != null)
{

postOrder(node.left);

postOrder(node.right);

Console.Write(node.key + " ");

Node minValueNode(Node node)


{
Node current = node;

while (current.left != null)


current = current.left;
return current;
}

Node deleteNode(Node root, int key)


{

if (root == null)
return root;

if (key < root.key)


root.left = deleteNode(root.left, key);

else if (key > root.key)


root.right = deleteNode(root.right, key);

else
{

if ((root.left == null) || (root.right == null))


{
Node temp = null;
if (temp == root.left)
temp = root.right;
else
temp = root.left;

if (temp == null)
{
temp = root;
root = null;
}
else
root = temp;
}
else
{

Node temp = minValueNode(root.right);

root.key = temp.key;

root.right = deleteNode(root.right, temp.key);


}
}

if (root == null)
return root;

root.height = max(height(root.left),
height(root.right)) + 1;

int balance = getBalance(root);


if (balance > 1 && getBalance(root.left) >= 0)
return rightRotate(root);

if (balance > 1 && getBalance(root.left) < 0)


{
root.left = leftRotate(root.left);
return rightRotate(root);
}

if (balance < -1 && getBalance(root.right) <= 0)


return leftRotate(root);

if (balance < -1 && getBalance(root.right) > 0)


{
root.right = rightRotate(root.right);
return leftRotate(root);
}

return root;
}

public virtual int size()


{
return size(root);
}

public virtual int size(Node node)


{
if (node == null)
{
return 0;
}
else
{
return (size(node.left) + 1 + size(node.right));
}
}
public int Search(int k, Node v)
{
if (v == null)
{
return v.key;
}
if (k == v.key)
{
return k;
}
else if (k < v.key)
{
return Search(k, v.left);
}
else if (k > v.key)
{
return Search(k, v.right);
}
else
{
return v.key;
}
}
public static void Main(String[] args)
{
BBSTree tree = new BBSTree();

tree.root = tree.insert(tree.root, 35);


tree.root = tree.insert(tree.root, 10);
tree.root = tree.insert(tree.root, 20);
tree.root = tree.insert(tree.root, 30);
tree.root = tree.insert(tree.root, 40);
tree.root = tree.insert(tree.root, 50);
tree.root = tree.insert(tree.root, 25);
Console.WriteLine(tree.Search(25, tree.root));

tree.postOrder(tree.root);
Console.WriteLine();
Console.WriteLine("The size of binary tree is : " + tree.size());
}
}

You might also like