数据结构----二叉树(C#实现)

该帖只做笔记使用

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

构建树的类Tree.cs

初始化树

        public Node root;
        public int k;
        //初始化
        public Tree()
        {
            root=null;
            k = 0;
        }

定义节点Node

        public class Node
        {
            public int data;  
            public Node Lchild=null;
            public Node Rchild=null;
            public Node(int data)
            {
                this.data = data;
            }
        }

添加数据

        public void AddNode(int data)
        {
            //新建节点
            Node newN = new Node(data);
            newN.Lchild = null;
            newN.Rchild = null;

            //如树为空,则新节点插入为根节点
            if (root == null)
                root = newN;
            else
            {
                Node nowN = root;
                //遍历到临界的叶子节点(数据不允许重复)
                while ((newN.data>=nowN.data && nowN.Rchild!=null)|| (newN.data < nowN.data && nowN.Lchild != null))
                {
                    if (newN.data < nowN.data)
                        nowN = nowN.Lchild;
                    else if(newN.data > nowN.data)
                        nowN = nowN.Rchild;
                    else
                    {
                        Console.WriteLine("该数据已存在!");
                        return;
                    }
                }
                //插入到叶子节点的左/右节点
                if (newN.data < nowN.data)
                    nowN.Lchild = newN;
                else
                    nowN.Rchild = newN;
            }
            //数据长度+1
            k++;
        }

删除数据

        //删除数据
        public void DeleteN(int data)
        {
            Node parentN=null;
            Node nowN = root;
            bool find = false;    //找到数据所在节点的标志符
            while (nowN!=null && !find)
            {
                if (data == nowN.data)
                    find = true;
                else
                {
                    parentN = nowN;    //保存数据所在节点的父节点
                    if (data < nowN.data)
                        nowN = nowN.Lchild;
                    else
                        nowN = nowN.Rchild;
                }
            }

            if (!find)
                Console.WriteLine("没有找到该数据");
            else
            {
                if (nowN.Rchild == null)    //当右子树为空
                { 
                    if (nowN.Lchild == null)    //当左子树也为空,直接删除同时更新父节点的子节点
                    {
                        if (nowN.data < parentN.data)
                            parentN.Lchild = null;
                        else
                            parentN.Rchild = null;
                    }
                    else                 //当右子树也为空,将该节点的左子树更新为父节点的子树
                    {
                        if (nowN.data < parentN.data)
                        {
                            if (nowN.Lchild != null)
                                parentN.Lchild = nowN.Lchild;
                            else
                                parentN.Lchild = nowN.Rchild;
                        }
                        else
                        {
                            if (nowN.Lchild != null)
                                parentN.Rchild = nowN.Lchild;
                            else
                                parentN.Rchild = nowN.Rchild;
                        }
                    }

                }
                else                  //当右子树不为空,遍历右子树的左节点更新删除节点,同时将该节点的右子树更新该节点父节点的子树
                {
                    Node parentn = nowN;
                    Node n = nowN.Rchild;
                    while (n.Lchild != null)
                    {
                        parentn = n;
                        n = n.Lchild;
                    }
                    nowN.data = n.data;

                    if (parentn.Lchild == n)
                        parentn.Lchild = n.Rchild;
                    else
                        parentn.Rchild = n.Rchild;
                }
            }
            //数据长度-1
            k--;               
        }

中序遍历

        //中序遍历
        public int[] dataset()
        {
            Stack<Node> stack = new Stack<Node>();        //Stack<T> stack = new Stack<T>(); 
            Console.WriteLine("-----------入栈-------------");
            int[] set = new int[k];
            int i = 0;
            Node nowN = root;
            Boolean flag = true;
            while (nowN != null&& flag)   
            {
                stack.Push(nowN);
                Console.WriteLine("入栈数据:" + nowN.data);
                nowN = nowN.Lchild;                //将左子树压入栈

                while (nowN == null && stack.Count != 0)       //左子树为空时pop栈
                {
                    Node n = stack.Pop();
                    set[i] = n.data;
                    i++;
                    if (n.Rchild != null)     //当右子树不为空时,继续将左子树压入栈中
                    {
                        nowN = n.Rchild;
                    }else if (stack.Count == 0)          //当栈中数据全部pop且右子树为空,中序遍历结束
                        flag = false;     
                }
            }

            return set;
        }

主程序

添加数据

        private void insert_Click(object sender, EventArgs e)
        {
            int data =System.Convert.ToInt16(textBox1.Text);
            tree.AddNode(data);
            disp();
        }

删除数据

        private void delete_Click(object sender, EventArgs e)
        {
            int data = System.Convert.ToInt16(textBox1.Text);
            tree.DeleteN(data);
            disp();
        }

显示数据

        private void disp()
        {
            int k = tree.k;
            int[] set = tree.dataset();
            dispalyBox.Items.Clear();
            dispalyBox.Items.Add("中序遍历:");
            for (int i = 0; i < k; i++)
            {
                dispalyBox.Items.Add(set[i]);
            }
        }

可视化

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值