码迷,mamicode.com
首页 > 编程语言 > 详细

java数据结构(二叉树)

时间:2017-04-02 14:27:26      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:数据结构   roo   logs   class   public   return   new   java数据结构   blog   

Node节点:

 1 public class Node {
 2 public long data;
 3 public String sData;
 4 public Node leftChild;
 5 public Node rightChild;
 6 public Node(long data,String sData) {
 7 this.data = data;
 8 this.sData = sData;
 9 }
10 }

Tree:

 1 public class Tree {
 2     public Node root;
 3     
 4     public void insert(long value,String sValue){
 5         Node newNode = new Node(value,sValue);
 6         Node current = root;
 7         Node parent;
 8         if(root == null){
 9             root = newNode;
10             return;
11         }else{
12             while(true){
13                 parent = current;
14                 if(current.data > value){
15                     current = current.leftChild;
16                     if(current == null){
17                         parent.leftChild = newNode;
18                         return ;
19                     }
20                 }else{
21                     current = current.rightChild;
22                     if(current == null){
23                         parent.rightChild = newNode;
24                         return;
25                     }
26                 }
27             }
28         }
29     }
30     
31     public Node find(long value){
32         Node current = root;
33         while(current.data != value){
34             if(current.data >  value){
35                 current = current.leftChild;
36             }else{
37                 current = current.rightChild;
38             }
39             if(current == null){
40                 return null;
41             }
42         }
43         return current;
44     }
45 }

 

java数据结构(二叉树)

标签:数据结构   roo   logs   class   public   return   new   java数据结构   blog   

原文地址:http://www.cnblogs.com/bianqi/p/6658991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!