查找之二叉查找树

        数据结构查找篇的最后一贴,二叉查找树

        形成树型结构,在进行查找       

 

public class BTreeSearch
{
 public static int Max  = 10;
 public static int[] Data = { 15, 2, 13, 6, 17, 25, 37, 7, 3, 18 }; // 数据数组
 public static int Counter = 1;

 public static void main(String args[])
 {
  int i; // 循环计数变量
  BNTreeArray BNTree = new BNTreeArray(); // 声明二叉树数组

  BNTree.TreeData[0] = Data[0];

  for (i = 1; i < Max; i++)
   BNTree.Create(Data[i]); // 建立二叉查找树

  int KeyValue = 25;
  // 调用二叉查找法
  if (BNTree.BinarySearch(KeyValue) > 0)
   // 输出查找次数
   System.out
     .println("Search Time = " + BNTree.BinarySearch(KeyValue));
  else
   // 输出没有找到数据
   System.out.println("No Found!!");
 }
}

class BNTreeArray
{
 public static int MaxSize  = 20;
 public static int[] TreeData = new int[MaxSize];
 public static int[] RightNode = new int[MaxSize];
 public static int[] LeftNode = new int[MaxSize];

 public BNTreeArray()
 {
  int i; // 循环计数变量

  for (i = 0; i < MaxSize; i++)
  {
   TreeData[i] = 0;
   RightNode[i] = -1;
   LeftNode[i] = -1;
  }
 }

 // ----------------------------------------------------
 // 建立二叉树
 // ----------------------------------------------------
 public void Create(int Data)
 {
  int i; // 循环计数变量
  int Level = 0; // 树的阶层数
  int Position = 0;

  for (i = 0; TreeData[i] != 0; i++)
   ;

  TreeData[i] = Data;
  while (true) // 寻找节点位置
  {
   // 判断是左子树或是右子树
   if (Data > TreeData[Level])
   {
    // 右树是否有下一阶层
    if (RightNode[Level] != -1)
     Level = RightNode[Level];
    else
    {
     Position = -1; // 设定为右树
     break;
    }
   }
   else
   {
    // 左树是否有下一阶层
    if (LeftNode[Level] != -1)
     Level = LeftNode[Level];
    else
    {
     Position = 1; // 设定为左树
     break;
    }
   }
  }

  if (Position == 1) // 建立节点的左右连结
   LeftNode[Level] = i; // 连结左子树
  else
   RightNode[Level] = i; // 连结右子树
 }

 // ---------------------------------------------------------
 // 二叉查找法
 // ---------------------------------------------------------
 public static int BinarySearch(int KeyValue)
 {
  int Pointer; // 现在的节点位置
  int Counter; // 查找次数

  Pointer = 0;
  Counter = 0;
  while (Pointer != -1)
  {
   Counter++;
   // 找到了欲寻找之节点
   if (TreeData[Pointer] == KeyValue)
    return Counter; // 传回查找次数
   else if (TreeData[Pointer] > KeyValue)
    Pointer = LeftNode[Pointer]; // 往左子树找
   else
    Pointer = RightNode[Pointer];// 往右子树找
  }
  return 0; // 该节点不在此二叉树中
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值