在这里,我正在制作一个对BFS有益的 Weight-based Binary Tree ,因为它保持了树的 balancer .
我会在_2706105中实现它:
class BT_Node
{
public int value;
public int depth;
public long weight;
public BT_Node left, right;
public BT_Node (int value, int depth)
{
this.depth = depth;
this.value = value;
this.weight = 0;
this.left = this.right = null;
}
}
class BT
{
private BT_Node root;
public long size ()
{
return (root!=null ? root.weight : 0);
}
public long size (BT_Node node)
{
return (node!=null ? node.weight : 0);
}
public void insert (int value)
{
int depth = 0;
BT_Node parent = root;
if (root == null)
{
root = new BT_Node (value, 0);
}
else
{
root.weight++;
while (parent.left!=null && parent.right!=null)
{
if (parent.left.weight <= parent.right.weight)
parent=parent.left;
else
parent=parent.right;
parent.weight++;
}
if (parent.left == null)
parent.left = new BT_Node (value, parent.depth+1);
else
parent.right = new BT_Node (value, parent.depth+1);
}
}
}