C# 二叉树的综合操作(二):删除节点

来源:互联网 发布:sql语句创建视图 编辑:程序博客网 时间:2024/05/11 04:18

    删除二叉树的节点相对复杂一些。 要分为三种情况:(1)要删除的节点为叶子节点,即此节点没有子节点;(2)要删除的节点只有一个子节点;(3)要删除的节点有两个子节点。其中每种情况又要细分为同种情况:要删除的节点是其父节点的左子节点,以及,要删除节点是其父节点的右子节点。

    删除操作需要用到上一章说的到的“查找方法”。将上一章的代码加入 Delete(int key)方法,完事代码好

    public class Node                             // 定义二叉树节点类 Node    {        public int Data;        public Node Left;        public Node Right;        public void Display()        {            Console.WriteLine(Data);        }        public Node(int x)        {            Data = x;        }    }    public class BinaryTree              // 定义二叉树    {        public Node Current;             //         Node Parent;                     // 定义一个 Parent,为添加删除节点方法做准备        public Node Root;                // 定义根结点        public BinaryTree()             // 构造函数,初始化二叉树        {            Root = null;        }        public void InOrder(Node theRoot)            // 通过递归,中序遍历        {            if (theRoot != null)            {                InOrder(theRoot.Left);                Console.Write(theRoot.Data + "\t");                InOrder(theRoot.Right);            }        }        public void PreOrder(Node theRoot)          // 通过递归,先序遍历        {            if (theRoot != null)            {                Console.Write(theRoot.Data + "\t");                PreOrder(theRoot.Left);                PreOrder(theRoot.Right);            }        }        public void PostOrder(Node theRoot)        // 通过递归,后序遍历        {            if (theRoot != null)            {                PostOrder(theRoot.Left);                PostOrder(theRoot.Right);                Console.Write(theRoot.Data + "\t");            }        }        public void Insert(int x)                  // 插入节点方法        {            Node newNode = new Node(x);            Current = Root;            if (Root == null)            {                Root = newNode;            }            else            {                while (true)                {                    Parent = Current;                    if (x < Current.Data)                    {                        Current = Current.Left;                        if (Current == null)                        {                            Parent.Left = newNode;                            break;                        }                    }                    else                    {                        Current = Current.Right;                        if (Current == null)                        {                            Parent.Right = newNode;                            break;                        }                    }                }            }        }        public int Min()              // 查找最小值        {            Current = Root;            while (Current.Left != null)                Current = Current.Left;            return Current.Data;        }        public int Max()              // 查找最大值        {            Current = Root;            while (Current.Right != null)                Current = Current.Right;            return Current.Data;        }        public Node Find(int key)     // 查找某一个确定的值        {            Current = Root;            while (Current != null)            {                if (key == Current.Data)                    break;               // 找到了就结束 while 循环                if (key < Current.Data)                {                    Parent = Current;                    Current = Current.Left;                }                else                {                    Parent = Current;                    Current = Current.Right;                }            }            if (Current == null)                return null;            else                return Current;        }        public void Delete(int key)        {            // 1. leaf node            Node Target = Find(key);            if (Target != null && Target != Root)            {                if (Current.Left == null && Current.Right == null)                    if (Parent.Left == Current)                        Parent.Left = null;                    else                        Parent.Right = null;                //2. have one node                else if (Current.Left == null && Current.Right != null)                {                    if (Parent.Right == Current)                        Parent.Right = Current.Right;                    else                        Parent.Left = Current.Right;                }                else if (Current.Left != null && Current.Right == null)                {                    if (Parent.Right == Current)                        Parent.Right = Current.Left;                    else                        Parent.Left = Current.Left;                }                //3. have two nodes                else                {                    Node soccessor = Current.Right;   //  后继节点                    Node soccessorParent = Current; // 后继节点的父结点                    while (soccessor.Left != null)                    {                        soccessorParent = soccessor;                        soccessor = soccessor.Left;                    }                    // 3.1 要删除点是其父节点的左节点                    if (Current == Parent.Left)                    {                        Parent.Left = soccessor;                        soccessor.Left = Current.Left;                        if (Current == soccessorParent)    // 判断要删除点是否为后继点的父节点                        {                            Current.Right = null;                        }                        else                        {                            soccessor.Right = Current.Right;                            soccessorParent.Left = null;                        }                    }                    // 3.2 要删除点是其父节点的右节点                    else if (Current == Parent.Right)                    {                        Parent.Right = soccessor;                        soccessor.Left = Current.Left;                        if (Current == soccessorParent)                        {                            Current.Right = null;                        }                        else                        {                            soccessor.Right = Current.Right;                            soccessorParent.Left = null;                        }                    }                }            }            // 如果要删除点是根节点,直接删除整棵树            else if (Target == Root)                Root = null;        }    }    class Program    {        static void Main(string[] args)        {            BinaryTree bst = new BinaryTree();            bst.Insert(25);            bst.Insert(50);            bst.Insert(15);            bst.Insert(33);            bst.Insert(4);            bst.Insert(100);            bst.Insert(20);            bst.Insert(38);            bst.Insert(1);            bst.Insert(10);            bst.Insert(18);            bst.Insert(30);            bst.Insert(32);            Console.WriteLine("The min is: {0}", bst.Min());            Console.WriteLine("The max is: {0}", bst.Max());            Console.Write("Find \"100\": ");            Console.Write(bst.Find(100).Data + "\n");            Console.WriteLine("\n" + "PreOrder: ");            bst.PreOrder(bst.Root);            Console.WriteLine("\n" + "PostOrder:");            bst.PostOrder(bst.Root);            Console.WriteLine("\n" + "InOrder: ");            bst.InOrder(bst.Root);            bst.Delete(33);            Console.WriteLine("\n" + "PostOrder:");            bst.InOrder(bst.Root);            Console.Read();
运行结果:

The min is: 1
The max is: 100
Find "100": 100


PreOrder:
25      15      4       1       10      20      18      50      33      30      32      38      100
PostOrder:
1       10      4       18      20      15      32      30      38      33      100     50      25
InOrder:
1       4       10      15      18      20      25      30      32      33      38      50      100
After delete, InOrder:
1       4       10      15      18      20      25      30      32      38      50      100

2 0
原创粉丝点击