Professional Documents
Culture Documents
Лабораторна робота 2 Захаров Юрій ПЗПІ-20-2
Лабораторна робота 2 Захаров Юрій ПЗПІ-20-2
Лабораторна робота №2
З дисципліни «Алгоритми та структури даних»
public Node(int d)
{
key = d;
height = 1;
}
}
Node root;
int height(Node N)
{
if (N == null)
return 0;
return N.height;
}
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;
if (node == null)
return (new Node(key));
node.height = 1 + max(height(node.left),
height(node.right));
return node;
preOrder(node.left);
preOrder(node.right);
}
}
inOrder(node.left);
inOrder(node.right);
}
}
if (node != null)
{
postOrder(node.left);
postOrder(node.right);
if (root == null)
return root;
else
{
if (temp == null)
{
temp = root;
root = null;
}
else
root = temp;
}
else
{
root.key = temp.key;
if (root == null)
return root;
root.height = max(height(root.left),
height(root.right)) + 1;
return root;
}
tree.postOrder(tree.root);
Console.WriteLine();
Console.WriteLine("The size of binary tree is : " + tree.size());
}
}