码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树的构建与显示

时间:2016-10-02 12:47:43      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

这里只实现二叉树的增加节点和输出功能。

要实现二叉树排序,其节点数据必须实现Comparable接口。

package org.lyk.entities;

public class Book implements Comparable<Book>
{
    private String name;
    private double price;
    

    public Book(String name,double price)
    {
        super();
        this.name = name;
        this.price = price;
    }
    
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public double getPrice()
    {
        return price;
    }

    public void setPrice(double price)
    {
        this.price = price;
    }

    @Override
    public String toString()
    {
        return "Book [name=" + name + ", price=" + price + "]";
    }

    @Override
    public int compareTo(Book o)
    {
        if(this.price < o.price)
            return -1;
        else if(this.price > o.price)
            return 1;
        else 
            return 0;
    } 
    
    
}

实现二叉树:

package org.lyk.entities;
 
public class BinaryTree<T>
{
    private class Node
    {
        private Comparable<T> data;
        private Node left;
        private Node right;
        
        
        private Node(Comparable<T> data)
        {
            this.data = data;
        }
        
        @SuppressWarnings("unchecked")
        private void add(Comparable<T> data)
        {
            if(this.data.compareTo((T) data) < 0)
            {
                if(this.right == null)
                    this.right = new Node(data);
                else
                    this.right.add(data);
            }
            else
            {
                if(this.left == null)
                    this.left = new Node(data);
                else
                    this.left.add(data);
            }
        }
        
        private void get(Object[] retVal)
        {
            if(this.left != null)
                this.left.get(retVal);
            
            retVal[BinaryTree.this.foot++] = this.data;
            
            if(this.right != null)
                this.right.get(retVal);
        }
    }
    //=======================================
    private Node root;
    private int count;
    private int foot;
    public void add(Comparable<T> data)
    {
        if(this.root == null)
            this.root = new Node(data);
        else
            this.root.add(data);
        
        this.count++;
    }
    
    public Object[] toArray()
    {
        if(this.root == null)
            return null;
        
        Object[] retVal = new Object[this.count];
        this.foot = 0;
        this.root.get(retVal);
        return retVal;
    }
}

测试代码:

package org.lyk.main;

import org.lyk.entities.*;
import org.lyk.interfaces.*;
  
import java.math.*; 
import java.sql.*;
import java.text.*; 
import java.util.*; 
 



public class Main
{
    public static void main(String[] args) 
    {
        Book[] books = new Book[]
        {
            new Book("Java编程思想", 73.8),
            new Book("Java从入门到精通", 40.7),
            new Book("疯狂Java讲义(第3版 附光盘)", 91.3),
            new Book("O‘Reilly:Head First Java", 47.3),
            new Book("Java Web整合开发王者归来", 78.8)
        };
        
        BinaryTree<Book> bt = new BinaryTree<>();
        for(Book item : books)
        {
            bt.add(item);
        }
        Object[] result = bt.toArray();
        for(Object item : result)
        {
            System.out.println(item);
        }
    } 
}

 

二叉树的构建与显示

标签:

原文地址:http://www.cnblogs.com/kuillldan/p/5927266.html

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