首页 > 代码库 > 二叉树的构建与显示

二叉树的构建与显示

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

要实现二叉树排序,其节点数据必须实现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 =http://www.mamicode.com/ 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);        }    } }

 

二叉树的构建与显示