有关二叉树方法java实现

二叉树的实体类及方法成员:

  1 package mode;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 public class BinaryTree {
  7      private int data;
  8 
  9      private BinaryTree left;
 10 
 11      private BinaryTree right;
 12 
 13      public BinaryTree(int data) {
 14       this.data = data;
 15       left = null;
 16       right = null;
 17      }
 18 
 19     //插入节点
 20 
 21      public void insertTree(BinaryTree root, int data) {
 22       if (data >= root.data) {
 23        if (root.right == null) {
 24         root.right = new BinaryTree(data);
 25        } else {
 26         insertTree(root.right, data);
 27        }
 28       } else {
 29        if (root.left == null) {
 30         root.left = new BinaryTree(data);
 31        } else {
 32         insertTree(root.left, data);
 33        }
 34       }
 35      }
 36 
 37      // 结果返回树的高度
 38      public int height() {
 39       int heightOfTree;
 40       if (this == null)
 41        return -1;
 42       int leftHeight = (left == null ? 0 : left.height());
 43       int rightHeight = (right == null ? 0 : right.height());
 44       heightOfTree = leftHeight < rightHeight ? rightHeight : leftHeight;
 45       return 1 + heightOfTree;
 46      }
 47      
 48      //获得二叉树的节点总数
 49      public int size(BinaryTree root){
 50          if(root==null){
 51              return 0;
 52          }else{
 53              return 1+size(root.left)+size(root.right);
 54          } 
 55      }
 56      
 57      public List<BinaryTree> longestPath(BinaryTree root){
 58          List<BinaryTree> longPath=new ArrayList<BinaryTree>();
 59          List<BinaryTree> lPath=new ArrayList<BinaryTree>();
 60          List<BinaryTree> rPath=new ArrayList<BinaryTree>();
 61          if(root==null){
 62              return longPath;
 63          }else{
 64              longPath.add(root);
 65              lPath=longestPath(root.left);
 66              rPath=longestPath(root.right);
 67              if(lPath.size()>=rPath.size()){
 68                  longPath.addAll(lPath);
 69              }else{
 70                  longPath.addAll(rPath);
 71              }
 72              return longPath;
 73          }
 74      }
 75 
 76      // 前序遍历二叉树
 77      public void preOrder(BinaryTree parent) {
 78       if (parent == null)
 79        return;
 80       System.out.print(parent.data + " ");
 81       preOrder(parent.left);
 82       preOrder(parent.right);
 83      }
 84 
 85      // 中序遍历二叉树
 86      public void inOrder(BinaryTree parent) {
 87       if (parent == null)
 88        return;
 89       inOrder(parent.left);
 90       System.out.print(parent.data + " ");
 91       inOrder(parent.right);
 92      }
 93 
 94      // 后序遍历二叉树
 95      public void postOrder(BinaryTree parent) {
 96       if (parent == null)
 97        return;
 98       postOrder(parent.left);
 99       postOrder(parent.right);
100       System.out.print(parent.data + " ");
101 
102      }
103      
104      //左右树交换
105      public void swapBTree(BinaryTree root){
106          if(root!=null){
107              BinaryTree btmp=root.getLeft();
108              root.setLeft(root.getRight());
109              root.setRight(btmp);
110              swapBTree(root.left);
111              swapBTree(root.right);
112          }
113      }
114      //查找
115      public boolean searchkey(BinaryTree root, int key) {
116       boolean bl = false;
117       if (root == null) {
118        bl = false;
119        return bl;
120       } else if (root.data == key) {
121        bl = true;
122        return bl;
123       } else if (key >= root.data) {
124        return searchkey(root.right, key);
125       }
126       return searchkey(root.left, key);
127      }
128 
129     public int getData() {
130         return data;
131     }
132 
133     public void setData(int data) {
134         this.data = data;
135     }
136 
137     public BinaryTree getLeft() {
138         return left;
139     }
140 
141     public void setLeft(BinaryTree left) {
142         this.left = left;
143     }
144 
145     public BinaryTree getRight() {
146         return right;
147     }
148 
149     public void setRight(BinaryTree right) {
150         this.right = right;
151     }
152 }

实现测试类:

 1 import java.util.Iterator;
 2 import java.util.List;
 3 
 4 import mode.BinaryTree;
 5 public class TestBTree {
 6 
 7     /**测试二叉树查找
 8      * @param args
 9      */
10     public static void main(String[] args) {
11           int[] data = { 12, 11, 34, 45, 67, 89, 56, 43, 22, 98 };
12           BinaryTree root =new BinaryTree(data[0]);
13           for (int i = 1; i < data.length; i++) {
14            root.insertTree(root, data[i]);
15           }
16           System.out.println();
17           root.postOrder(root);
18           System.out.println();
19           root.preOrder(root);
20           System.out.println();
21           root.inOrder(root);
22           System.out.println(data[data.length - 1]);
23           int key = Integer.parseInt("34");
24           if (root.searchkey(root, key)) {
25            System.out.println("找到了:" + key);
26           } else {
27            System.out.println("没有找到:" + key);
28           }
29           System.out.println(root.size(root));
30           List<BinaryTree> list=null;
31           if(root!=null){
32               list=root.longestPath(root);
33           }
34           Iterator<BinaryTree> it =list.iterator();
35           while(it.hasNext()){
36               BinaryTree bt=it.next();
37               System.out.print(bt.getData()+"    ");
38           }
39           System.out.println();
40           root.swapBTree(root);
41           root.preOrder(root);  
42     }
43 }
View Code

 

 

 

 

转载于:https://www.cnblogs.com/smallBeans/p/3627532.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值