Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 309|回复: 0

[泛型学习]泛型二叉树

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-30 23:56:44 | 显示全部楼层 |阅读模式
    一、泛型二叉树
    1. public class BinaryTree< T extends Comparable< T>> {//二叉树
    2.   LinkedList< T> values;            //一个链表              
    3.   private Node root;           // 根节点
    4.   
    5.   public void add(T value) {//在树中添加数据
    6.     if(root == null) {                 
    7.       root = new Node(value);         
    8.     } else {                           
    9.       add(value, root);               
    10.     }
    11.   }
    12.   // 在树中递归插入一个对象
    13.   private void add(T value, Node node) {
    14.     int comparison = node.obj.compareTo(value);
    15.     if(comparison == 0) {            
    16.       ++node.count;                    
    17.       return;
    18.     }
    19.     if(comparison > 0) {               
    20.       if(node.left == null) {         
    21.         node.left = new Node(value);   
    22.       } else {                        
    23.         add(value, node.left);         
    24.       }
    25.     } else {                           
    26.       if(node.right == null) {         
    27.         node.right = new Node(value);
    28.       } else {
    29.         add(value, node.right);
    30.       }
    31.     }
    32.   }

    33.   public LinkedList< T> sort() {//排序
    34.     values = new LinkedList< T>();      
    35.     treeSort(root);                    
    36.     return values;
    37.   }
    38.   
    39.   
    复制代码

      
    1. private void treeSort(Node node) {
    2.     if(node != null) {                 
    3.       treeSort(node.left);            
    4.       // List the duplicate objects for the current node
    5.       for(int i = 0 ; i< node.count ; i++) {
    6.         values.addItem(node.obj);
    7.        }
    8.       treeSort(node.right);            // Now process the right child
    9.     }
    10.   }

    11.   // 定义节点类
    12.   private class Node {
    13.     Node(T value) {
    14.       obj = value;
    15.       count = 1;
    16.     }
    17.    
    18.     T obj;                                       // 存储在节点中的对象
    19.     int count;                                   // 相同节点的数目
    20.     Node left;                                   // 左孩子节点
    21.     Node right;                                  // 右孩子点
    22.   }
    23. }
    复制代码

    二、泛型链表
    1. import java.util.Iterator;
    2. import java.util.NoSuchElementException;
    3. public class LinkedList< T> implements Iterable< T> {//泛型链表
    4.   private ListItem start = null;    //头节点
    5.   private ListItem end = null;      //未节点
    6.   private ListItem current = null;  //当前节点

    7.   public LinkedList() {}//构造函数

    8.   public LinkedList(T item) {//构造函数
    9.     if(item != null) {
    10.       current=end=start=new ListItem(item);     
    11.     }
    12.   }
    13.   
    14.   public LinkedList(T[] items) {//构造函数
    15.     if(items != null) {
    16.       
    17.       for(T item : items) {
    18.         addItem(item);
    19.       }
    20.       current = start;
    21.     }
    22.   }
    23.   
    24.   public void addItem(T item) {//往链表中添加节点
    25.     ListItem newEnd = new ListItem(item);   
    26.     if(start == null) {                     
    27.       start = end = newEnd;                 
    28.     } else {                                
    29.       end.next = newEnd;                  
    30.       end = newEnd;                        
    31.     }
    32.   }
    33.   
    34.   public T getFirst() {//获取链表中第一个节点的数据
    35.     current = start;
    36.     return start == null ? null : start.item;
    37.   }
    38.   
    39.   public T getNext() { // 获取下一个节点中的数据
    40.     if(current != null) {
    41.       current = current.next;           
    42.     }
    43.     return current == null ? null : current.item;
    44.   }
    45.   
    46.   public Iterator< T> iterator() {//返回链表的一个Iterator对象
    47.     return new ListIterator();
    48.   }

    49.   private class ListIterator implements Iterator< T> {
    50.    
    51.     public ListIterator() {//构造函数
    52.       nextElement = start;
    53.     }
    54.    
    55.     public boolean hasNext() {//链表中是否还有节点
    56.       return nextElement != null;
    57.     }
    58.     public T next() {//获取链表中的下一个节点
    59.       if(nextElement == null) {
    60.         throw new java.util.NoSuchElementException();
    61.       }
    62.       T element = nextElement.item;
    63.       nextElement = nextElement.next;
    64.         return element;
    65.     }
    66.    
    67.     public void remove() {
    68.       throw new IllegalStateException();
    69.     }
    70.     ListItem nextElement;     // The next element from the iterator
    71.   }
    72.   private class ListItem {//节点类
    73.    
    74.     public ListItem(T item) {
    75.       this.item = item;                  
    76.       next = null;                     
    77.     }
    78.    
    79.     public String toString() {
    80.       return "ListItem " + item ;
    81.     }
    82.     ListItem next;  //下一节点                    
    83.     T item;   //节点中的数据                           
    84.   }
    85. }
    复制代码
      三、测试
    1. public class TryBinaryTree {
    2.   public static void main(String[] args) {
    3.    
    4.     //创建存储到树中的整型数组
    5.     int[] numbers = new int[30];
    6.     for(int i = 0 ; i< numbers.length ; i++) {
    7.       numbers[i] = (int)(1000.0*Math.random());   // 0到999的随机数
    8.     }
    9.    
    10.     int count = 0;
    11.     System.out.println("Original values are:");
    12.     for(int number : numbers) {
    13.       System.out.printf("%6d", number);
    14.       if(++count%6 == 0) {
    15.         System.out.println();
    16.       }
    17.     }
    18.     // 创建树并添加数据
    19.     BinaryTree
    20.    tree = new BinaryTree
    21.   
    22.    ();
    23.     for(int number:numbers) {
    24.       tree.add(number);
    25.     }
    26.     // 获取并输出树中的数据
    27.     LinkedList
    28.    
    29.      values = tree.sort();
    30.     count = 0;
    31.     System.out.println("
    32. Sorted values are:");
    33.     for(Integer value : values) {
    34.       System.out.printf("%6d", value);
    35.       if(++count%6 == 0) {
    36.         System.out.println();
    37.       }
    38.     }
    39.      //创建存储到树中的字符串数组
    40.     String[] words = {"vacillate", "procrastinate", "arboreal", "syzygy", "xenocracy",
    41.                          "zygote", "mephitic", "soporific", "grisly", "gristly" };
    42.    
    43.     System.out.println("
    44. Original word sequence:");
    45.     for(String word : words) {
    46.       System.out.printf("%-15s", word);
    47.         if(++count%5 == 0) {
    48.           System.out.println();
    49.         }
    50.     }
    51.     BinaryTree
    52.    
    53.       cache = new BinaryTree
    54.      
    55.       ();
    56.     for(String word : words) {
    57.       cache.add(word);
    58.     }
    59.     //排序
    60.     LinkedList
    61.       
    62.         sortedWords = cache.sort();
    63.    
    64.     System.out.println("
    65. Sorted word sequence:");
    66.     count = 0;
    67.     for(String word : sortedWords) {
    68.       System.out.printf("%-15s", word);
    69.       if(++count%5 == 0) {
    70.         System.out.println();
    71.       }
    72.     }
    73.   }
    74. }
    75.       
    76.      
    77.    
    78.    
    79.   
    复制代码
      运行结果:
    C:Test>java TryBinaryTree
    Original values are:
    223 874 886 890 177 265
    173 906 561 771 470 900
    838 999 280 637 697 231
    234 735 88 729 449 167
    248 425 758 383 543 835 Sorted values are:
    88 167 173 177 223 231
    234 248 265 280 383 425
    449 470 543 561 637 697
    729 735 758 771 835 838
    874 886 890 900 906 999 Original word sequence:
    vacillate procrastinate arboreal syzygy xenocracy
    zygote mephitic soporific grisly gristly Sorted word sequence:
    arboreal grisly gristly mephitic procrastinate
    soporific syzygy vacillate xenocracy zygote C:Test>
      

      
      
       
       

         
       

         
       
      


    源码下载:http://file.javaxxz.com/2014/10/30/235643906.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-18 23:52 , Processed in 0.379898 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表