`

数据结构之查找(二)二叉查找树

 
阅读更多

一、二叉查找树

    

        1.定义特点维基百科

        2.我当时在写实例化的方法的时候很犯愁

           =》理解RootNode为整个数中的一个中间节点key

                 =》Quick Sort的概念

           =》实例化的时候不用找到某两个节点的范围

           =》按照InOrder Traversal 结构式排序的顺序

 

二、基本实现

       =》节点结构

       =》查询

       =》添加

       =》实例化

       =》中序遍历

       =》效果图

       =》删除节点(实现了在)

 

//节点的基本结构 
 class TreeNode {
      private int num;
      private TreeNode leftChilder;
      private TreeNode rightChilder;

      //get\set method

}

 

//查询
  //当根为空
  //不为空
    //找到  
    //找不到
//search the BST
        public static Boolean searchBST(int key) { 
            // the root is null
            TreeNode temp = head;
            if (temp == null) return false;

            while (temp != null)
            {
                int num = temp.getNum();
                if (key == num) return true;
                if (key > num) temp = temp.getRightChilder();
                if (key < num) temp = temp.getLeftChilder();
            }
         return false;
        }

 

//插入数据
//首先查询数据
  //存在继续插入
  //不存在
    //如果Head为空新建
    //如果不为空找到位置
      //左边、右边知道它左边、右边为空
  public static void insertBST(int insertNum) {
            //first judege if exist it?
              //exist then continue
              //not exist then
                  //judge head
                  // find it
            if (searchBST(insertNum))
            {
                return;
            }
            else {
                if (head == null)
                {
                    head = new TreeNode();
                    head.setLeftChilder(null);
                    head.setRightChilder(null);
                    head.setNum(insertNum);
                    Console.WriteLine("===>insert ok...");
                    return;
                }
                else {
                    TreeNode headCopy = head;
                    TreeNode temp = new TreeNode();
                    temp.setNum(insertNum);
                    temp.setLeftChilder(null);
                    temp.setRightChilder(null);
                 
                    while (true)
                    {                          //find the left or right position
                        if (headCopy.getNum() > insertNum)
                        {
                            if (headCopy.getLeftChilder() == null)
                            {
                                headCopy.setLeftChilder(temp);
                                break;
                            }
                            headCopy = headCopy.getLeftChilder();
                        }
                        else
                        {
                            if (headCopy.getRightChilder() == null)
                            {
                                headCopy.setRightChilder(temp);
                                break;
                            }
                            headCopy = headCopy.getRightChilder();
                        }
                    }
                    Console.WriteLine("===>insert ok...");
                    return;
                }
             
            }
        }

 

//实例化查询树
//实例化很特别,不跟实例化二叉树一一样
//                    二叉树可以直接递归插入
//这里多了一个位置的查找过程
   //init a binary search tree;
        //special positon is the insert is not remove is a not change
        public static void initBST() { 
            try
            {
                while (true)
                {
                    Console.WriteLine("===>If you input -1 it will be end!");
                        Console.Write("===>Input you num:");

                    int data = Int32.Parse(Console.ReadLine());

                    if (data == -1) {
                        Console.WriteLine("The BST init successful...");
                        break;                                 //input the end
                    }
                    else
                    {
                        insertBST(data);
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("\n===>Yon Input Error! So this node is null!!!");
            }
        }

 

//外加一个左子树遍历
//刚好一个排序效果
  public static void inorderTravers(TreeNode t) {
            if (t != null && t.getNum() != -1) {
                inorderTravers(t.getLeftChilder());
                Console.WriteLine(t.getNum());
                inorderTravers(t.getRightChilder());
            }
   }

 

 

//删除效果
//首先判断节点存在不
  //不存在继续
  //存在
    //如果是叶子结点直接父节点指向NULL
      //如果只有左子树 左子树替代
    //如果只有右子树 右子树替代
    //如果左右子树都有
       //可以求出左边最大值 删除它然后替换
       //也可以求出右边最小值 删除然后替换
========================================
       //delete the BST
        //exists
          //   if not exists return;
          //   if exists
                 //if is the leaf node delete it
                 //if the rightChild is not exist just 
                 //if the leftChild is not exist just 
                 //if the all exists then
        public static void deleteBST(int deleteNum) {
            if (!searchBST(deleteNum))
            {
                Console.WriteLine("The number is not exist...");
            }
            else {
               //get the  node
                TreeNode temp = head;
                TreeNode parent = null;
                while (temp.getNum()!=deleteNum) { //if get the  
                    parent = temp;
                    if (temp.getNum() > deleteNum) temp = temp.getLeftChilder();
                    else if (temp.getNum() < deleteNum) temp = temp.getRightChilder();
                }

                //if the node is the leaf
                if (temp.getLeftChilder() == null && temp.getRightChilder() == null) {
                    if (deleteNum < parent.getNum()) parent.setLeftChilder(null);
                    else {
                        parent.setRightChilder(null);
                    }
                }

                //if the node's left is null
                else if (temp.getLeftChilder() == null)
                {
                    TreeNode right = temp.getRightChilder();

                    temp.setNum(right.getNum());
                    temp.setLeftChilder(right.getLeftChilder());
                    temp.setRightChilder(right.getRightChilder());
                }

                //if the node's right is null
                else if (temp.getRightChilder() == null)
                {
                    TreeNode left = temp.getLeftChilder();

                    temp.setNum(left.getNum());
                    temp.setLeftChilder(left.getLeftChilder());
                    temp.setRightChilder(left.getRightChilder());
                }

                //if have all the node
                    //find the left max
                    //find the right min
                else if (temp.getLeftChilder() != null && temp.getRightChilder() != null)
                {
                    //find the left max
                    TreeNode max = temp.getLeftChilder();
                    while (max.getRightChilder() != null) {
                        max = max.getRightChilder();
                    }
                    deleteBST(max.getNum());
                    temp.setNum(max.getNum());
                }
            }

 

 再来一张图来



 

  • 大小: 24 KB
  • 大小: 24.6 KB
分享到:
评论

相关推荐

    数据结构实验之二叉排序树

    这是数据结构实验中的二叉排序树,能够进行动态查找,将数据插入到二叉排序树中,或者从二叉排序树中删除某个元素。主要是查找功能!

    数据结构课程设计二叉排序树的实现

    二叉排序树的实现 二叉排序补充概念(也可以参考书上第九章第二节) 左子树的数据总是小于根和右子树的数据,这种就叫做二叉排序树,简单一点,二叉排序树左边的数据小于右边. ...课程设计数据结构二叉排序树

    数据结构___二叉排序树

    一、实验目的 (1)理解动态查找表的动态生成过程; (2)任意给出一组数(不...(1)定义二叉排序树的数据结构; (2)实现二叉排序树的插入算法与查找算法,并建立二叉排序树; (3)进行数据查找和建树过程的比较。

    综合查找算法(顺序查找、折半查找、二叉排序树、哈希表)-数据结构课程设计

    数据结构课程设计-综合查找算法(顺序查找、折半查找、二叉排序树、哈希表) 可以在Microsoft Visual C++ 上运行没有错误 包括论文word文档,答辩的ppt等

    《数据结构课程设计》二叉排序树基本操作实验程序

    1,动态创建二叉排序树 2,输出二叉排序树 3.判断是否为二叉排序树 4.输出查找某元素的路径 5.删除特定关键字结点

    二叉排序树的查找与建立

    老师给的资源,对于数据结构入门的学生很有帮助的。

    数据结构 二叉排序树的查找

    采用C++语言编写的程序,二叉排序树的查找方法简单。

    数据结构之二叉排序树的生成

    该程序演示二叉排序树的生成。 包括: 初始化二叉树 查找指定值 插入函数 打印二叉树 注:本程序为本人课堂实验程序,请勿到处转载!仅限学习交流使用!

    数据结构课程设计二叉搜索树

    二叉排序树。用二叉链表作存储结构。 要求: (1)以回车(0)为输入结束标志,输入数列L,生成一棵二叉排序...(4)输入元素x,查找二叉排序树T,若存在含x的结点,则删除该结点,并作中序遍历(执行操作2);否则输出信息“无x”

    数据结构实验——二叉排序树查找

    实验内容:建立有n个元素的二叉排序树,并在其上进行查找。 实验说明:(1)建立n个元素的二叉树,以链式结构存储,数据元素为整型。(2)在该二叉树上查找某数据,若查找成功则输出成功信息,若查找失败,则插入该数据...

    二叉查找排序树的实现代码

    最近在研究数据结构这本书,自己动手实现的一个二叉查找排序树的类BinSortTree,实现数据的插入,查找,删除,层序遍历,中序遍历等操作,熟悉数据结构的朋友都知道,根据二叉排序树的定义,中序遍历后得到的序列...

    折半查找、二叉排序树、链式哈希表的建立与查找

    折半查找、二叉排序树的建立、查找与删除、链式哈希表的建立与查找: 1————建立有序表———— 2————折半查找————— 3————建立二叉排序树—— 4————二叉排序树查找—— 5————二叉排序树...

    数据结构——二叉查找树(BST)静态查找表.zip

    数据结构——二叉查找树(BST)静态查找表,使用数据结构实现BST

    VC/C++实现二叉搜索树查找算法

    数据结构经典应用,VC/C++,模板类,顺序查找,二分查找,二叉搜索树

    AVL树数据结构平衡二叉查找树

    在计算机科学中,AVL树是最先发明的自平衡二叉查找树。在AVL树中任何节点的两个子树的高度最大差别为1,所以它也被称为高度平衡树。增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。AVL树得名于它的发明者...

    数据结构---二叉排序树

    数据结构实验程序---二叉排序树的生成。该课题是用来描述二叉排序树的,给定一列整型的数据(无序),按照二叉排序树的思想将该列数据排成一个有序的序列(通过中序遍历该二叉树...因此二叉排序树又称之为二叉查找树。

    数据结构 实现二叉排序树的各种算法(2)

    用函数实现如下二叉排序树算法: (1) 插入新结点 (2) 前序、中序、后序遍历二叉树 (3) 中序遍历的非递归算法 (4) 层次遍历二叉树 (5) 在二叉树中查找给定关键字(函数返回值为成功1,失败0) (6) ...

    第六课_二分查找与二叉查找树.pdf

    二分法查找法以及和二叉树查找的联系,部分二分搜索和二叉查找树的相关题目: 插入位置;区间查找;旋转位置查找;二叉查找树的编码和解码;逆序数的解题方法和代码实现

    数据结构实验-二叉排序树算法

    一、问题描述 根据给定的关键字序列,实现二叉排序树的基本操作。 输入格式:8, 10, 5, 6, 3, 13 二、实验目的 ...1、构造二叉排序树的存储结构。 2、实现创建、查找、插入、删除、平均查找长度等操作。

    数据结构课设--二叉排序树

    输入元素x,查找二叉排序树T,若存在含x的结点,则删除该结点,否则输出信息“无x”。 根据二叉排序树的概念,查找当前插入的元素的位置;删除结点如果不是叶子结点,要注意考虑如何使树仍为二叉排序树。

Global site tag (gtag.js) - Google Analytics