我正在创建AVL二叉树,其唯一的问题是根不会改变其位置或获得平衡,除了所有其他孩子的根或叶等
非常感谢任何帮助!
逻辑层文件
public class Node
{
public int data;
public Node left, right;
public Node(int data)
{
this.data = data;
left = null;
right = null;
}
}
public class BinaryTree
{
public Node root;
public BinaryTree()
{
root = null;
}
public int height(Node temp)
{
int h = 0;
if (temp != null)
{
int l_height = height(temp.left);
int r_height = height(temp.right);
int max_height = Math.Max(l_height, r_height);
h = max_height + 1;
}
return h;
}
public int diff(Node temp)
{
int l_height = height(temp.left);
int r_height = height(temp.right);
int b_factor = l_height - r_height;
return b_factor;
}
Node rr_rotation(Node parent)
{
Node temp;
temp = parent.right;
parent.right = temp.left;
temp.left = parent;
return temp;
}
Node ll_rotation(Node parent)
{
Node temp;
temp = parent.left;
parent.left = temp.right;
temp.right = parent;
return temp;
}
Node lr_rotation(Node parent)
{
Node temp;
temp = parent.left;
parent.left = rr_rotation(temp);
return ll_rotation(parent);
}
Node rl_rotation(Node parent)
{
Node temp;
temp = parent.right;
parent.right = ll_rotation(temp);
return rr_rotation(parent);
}
Node balance(Node temp)
{
int bal_factor = diff(temp);
if (bal_factor > 1)
{
if (diff(temp.left) > 0)
temp = ll_rotation(temp);
else
temp = lr_rotation(temp);
}
else if (bal_factor < -1)
{
if (diff(temp.right) > 0)
temp = rl_rotation(temp);
else
temp = rr_rotation(temp);
}
return temp;
}
public Node addNode(int data) // It only add the Root(that is 55 in the fig)
{
Node newNode = new Node(data);
if (root == null)
{
root = newNode;
}
return root;
}
public Node insertNode(Node root, int newNode) //But I want to make this should add root node.
{
if (root == null) (I think here is some problem)
{
root = new Node(newNode);
root.data = newNode;
root.left = null;
root.right = null;
return root;
}
if (newNode < root.data)
{
root.left = insertNode(root.left, newNode);
root = balance(root);
}
else if (newNode >= root.data)
{
root.right = insertNode(root.right, newNode);
root = balance(root);
}
return root;
}
}
展示图层文件
public partial class PresentationLayer : Form
{
BinaryTree obj = new BinaryTree();
int a, b;
public PresentationLayer()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) //add node button
{
int num = 0;
bool result = int.TryParse(textBox1.Text, out num);
if (result)
{
a = Int32.Parse(textBox1.Text);
obj.addNode(a); //It creates root
textBox1.Hide();
button1.Hide();
}
}
private void button2_Click_1(object sender, EventArgs e) //insert button
{
int num = 0;
bool result = int.TryParse(textBox2.Text, out num);
if (result)
{
b = Int32.Parse(textBox2.Text);
Node abc = new Node(b);
obj.insertNode(obj.root, b); //It is not creating root
textBox2.Clear();
}
}
}
}
非常感谢任何帮助!
答案 0 :(得分:0)
您应该将案例添加到ll_rotation
和rr_rotation
函数中以处理根旋转的时间。您应该能够检查参数parent
是否与root
是同一个节点,如果是,请设置root
以引用新的旋转节点。
例如,每Wikipedia's example of left rotation:
P
/ \
/ \
A Q
/ \
B C
向左旋转变为:
Q
/ \
/ \
P C
/ \
A B
如果调用ll_rotation(P)
,P
与root
属于同一节点,则该函数应将Q
指定为新root