二叉查找树 数组实现 ArrayBinarySearchTree

数组实现的二叉查找树

  • 适用查找操作频繁,插入、删除操作较少的情况

代码

using System;
namespace DataStructure
{
  /*
  数组实现的二叉查找树
  输入一个数组,获得一颗二叉查找树
  
  找parent 和 child index的数值规律
  0
  1,2
  3,4,5,6
  7,8,9,10,11,12,13,14
  15,16,17,18

  layer: 1,2,4,8
  次方:0,1,2,3

  得
  parent = (child - 1) / 2
  childRight = (parent + 1) * 2
  childLeft = (parent + 1) * 2 - 1

   */
  public class ArrayBinarySearchTree
  {
    TreeNode[] nodeArray;

    public ArrayBinarySearchTree(int[] values)
    {
      /*
      计算数组长度需要计算有多少层,然后把所有层的节点个数加起来
      假设每个节点除了根节点都是右孩子,极限情况,每层一个节点,这样计算出最多需要多大空间
       */
      nodeArray = new TreeNode[ComputeAllCount(values.Length)];
      for (var i = 0; i < values.Length; i++)
      {
        Insert(values[i]);
      }
    }

    public int Insert(int value)
    {
      return Insert(0, value);
    }

    public int Insert(int rootIndex, int value)
    {
      var node = nodeArray[rootIndex];
      if (nodeArray[rootIndex] == null)
      {
        nodeArray[rootIndex] = new TreeNode(value);
        return rootIndex;
      }

      if (value < nodeArray[rootIndex].val)
      {
        return Insert(GetLeftChildIndex(rootIndex), value);
      }
      else
      {
        return Insert(GetRightChildIndex(rootIndex), value);
      }
    }
    
    public TreeNode Find(int val)
    {
      return Find(0, val);
    }

    public TreeNode Find(int rootIndex, int value)
    {
      var node = nodeArray[rootIndex];
      if (node == null)
      {
        return null;
      }
      if(value == node.val){
        return node;
      }
      if (value < node.val)
      {
        return Find(GetLeftChildIndex(rootIndex), value);
      }
      else
      {
        return Find(GetRightChildIndex(rootIndex), value);
      }
    }

    public int FindIndex(int value){
      return FindIndex(0, value);
    }

    public int FindIndex(int rootIndex, int value){
      var node = nodeArray[rootIndex];
      if (node == null)
      {
        return -1;
      }
      if(value == node.val){
        return rootIndex;
      }
      if (value < node.val)
      {
        return FindIndex(GetLeftChildIndex(rootIndex), value);
      }
      else
      {
        return FindIndex(GetRightChildIndex(rootIndex), value);
      }
    }

    public static int GetLayerCount(int layer)
    {
      return (int)Math.Pow(2, layer - 1);
    }

    public static int ComputeAllCount(int layerCount)
    {
      var sum = 0;
      for (var i = 1; i <= layerCount; i++)
      {
        sum += GetLayerCount(i);
      }
      return sum;
    }

    public static int GetLeftChildIndex(int index)
    {
      return (index + 1) * 2 - 1;
    }

    public static int GetRightChildIndex(int index)
    {
      return (index + 1) * 2;
    }

    public static int GetParentIndex(int index)
    {
      return (index - 1) / 2;
    }
  }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值