数据结构-二叉排序树
结点类代码实现:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _013_二叉树 { class BsNode { private BsNode LeftChild=null; private BsNode RightChild=null; private BsNode Parent=null; private int Data; internal BsNode rightChild { get => RightChild; set => RightChild = value; } internal BsNode leftChild { get => LeftChild; set => LeftChild = value; } internal BsNode parent { get => Parent; set => Parent = value; } internal int data { get => Data; set => Data = value; } public BsNode() { } public BsNode(int item) { this.data = item; } } }
排序树代码实现:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _013_二叉树 { /// <summary> /// 二叉排序树 /// </summary> class BsTree { private BsNode RootNode = null; /// <summary> /// 添加数据 /// </summary> /// <param name="item"></param> public void Add(int item) { BsNode newNode = new BsNode(item); if (RootNode == null)//空树 RootNode = newNode; else { BsNode temp = RootNode; while (true) { if (item >= temp.data)//放在temp的右边 { if (temp.rightChild == null) //如果右边为空 就直接添加 { temp.rightChild = newNode; newNode.parent = temp; break; } else //不为空 ,就赋值继续查找 { temp = temp.rightChild; } } else //放在temp的左边 { if (temp.leftChild == null) //如果左边为空 就直接添加 { temp.leftChild = newNode; newNode.parent = temp; break; } else//不为空 ,就赋值继续查找 { temp = temp.leftChild; } } } } } /// <summary> /// 查找 /// </summary> /// <param name="item"></param> /// <returns></returns> public bool Find(int item) { //return Find(item, RootNode); //方法二 BsNode temp = RootNode; while (true) { if (temp == null) return false; if (temp.data == item) return true; if (temp.data < item) { temp = temp.rightChild; } else { temp = temp.leftChild; } } } /// <summary> /// 查找方法1 /// </summary> /// <param name="item"></param> /// <param name="node"></param> /// <returns></returns> public bool Find(int item,BsNode node) { if (node == null) return false; if (node.data == item) return true; else { if (item > node.data) { return Find(item, node.rightChild); } else { return Find(item, node.leftChild); } } } public void MiddleTraversal() { MiddleTraversal(RootNode); } /// <summary> /// 中序遍历 /// </summary> /// <param name="node"></param> private void MiddleTraversal(BsNode node) { if (node == null) return; MiddleTraversal(node.leftChild); Console.Write(node.data+" "); MiddleTraversal(node.rightChild); } public bool Delete(int item) { BsNode temp = RootNode; while (true) { if (temp == null) return false; if (temp.data == item) { Delete(temp); return true; } if (temp.data < item) { temp = temp.rightChild; } else { temp = temp.leftChild; } } } private void Delete(BsNode node) { //第一种情况 删除叶子结点 if(node.leftChild == null && node.rightChild==null) { if (node.parent == null) { RootNode = null; } if (node.parent.leftChild == node) { node.parent.leftChild = null; } else if(node.parent.rightChild == node) { node.parent.rightChild = null; } return; } // 第二种情况 仅有左子树或者右子数的结点 if (node.leftChild==null && node.rightChild != null) { node.data = node.rightChild.data; node.rightChild =null; return; } if (node.leftChild != null && node.rightChild == null) { node.data = node.leftChild.data; node.leftChild = null; return; } //第三种情况 左右子树都有节点 BsNode temp = node.rightChild; while (temp.leftChild != null) { temp = temp.leftChild; } node.data = temp.data; Delete(temp); } } }