Interview(5)Binary Tree

Interview(5)Binary Tree

Binary Tree

public class BinTreeNode implements BinTreePosition {
    protected Object element; // item in this node
    protected BinTreePosition parent; //parent
    protected BinTreePosition lChild; // left child
    protected BinTreePosition rChild; // right child
    protected int size; // children size
    protected int height; //height
    protected int depth; //depth

    public BinTreeNode(){
        this(null, null, true, null, null);
    }

    public BinTreeNode(Object e, //element in node
                                    BinTreePosition p, //parent node
                                    boolean asLChild, // left child of parent
                                    BinTreePosition l, // left child
                                    BinTreePosition r // right child
                                    ){
        size = 1; height = depth = 0; parent = lChid = rChild = null;  
        element = e;
        if(null != p){ //set link to parent
                if(asLChild){
                    p.attachL(this);
                }else{
                    p.attachR(this);
                }
        }
        if(null != l){ attachL(l); }
        if(null != r){ attachR(r); }
    }

    public Object getElem(){ return element; }
    public Object setElem(Object obj){
        Object bak = element; element = obj; return bak;
    }

    public boolean hasParent(){
        return null != parent;
    }

    public BinTreePosition getParent(){ return parent; }
    public void setParent(BinTreePosition p){ parent = p; }
    public boolean isLeaf(){
        return !hasLChild() && !hasRChild();
    }

    public boolean isLChild(){
        return (hasParent() && this == getParent().getLChild())? true: false;
    }

    public boolean hasLChild() { return null != lChild; }
    public BinTreePosition getLChild() { return lChild; }
    public void setLChild(BinTreePosition c){ lChild = c; }
    public boolean isRChild(){
        return (hasParent() && this == getParent().getRChild()) ? true : false;
    }
    public boolean hasRChild() { return null != rChild; }
    public BinTreePosition getRChild(){  return rChild; }
    public void setRChild(BinTreePosition c){ rChild = c; }
    public int getSize() { return size; }
    public void udpateSize() {
        size = 1; //current node
        if(hasLChild()) { size += getLChild().getSize();}
        if(hasRChild()) { size += getRChild().getSize(); }
        if(hasParent()) { getParent().updateSize(); }
    }

    public it getHeight(){ return height; }
    public void updateHeight() {
        height = 0;
        if(hasLChild()) { height = Math.max(height, 1 + getLChild().getHeight()); }
        if(hasRChild()) { height = Math.max(height, 1 + getRChild().getHeight()); }
        if(hasParent()) { getParent().updateHeight(); }
    }

    public int getDepth(){ return depth; }
    public void updateDepth(){
        depth = hasParent() ? 1+ getParent().getDepth() : 0; //current node
        if(hasLChild()) { getLChild().updateDepth(); }
        if(hasRChild()) { getRChild().updateDepth(); }
    }

    public BinTreePosition getPrev(){ //use mid iterator find the previous node
        ..snip...
    }
    …snip...
}

Method: updateSize(v)
Inputs: Binary Tree Node v
Outputs: update the size for v
{
    size(v) = 1 + size(lc) + size(rc);
    update size for parents till root
}

Method: updateHeight(v) - O(depth(v) + 1)
Input: Binary Tree Node v
Outputs: update height of v
{
    height(v) = 0;  //no children
    if has left child node, height(v) = Max(height(v), 1 + height(lc));
    if has right child node, height(v) = Max(height(v), 1 + height(rc));
    update height for all parents
}

Method: updateDepth(v)
Inputs: Binary tree Node v
Outputs: Update depth for v
{
    if parent node p, depth(v) = depth(p) + 1;
    else depth(v) = 0;
    if v left child node, updateDepth(lc);
    if v right child node, updateDepth(rc);
}

Method: secede(v) - O(depth(v) + size(v) +1)
Inputs: binary tree node v
Outputs: v is root, the sub tree will secede
{
    if v has parent,
    #1 cut the reference from parent to v
    #2 updateSize(v) updateHeight(v)
    #3 cut the reference from v to parent
    #4 updateDepth(v)
}

Method: attachL(p, c)
Inputs: binary tree node p and c
Outputs: c will be the left child of p
{
    if p has left child node, secede(lc)
    secede(c)
    set the reference between p and c
    updateSize(p) updateHeight(p)
    updateDepth(c)
}

Inorder Traversal
Method: InorderTraversal(v)
Inputs: binary tree node v
Outputs: v inorder traversal
{
    if(null != v){
        InorderTraversal(lc) //left child node inorder traversal
        visit v
        InorderTraversal(rc)//right child node inorder traversal
    }
}

Full Binary Tree
public interface FullBinaryTree extend BinaryTree{
    public BinTreePosition addLast(Object e);
    public Object delLast();
    public BinTreePosition posOfNode(int i);
}

…snip...


References:
http://sillycat.iteye.com/blog/2382186



猜你喜欢

转载自sillycat.iteye.com/blog/2383334