二叉树的链表存储

二叉树的链表存储,就是让每个节点都能保存指向其左右节点的信息。为每个节点添加left和right两个指向(指针),分别引用该节点的左右节点。

首先定义节点:

[java]  view plain copy
  1. package my.bintree;  
  2.   
  3. public class TreeNode<T> {  
  4.     private T data;  
  5.     private TreeNode<T> left;  
  6.     private TreeNode<T> right;  
  7.       
  8.     public TreeNode(){    
  9.     }  
  10.       
  11.     public TreeNode(T data){  
  12.         this.data = data;  
  13.     }  
  14.       
  15.     public TreeNode(T data, TreeNode<T> left, TreeNode<T> right){  
  16.         this.data = data;  
  17.         this.left = left;  
  18.         this.right = right;  
  19.     }  
  20.   
  21.     public Object getData() {  
  22.         return data;  
  23.     }  
  24.   
  25.     public void setData(T data) {  
  26.         this.data = data;  
  27.     }  
  28.   
  29.     public TreeNode<T> getLeft() {  
  30.         return left;  
  31.     }  
  32.   
  33.     public void setLeft(TreeNode<T> left) {  
  34.         this.left = left;  
  35.     }  
  36.   
  37.     public TreeNode<T> getRight() {  
  38.         return right;  
  39.     }  
  40.   
  41.     public void setRight(TreeNode<T> right) {  
  42.         this.right = right;  
  43.     }  
  44. }  

然后,实现二叉树的链表存储,其中获取二叉树的深度为关键操作。

[java]  view plain copy
  1. package my.bintree;  
  2.   
  3. public class LinkBinTree<T> {  
  4.     private TreeNode<T> root;  
  5.       
  6.     public LinkBinTree(TreeNode<T> root){  
  7.         this.root = root;  
  8.     }  
  9.       
  10.     public LinkBinTree(T data){  
  11.         this.root = new TreeNode<T>(data);  
  12.     }  
  13.   
  14.     public boolean isEmpty(){  
  15.         return root == null;  
  16.     }  
  17.       
  18.     //获取根节点,如果非空  
  19.     public TreeNode<T> getRoot(){  
  20.         if(isEmpty()){  
  21.             throw new RuntimeException("tree is empty");  
  22.         }  
  23.           
  24.         return this.root;  
  25.     }  
  26.       
  27.     //获取指定节点(非根节点)的父节点,需要遍历整个二叉树  
  28.     public TreeNode<T> getParent(TreeNode<T> node){  
  29.         return null;  
  30.     }  
  31.       
  32.     //返回指定节点(非叶子节点)的左节点的数据,如果左节点不在返回null  
  33.     @SuppressWarnings("unchecked")  
  34.     public T leftChild(TreeNode<T> parent){  
  35.         if(parent == null){  
  36.             throw new RuntimeException("节点为空");  
  37.         }  
  38.           
  39.         return parent.getLeft() == null ? null : (T)parent.getLeft().getData();  
  40.     }  
  41.       
  42.     //返回指定节点(非叶子节点)的右节点的数据,如果左节点不在返回null  
  43.     @SuppressWarnings("unchecked")  
  44.     public T rightChild(TreeNode<T> parent){  
  45.         if(parent == null){  
  46.             throw new RuntimeException("节点为空");  
  47.         }  
  48.           
  49.         return parent.getRight() == null ? null : (T)parent.getRight().getData();  
  50.     }  
  51.       
  52.     /** 
  53.      * 为指定节点添加子节点 
  54.      * @param parent 需要添加左右节点的节点 
  55.      * @param data 子节点的数据 
  56.      * @param isLeft 是否为左右节点:true 左节点,false 右节点 
  57.      * @return 
  58.      */  
  59.     public TreeNode<T> addNode(TreeNode<T> parent, T data, boolean isLeft){  
  60.         if(parent == null){  
  61.             throw new RuntimeException("节点为空,不能添加子节点");  
  62.         }  
  63.           
  64.         if(isLeft && parent.getLeft() != null){  
  65.             throw new RuntimeException("节点也有左节点,不能添加子节点");  
  66.         }  
  67.           
  68.         if(!isLeft && parent.getRight() != null){  
  69.             throw new RuntimeException("节点也有右节点,不能添加子节点");  
  70.         }  
  71.           
  72.         TreeNode<T> newNode = new TreeNode<T>(data);  
  73.         if(isLeft){  
  74.             parent.setLeft(newNode);  
  75.         }else{  
  76.             parent.setRight(newNode);  
  77.         }  
  78.           
  79.         return newNode;  
  80.     }  
  81.       
  82.     public int getDeep(){  
  83.         return getDeep(this.root);  
  84.     }  
  85.       
  86.     //采用递归的方式来获取节点的深度  
  87.     public int getDeep(TreeNode<T> node){  
  88.         if(node == null){  
  89.             return 0;  
  90.         }  
  91.           
  92.         if(node.getLeft() == null && node.getRight() == null){  
  93.             return 1;  
  94.         }else{  
  95.             int leftDeep = getDeep(node.getLeft());  
  96.             int rightDeep = getDeep(node.getRight());  
  97.             int max = leftDeep > rightDeep ? leftDeep : rightDeep;  
  98.             //如果该节点有子树,必须计算一次  
  99.             return max + 1;  
  100.         }  
  101.     }  
  102. }  

最后,编写一个客户端,来时测试二叉树的链表存储是否正确:

[java]  view plain copy
  1. package my.bintree;  
  2.   
  3. public class LinkBinTreeClient {  
  4.     public static void main(String[] args){  
  5.         LinkBinTree<String> tree = new LinkBinTree<String>("root");  
  6.         TreeNode<String> b = tree.addNode(tree.getRoot(), "left"true);  
  7.         TreeNode<String> c = tree.addNode(tree.getRoot(), "right"false);  
  8.         tree.addNode(b,"left",true);  
  9.         System.out.println(tree.toString());  
  10.         System.out.println(tree.getDeep());  
  11.     }  
  12. }  

控制台输出:

my.bintree.LinkBinTree@1fc4bec
3

注:未复写toString函数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值