一、二叉查找树
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()); } }
再来一张图来
相关推荐
二叉排序树,又称二叉查找树或二叉搜索树,是一种特殊的数据结构,它具有以下特性:对于树中的任意节点,其左子树中的所有节点的值都小于该节点的值,而右子树中所有节点的值都大于该节点的值。这种特性使得在二叉...
总结来说,"数据结构实验之二叉排序树"是一个实践性的学习项目,旨在通过动手实现二叉排序树的查找、插入和删除操作,来巩固和加深对数据结构理论知识的理解。在这个过程中,学生将学习到如何使用MFC来构建交互式...
在数据结构的学习中,查找算法是至关重要的一个环节,它涉及到如何高效地在数据集合中找到特定元素。本文将详细探讨四种常见的查找算法:顺序查找、折半查找、二叉排序树查找以及哈希表查找,并结合提供的"综合查找...
二叉排序树(Binary Search Tree, BST),又称为二叉查找树或有序二叉树,是一种特殊的二叉树数据结构。它具有以下性质: 1. 若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值; 2. 若任意节点...
在IT领域,数据结构是计算机科学的基础,而二叉排序树(Binary Sort Tree,BST)作为其中的一种重要数据结构,有着广泛的应用。本实验程序旨在深入理解和掌握二叉排序树的基本操作,包括动态创建、输出、判断、查找...
一、实验目的 (1)理解动态查找表的动态生成过程; (2)任意给出一组数(不...(1)定义二叉排序树的数据结构; (2)实现二叉排序树的插入算法与查找算法,并建立二叉排序树; (3)进行数据查找和建树过程的比较。
二叉排序树,又称二叉查找树或二叉搜索树,是数据结构中的一种重要类型,主要用于高效地存储和检索数据。它具有以下关键特性: 1. **定义**:二叉排序树是一种特殊的二叉树,其中每个节点的左子树只包含比该节点小...
二叉排序树(Binary Sort Tree,BST)是一种特殊的二叉树数据结构,它具有以下特性:每个节点的左子树只包含比当前节点小的元素,而右子树则只包含比当前节点大的元素。这种结构使得二叉排序树在插入、删除和查找...
实验内容:建立有n个元素的二叉排序树,并在其上进行查找。 实验说明:(1)建立n个元素的二叉树,以链式结构存储,数据元素为整型。(2)在该二叉树上查找某数据,若查找成功则输出成功信息,若查找失败,则插入该数据...
二叉排序树(Binary Search Tree),也称作二叉查找树或有序二叉树,是一种特殊的二叉树数据结构。它具有以下性质: 1. 若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值; 2. 若任意节点的右子树...
数据结构是计算机科学中的核心概念,它涉及到如何高效地存储...通过这次课程设计,你不仅将深入理解二叉平衡排序树的原理,还将锻炼到实际问题的解决能力和编程技巧,为未来在数据结构与算法领域的发展打下坚实的基础。
二叉排序树(Binary Search Tree, BST),又称二叉查找树或有序二叉树,是一种特殊的二叉树,其特点在于对于任意节点而言,其左子树中的所有节点的值均小于该节点的值,而右子树中的所有节点的值均大于该节点的值。...
总而言之,二分查找与二叉查找树都是高效处理排序数据的算法和数据结构,它们在计算机科学和软件工程中扮演着重要的角色,尤其是在需要快速查找和排序的场景中。掌握这两者的原理和应用,对于提升编程能力和解决实际...
老师给的资源,对于数据结构入门的学生很有帮助的。
采用C++语言编写的程序,二叉排序树的查找方法简单。
二叉排序树,又称二叉查找树或二叉搜索树,是一种特殊的二叉树数据结构,它具有以下特性:对于树中的每个节点,其左子树中的所有节点的值都小于该节点的值,而右子树中的所有节点的值都大于该节点的值。这种性质使得...
二叉排序树。用二叉链表作存储结构。 要求: (1)以回车(0)为输入结束标志,输入数列L,生成一棵二叉排序...(4)输入元素x,查找二叉排序树T,若存在含x的结点,则删除该结点,并作中序遍历(执行操作2);否则输出信息“无x”
二叉查找树(Binary Search Tree, BST)是一种特殊的数据结构,它在计算机科学中用于高效地存储和检索数据。在二叉查找树中,每个节点包含一个键(key)、一个关联的值、一个指向左子节点的引用以及一个指向右子节点...
在这个实验中,我们专注于四个关键的算法和数据结构:单链表的基本操作、二叉树的遍历、折半查找以及二叉排序树,还有内部排序的实现。 首先,单链表是一种线性数据结构,其中每个元素(节点)包含数据和一个指向下...