用C#寫二叉數

來源:互聯網
上載者:User
using System;
using System.Collections.Generic;
using System.Text;

namespace BinaryTree
{
    class Program
    {
        static void Main(string[] args)
        {
            BTree bt = new BTree();
            Node parent= null, current = null;
            int i = 1;
            while(i>0)
            {
                Console.WriteLine("Input an integer");
                i = int.Parse(Console.ReadLine());
                bt.find(i, ref parent, ref current);
                if (current != null) Console.WriteLine("{0}Already exist!", i);
                else
                {
                    bt.addNew(i, parent, current);
                    Console.WriteLine("{0} inserted!", i);
                }
            };
            return;
        }
    }
    class Node
    {
        public int ivalue;
        public Node lchild;
        public Node rchild;

        public Node(int i, Node l, Node r)
        {
            ivalue = i;
            lchild = l;
            rchild = r;
        }
    }
    class BTree
    {
        public Node ROOT;
        public BTree()
        {
            ROOT = null;
        }

        public bool contain(int element)
        {
            Node parent = null, currentNode = null;
            find(element, ref parent, ref currentNode);
            if(currentNode == null) return false;
            return true;
        }
       
        public bool add(int element)
        {
            Node tmp, parent = null, currentNode = null;
            find(element, ref parent, ref currentNode);
            if(currentNode != null)
            {
                return false;   // already there
            }
            else
            {
                tmp = new Node(element, null, null);
                if(parent == null) // empty tree
                    ROOT = tmp;
                else
                    if(element<parent.ivalue)
                        parent.lchild =tmp;
                    else   
                        parent.rchild = tmp;
            }
            return true;
        }

        // must make sure that the value is not in the tree
        public void addNew(int element, Node parent, Node currentNode)
        {
            //if(currentNode != null) return false;
            Node tmp;
            // currentNode should be null
            tmp = new Node(element, null, null);
            if (parent == null) // empty tree
                ROOT = tmp;
            else
                if (element < parent.ivalue)
                    parent.lchild = tmp;
                else
                    parent.rchild = tmp;
        }
                   
        public void find(int element, ref Node parent, ref Node currentNode)
        {
            currentNode = ROOT;
            parent = null;
            while ((currentNode != null) && (currentNode.ivalue != element))
            {
                parent = currentNode;
                if (element < currentNode.ivalue) // left
                    currentNode = currentNode.lchild;
                else    // right
                    currentNode = currentNode.rchild;
            }
        }                
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.