Demo entry 5793029

Binary Tree

   

Submitted by anonymous on Jul 15, 2016 at 23:13
Language: Java. Code size: 2.2 kB.

package binTree;

public class BinaryTree 
{
    private Object data;
    private BinaryTree left;
    private BinaryTree right;
    private BinaryTree parent;
    
    public BinaryTree(Object data, BinaryTree parent)
    {
        this.data = data;
        this.parent = parent;
        
    }
    
    public BinaryTree left()
    {
        return this.left;
    }
    
    public BinaryTree right()
    {
        return this.right;
    }
    
    public void setLeftTree(BinaryTree left)
    {
        this.left = left;
    }
    
    public void setRightTree(BinaryTree right)
    {
        this.right = right;
    }
    
    public BinaryTree parent(){
        return this.parent;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
    
    public static void inorder(BinaryTree tree)
    {
        if(tree == null){
            return;
        }
        
        inorder(tree.left());
        System.out.print(tree.getData()+" ");
        inorder(tree.right());
    
    }
    
    public static void preorder(BinaryTree tree)
    {
        if(tree == null){
            return;
        }
        
        System.out.print(tree.getData()+" ");
        preorder(tree.left());
        preorder(tree.right());
    
    }
    
    public static void postorder(BinaryTree tree)
    {
        if(tree == null){
            return;
        }
        
        
        postorder(tree.left());
        postorder(tree.right());
        System.out.print(tree.getData()+" ");
    
    }
    
    public BinaryTree root(){
        
        BinaryTree t = this;
        while(t.parent!=null){
            t = t.parent;
        }
        
        return t;
        
    }
    
    public boolean isRoot(){
        return (this == this.root());
    }
    
    public boolean isExternal(){
        return (this.left == null && 
                this.right == null);
    }
    
    public boolean isInternal(){
        return (this.left != null ||
                this.right != null);
    }
    

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).