使用C#操作二叉树的插入查询遍历和打印(代码)

13 篇文章 0 订阅

关于二叉树详细介绍请移步:https://blog.csdn.net/weixin_40165163/article/details/83826223

Node类:

 public class Node
    {
        public int Item { set; get; }   //节点数据
        public Node LeftChild { set; get; } //左子节点的引用
        public Node RightChild { set; get; } //右子节点的引用
        public bool IsDelete { set; get; }//表示节点是否被删除

        public Node(int data)
        {
            this.Item = data;
        }
    }

二叉树的插入、删除、打印、查找最大值、最小值、中序遍历、前序遍历、后序遍历

 public class BinaryTree
    {
        //表示根节点
        public Node _root;

        public void PrintTree(Node head, int height, string to, int len)
        {
            if (head == null)
            {
                return;
            }
            PrintTree(head.RightChild, height + 1, "right", len);
            string val = to + "\""+head.Item +"\""+ to;
            int lenM = val.Length;
            int lenL = (len - lenM) / 2;
            int lenR = len - lenM - lenL;
            val = GetSpace(lenL) + val + GetSpace(lenR);
            Console.WriteLine(GetSpace(height * len) + val);
            PrintTree(head.LeftChild, height + 1, "left", len);
        }

        public  string GetSpace(int num)
        {
            string space = " ";
            StringBuilder buf = new StringBuilder("");
            for (int i = 0; i < num; i++)
            {
                buf.Append(space);
            }
            return buf.ToString();
        }

        //查找节点
        public Node Find(int key)
        {
            Node current = _root;
            while (current != null)
            {
                if (current.Item > key)
                {//当前值比查找值大,搜索左子树
                    current = current.LeftChild;
                }
                else if (current.Item < key)
                {//当前值比查找值小,搜索右子树
                    current = current.RightChild;
                }
                else
                {
                    return current;
                }
            }
            return null;//遍历完整个树没找到,返回null
        }

        //插入节点
        public bool Insert(int data)
        {
            Node newNode = new Node(data);
            if (_root == null)
            {//当前树为空树,没有任何节点
                _root = newNode;
                return true;
            }
            else
            {
                Node current = _root;
                Node parentNode = null;
                while (current != null)
                {
                    parentNode = current;
                    if (current.Item > data)
                    {//当前值比插入值大,搜索左子节点
                        current = current.LeftChild;
                        if (current == null)
                        {//左子节点为空,直接将新值插入到该节点
                            parentNode.LeftChild = newNode;
                            return true;
                        }
                    }
                    else
                    {
                        current = current.RightChild;
                        if (current == null)
                        {//右子节点为空,直接将新值插入到该节点
                            parentNode.RightChild = newNode;
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        //中序遍历
        public void InfixOrder(Node current)
        {
            if (current != null)
            {
                InfixOrder(current.LeftChild);
                Console.WriteLine(current.Item + " ");
                InfixOrder(current.RightChild);
            }
        }

        //前序遍历
        public void PreOrder(Node current)
        {
            if (current != null)
            {
                Console.WriteLine(current.Item + " ");
                InfixOrder(current.LeftChild);
                InfixOrder(current.RightChild);
            }
        }

        //后序遍历
        public void PostOrder(Node current)
        {
            if (current != null)
            {
                InfixOrder(current.LeftChild);
                InfixOrder(current.RightChild);
                Console.WriteLine(current.Item + " ");
            }
        }
        //找到最大值
        public Node FindMax()
        {
            Node current = _root;
            Node maxNode = current;
            while (current != null)
            {
                maxNode = current;
                current = current.RightChild;
            }
            return maxNode;
        }
        //找到最小值
        public Node FindMin()
        {
            Node current = _root;
            Node minNode = current;
            while (current != null)
            {
                minNode = current;
                current = current.LeftChild;
            }
            return minNode;
        }

        public bool RemoveTree(int key)
        {
            Node current = _root;
            Node parent = _root;
            bool isLeftChild = false;
            //查找删除值,找不到直接返回false
            while (current.Item != key)
            {
                parent = current;
                if (current.Item > key)
                {
                    isLeftChild = true;
                    current = current.LeftChild;
                }
                else
                {
                    isLeftChild = false;
                    current = current.RightChild;
                }
                if (current == null)
                {
                    return false;
                }
            }
            //如果当前节点没有子节点
            if (current.LeftChild == null && current.RightChild == null)
            {
                if (current == _root)
                {
                    _root = null;
                }
                else if (isLeftChild)
                {
                    parent.LeftChild = null;
                }
                else
                {
                    parent.RightChild = null;
                }
                return true;

                //当前节点有一个子节点,右子节点
            }
            else if (current.LeftChild == null && current.RightChild != null)
            {
                if (current == _root)
                {
                    _root = current.RightChild;
                }
                else if (isLeftChild)
                {
                    parent.LeftChild = current.RightChild;
                }
                else
                {
                    parent.RightChild = current.RightChild;
                }
                return true;
                //当前节点有一个子节点,左子节点
            }
            else if (current.LeftChild != null && current.RightChild == null)
            {
                if (current == _root)
                {
                    _root = current.LeftChild;
                }
                else if (isLeftChild)
                {
                    parent.LeftChild = current.LeftChild;
                }
                else
                {
                    parent.RightChild = current.LeftChild;
                }
                return true;
            }
            else
            {
                //当前节点存在两个子节点
                Node successor = GetSuccessor(current);
                if (current == _root)
                {
                    successor = _root;
                }
                else if (isLeftChild)
                {
                    parent.LeftChild = successor;
                }
                else
                {
                    parent.RightChild = successor;
                }
                successor.LeftChild = current.LeftChild;
            }
            return false;

        }

        private Node GetSuccessor(Node delNode)
        {
            Node successorParent = delNode;
            Node successor = delNode;
            Node current = delNode.RightChild;
            while (current != null)
            {
                successorParent = successor;
                successor = current;
                current = current.LeftChild;
            }
            //后继节点不是删除节点的右子节点,将后继节点替换删除节点
            if (successor != delNode.RightChild)
            {
                successorParent.LeftChild = successor.RightChild;
                successor.RightChild = delNode.RightChild;
            }
            return successor;
        }
    }

测试代码:

class Program
    {
        static void Main(string[] args)
        {
            BinaryTree bt = new BinaryTree();
            bt.Insert(50);
            bt.Insert(20);
            bt.Insert(80);
            bt.Insert(10);
            bt.Insert(30);
            bt.Insert(60);
            bt.Insert(90);
            bt.Insert(25);
            bt.Insert(85);
            bt.Insert(100);
            bt.PrintTree(bt._root,0,"root",10);
            bt.RemoveTree(10);//删除没有子节点的节点
            bt.RemoveTree(30);//删除有一个子节点的节点
            bt.RemoveTree(80);//删除有两个子节点的节点
            Console.WriteLine("--------------------------------------------------------------");
            bt.PrintTree(bt._root, 0, "root", 10);
            Console.WriteLine("--------------------------------------------------------------");
            Console.WriteLine("最大值:"+bt.FindMax().Item);
            Console.WriteLine("--------------------------------------------------------------");
            Console.WriteLine("最小值:"+bt.FindMin().Item);
            Console.WriteLine("--------------------------------------------------------------");
            Console.ReadKey();
        }
    }

效果图为:

记录生活点点滴滴。

版权声明:本文为博主原创文章,未经博主允许不得转载。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值