二叉树(一)

通过看书还有看高手们的博客,总结了一下二叉树:

 

(图片来自网上)

以上图的二叉树为例:

package com.jian.tree;

public class BinaryTree {
   public static void main(String[] args) {
      Tree root = init('A') ;//初始化根节点
      
      insertNode(root, 'A' , 'B') ; //添加节点
      insertNode(root, 'B' , 'C') ;
      insertNode(root, 'B' , 'D') ;
      insertNode(root, 'D' , 'E') ;
      insertNode(root, 'D' , 'F') ;
      insertNode(root, 'E' , 'G') ;
      
      System.out.print("先序遍历的结果是:") ;
      DLR(root) ; 
      System.out.println();
      System.out.print("先序遍历的结果是:") ;
      LDR(root) ;
      System.out.println();
      System.out.print("先序遍历的结果是:") ;
      LRD(root) ;
      System.out.println();
      System.out.println("树的深度为:" + getDepth(root));
   }
   
   
   //树的初始化,添加节点,查找节点
   public static Tree init(char data){ //初始化这个二叉树
       Tree root = new Tree() ;
       root.setData(data) ; //根的数值
       root.setLeft(null) ; //暂时设为null
       root.setRight(null) ;//暂时设为null
       
       return root ;
   }
   
   public static void insertNode(Tree tree , char key , char value){
       //key是父节点
       //value是将要添加的节点的值
       Tree parent = null ;
       Tree node = null ;  
      
       parent = findNode(tree, key) ;
       if(parent==null){
           System.out.println("错误:未找到父节点") ;
           node = null ;
           return ;
       }
       node = new Tree() ;//创建一个新的节点
       node.setData(value) ;
       node.setLeft(null) ;
       node.setRight(null) ;
       
       if(parent.getLeft()==null){ //左子树为空,则添加到左子树
           parent.setLeft(node) ;
       }else if(parent.getRight()==null){//右子树为空,则添加到右子树
           parent.setRight(node) ;
       }else return ;
   }
   
   public static Tree findNode(Tree tree , char key){  //根据key查找父节点
       Tree parent ;
       if(tree==null){
           return null ;
       }else {
           if(tree.getData()==key){
               return tree ;
           }
           else {
               if((parent=findNode(tree.getLeft(), key))!=null){  //递归查找
                  return parent ;
               }else if((parent=findNode(tree.getRight(), key))!=null){
                  return parent ;
               }
            else {
               return null;
            }
       }
       }
   }
   
   
   //-------------------------------------------树的遍历
   public static void DLR(Tree tree){  //先序遍历(递归)
       if(tree!=null){
           System.out.print(tree.getData()+" ");
           DLR(tree.getLeft()) ;
           DLR(tree.getRight()) ;
       }
   }
   public static void LDR(Tree tree){  //中序遍历(递归)
       if(tree!=null){
           LDR(tree.getLeft()) ;
           System.out.print(tree.getData()+" ");
           LDR(tree.getRight()) ;
       }
   }
   public static void LRD(Tree tree){  //后序遍历(递归)
       if(tree!=null){
           LRD(tree.getLeft()) ;
           LRD(tree.getRight()) ;
           System.out.print(tree.getData()+" ");
       }
   }
   
   //-------------------------------------------求树的深度
   
   public static int getDepth(Tree tree){   //求树的深度(递归)
       int leftDepth = 0 ;
       int rightDepth = 0 ;
       
       if(tree==null){
           return 0 ;
       }
       else {
           leftDepth = getDepth(tree.getLeft()) ;
           rightDepth = getDepth(tree.getRight()) ;
           
           if(leftDepth>rightDepth){
               return leftDepth + 1 ;
           }
           else {
               return rightDepth +1 ;
           }
       }
   }
   
}

class Tree{
     private char data ;  //节点数据
     private Tree left ;  //左子树
     private Tree right ; //右子树
    public char getData() {
        return data;
    }
    public void setData(char data) {
        this.data = data;
    }
    public Tree getLeft() {
        return left;
    }
    public void setLeft(Tree left) {
        this.left = left;
    }
    public Tree getRight() {
        return right;
    }
    public void setRight(Tree right) {
        this.right = right;
    }
     
     
}



 程序运行结果:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

先序遍历的结果是:A B C D E G F
先序遍历的结果是:C B G E D F A
先序遍历的结果是:C G E F D B A
树的深度为:5


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值