文档详情

java实现二叉树排序

pia****nwu
实名认证
店铺
DOC
14.50KB
约3页
文档ID:167039414
java实现二叉树排序_第1页
1/3

用java实现一个二叉树排序  2012-04-30 23:42:07|  分类: java|举报|字号 订阅package paredemo;class Node //节点{ private Comparable data; // 保存操作的数据内容 private Node left; // 左子树 private Node right;// 右子树 public Node(Comparable data) {  this.data = data; } public void addNode(Node newNode) {  if (newNpareTo(this.data) <= 0)  { // 放在左子树   if (this.left == null)   {// 还没有左子树,可以直接保存在此节点下的左子树    this.left = newNode;// 保存左子树   }   else   {    this.left.addNode(newNode);// 向下继续判断   }  }  if (newNpareTo(this.data) > 0)  { // 放在右子树   if (this.right == null)   {// 还没有右子树,可以直接保存在此节点下的右子树    this.right = newNode;// 保存右子树   }   else   {    this.right.addNode(newNode);// 向下继续判断   }  } } public void printNode() { // 采用中序遍历  if (this.left != null)  {// 存在左子树   this.left.printNode(); // 继续找到下面的左子树  }  System.out.println(this.data); // 找到根内容  if (this.right != null)  {// 存在右子树   this.right.printNode(); // 继续找到下面的右子树  } }}class BinaryTree //二叉树结构{ // 定义二叉树的操作类 private Node root; // 根节点 public void add(Comparable data) {// 接收数据  Node newNode = new Node(data); // 实例化节点类  if (this.root == null)  {// 没有根节点   this.root = newNode; // 第一个节点作为根节点  }  else  {   this.root.addNode(newNode);  } } public void print() { // 输出  this.root.printNode();// 输出全部的节点 }}public class CompareableDemo03{ public static void main(String[] args) {  BinaryTree bt = new BinaryTree();  bt.add(3);  bt.add(5);  bt.add(1);  bt.add(0);  bt.add(1);  bt.add(9);  bt.print(); }}。

下载提示
相关文档
正为您匹配相似的精品文档