平衡二叉树,java实现

public   class  MyBinaryTree
ExpandedBlockStart.gif
{
    
private MyNode root;// 根节点
    private MyBinaryTree left;// 左子树
    private MyBinaryTree right;// 右子树
    
ExpandedSubBlockStart.gif    
/**
     * 添加一个数 将数值插入到二叉树中,比当前结点小或等于当前结点的插在当前结点的左侧,比当前结点大的数插在当前结点的右侧,每次从根结点开始递归比较
     
*/

    
public void addData(int n)
ExpandedSubBlockStart.gif    
{
        
if (root == null)
ExpandedSubBlockStart.gif        
{
            root 
= new MyNode();
            root.setData(n);
        }

        
else
ExpandedSubBlockStart.gif        
{
            
int data = root.getData();
            
if (n <= data)
ExpandedSubBlockStart.gif            
{
                
if (this.left == null)
ExpandedSubBlockStart.gif                
{
                    
this.left = new MyBinaryTree();
                }

                
this.left.addData(n);
            }

            
else
ExpandedSubBlockStart.gif            
{
                
if (this.right == null)
ExpandedSubBlockStart.gif                
{
                    
this.right = new MyBinaryTree();
                }

                
this.right.addData(n);
            }

        }

    }

    
ExpandedSubBlockStart.gif    
/**
     * 先序排序
     
*/

    
public void preorder()
ExpandedSubBlockStart.gif    
{
        
if (this.root != null)
ExpandedSubBlockStart.gif        
{
            System.out.print(root.getData() 
+ ",");
        }

        
if (this.left != null)
ExpandedSubBlockStart.gif        
{
            
this.left.preorder();
        }

        
if (this.right != null)
ExpandedSubBlockStart.gif        
{
            
this.right.preorder();
        }

    }

    
ExpandedSubBlockStart.gif    
/**
     * 中序排序
     
*/

    
public void inorder()
ExpandedSubBlockStart.gif    
{
        
if (this.left != null)
ExpandedSubBlockStart.gif        
{
            
this.left.inorder();
        }

        
if (this.root != null)
ExpandedSubBlockStart.gif        
{
            System.out.print(root.getData() 
+ ",");
        }

        
if (this.right != null)
ExpandedSubBlockStart.gif        
{
            
this.right.inorder();
        }

    }

    
ExpandedSubBlockStart.gif    
/**
     * 后序排序
     
*/

    
public void postorder()
ExpandedSubBlockStart.gif    
{
        
if (this.left != null)
ExpandedSubBlockStart.gif        
{
            
this.left.postorder();
        }

        
if (this.right != null)
ExpandedSubBlockStart.gif        
{
            
this.right.postorder();
        }

        
if (this.root != null)
ExpandedSubBlockStart.gif        
{
            System.out.print(root.getData() 
+ ",");
        }

    }

    
    
public static void main(String[] args)
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
int[] arr = 28749316,0,5 };
        MyBinaryTree bt 
= new MyBinaryTree();
        
for (int i = 0; i < arr.length; i++)
ExpandedSubBlockStart.gif        
{
            bt.addData(arr[i]);
        }

        System.out.println(
"先序:");
        bt.preorder();
        System.out.println(
"\n中序:");
        bt.inorder();
        System.out.println(
"\n后序:");
        bt.postorder();
    }

    
}


ExpandedBlockStart.gif
/**
 * 节点对象
 
*/

class  MyNode
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif    
/** 存储的数据 */
    
private int data;
    
    
public int getData()
ExpandedSubBlockStart.gif    
{
        
return data;
    }

    
    
public void setData(int data)
ExpandedSubBlockStart.gif    
{
        
this.data = data;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值