C # Internal strength cultivation (algorithm)--Tree (ii) __ algorithm

Source: Internet
Author: User

Tree structure

Tree is an important non-linear data structure, intuitively, it is the data elements (called nodes in the tree) by the branching relationship of the structure, very much like the tree in nature.

Tree is an important non-linear data structure, intuitively, it is the data elements (called nodes in the tree) by the branching relationship of the structure, very much like the tree in nature. Tree structure is widely existed in the objective world, such as the Genealogy of human society and various social organizations can be represented by tree image. Tree in the computer field is also widely used, such as in the compilation of the source program as follows, the tree can be used to express the following syntax structure. As in the database system, the tree structure is also one of the important organizational forms of information. All questions that have a hierarchical relationship can be described by trees.

The relationship between trees is complex using chained storage

1, parent representation


2, child representation



3, child brother representation




Two fork Tree:

The general tree is a one-to-many relationship, the use of sequential structure storage is more difficult, but the binary tree is a special tree, each node has a maximum of two nodes, and the child nodes have the right and left points, and brothers, fathers, children can be very convenient by number, so we use the sequential storage structure using two-tree storage.

Binary tree Each node has up to two children, so for it to design a data field and two pointer fields, we call such a linked list of two fork list.




Two fork Tree characteristics:

1, at the first layer of the binary tree, there are 2i-1 nodes (i>=1).

2, the two-fork tree with a depth of K has at most 2k-1 nodes.

20+21+22+23+24+25+26+27+.....+2k-1+-1

=1+20+21+22+23+24+25+26+27+.....+2k-1-1

=21+21+22+23+24+25+26+27+.....+2k-1-1

=22+22+23+24+25+26+27+.....+2k-1-1

=23+23+24+25+26+27+.....+2k-1-1

=2k-1+2k-1-1

=2k-1

3, for a complete binary tree, assuming that it has n nodes, the node is numbered starting from 1, to any node I satisfies the following

A, its parents are node I/2 (except for the I=1)

B, the left child is the 2i right child is 2i+1

C, if 2i>n shows no left child 2i+1>n the right child


Two-fork Tree storage:

Sequential storage:

Private char[] _data = new[] {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J '};

    private void Usetree ()
    {
        bitree<char> bitree = new Bitree<char> (_data. Length);
        for (int i = 0; i < _data. Length; i++)
        {
            bitree.add (_data[i]);
        }

    public class bitree<t>
    {public
        t[] Data;
        public int Count;

        Public bitree (int capactiy)
        {
            Data = new t[capactiy];
            Count = 0;
        }

        public bool Add (T Item)
        {
            if (Count >= data.length) return false;

            Data[count] = Item;
            count++;
            return true;
        }
    

Chain Type storage:

Binary Tree Traversal:

1, pre-sequence traversal

First output the data of the current node, and then iterate through the output left node and right node.

        <summary>
        ///Pre-sequence traversal
        ///</summary> public
        void firsttraversal (int index)
        {
            if ( Index >= data.length) return;
            Traversal node number
            int munber = index + 1;

            if (Data[index). Equals ( -1)) return;

            Debug.Log (Data[index]);

            int leftnumber = munber*2;
            int rightnumber = munber*2 + 1;
            Firsttraversal (leftNumber-1);
            Firsttraversal (rightNumber-1);
        }

2, in-sequence traversal

First traversing the output of the left node, and then output the current node data, and then traverse the output right node

GDH B A E I C F

        <summary>
        ///in sequence traversal
        ///</summary> public
        void middletraversal (int index)
        {
            if ( Index >= data.length) return;
            Traversal node number
            int munber = index + 1;

            if (Data[index). Equals ( -1)) return;

            int leftnumber = Munber * 2;
            int rightnumber = Munber * 2 + 1;
            Middletraversal (leftNumber-1);

            Debug.Log (Data[index]);

            Middletraversal (rightNumber-1);
        }

3, after the subsequent traversal

First traverse the output left node, and then traverse the output right node, and finally output the current node data

G H D B I E F C A

        <summary>
        ///Subsequent traversal
        ///</summary> public
        void lasttraversal (int index)
        {
            if ( Index >= data.length) return;
            Traversal node number
            int munber = index + 1;

            if (Data[index). Equals ( -1)) return;

            int leftnumber = Munber * 2;
            int rightnumber = Munber * 2 + 1;
            Lasttraversal (leftNumber-1);
            Lasttraversal (rightNumber-1);

            Debug.Log (Data[index]);
        

4, Sequence traversal

From the first layer of the tree, from top to bottom traversal, in the same layer, from left to right node-by-point access to the output

        <summary>
        ///layer traversal
        ///</summary> public
        void Layertraversal ()
        {for
            (int i = 0 ; i < Count; i++)
            {
                Debug.Log (data[i]);
            }
        

Two-fork sort tree:

Binary sort tree, also known as binary lookup tree. It is either an empty tree or a two-prong tree with the following properties.

If its left subtree is not empty, the values of all nodes in the left subtree are small root structure;

If its right subtree is not empty, the value of all nodes in the right word is greater than the value of its root node;

Its left and right subtrees are also two-fork-sorted trees.

    <summary>///Tree Class///</summary> public class Bstree {private Bsnode _rootnode

        ;
        <summary>///Add///</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)//left to right {if (temp. Rightnode = = null)//Right node empty {temp.
                            Rightnode = NewNode; Temp.
                            ParentNode = temp;
                        Break temp = temp.
                    Rightnode;
           else//Left         {if (temp. Leftnode = = null) {temp.
                            Leftnode = NewNode; Temp.
                            ParentNode = temp;
                        Break temp = temp.
                    Leftnode;
        ///<summary>///Lookup///</summary>

            public bool Find (int item, Bsnode node) {if (node = null) return false; if (node.

            Data = = Item) return true; if (Item > node. Data) return to find (item, node.

            Rightnode); if (Item < node. Data) return to find (item, node.

            Leftnode);
        return false;
            ///<summary>///Delete///</summary> public bool Delete (int item) {

            Bsnode temp = _rootnode; while (true) {if (temp = null) return false; if (temp.
                    Data = = Item {Delete (temp);
                return true; temp = temp. Data > item? Temp. Leftnode:temp.
            Rightnode; } private void Delete (Bsnode node) {if node.
                ParentNode = = null) {_rootnode = null;
            Return ///Determine if node is a leaf nodes. Leftnode = = null && node. Rightnode = = null) {if node. Parentnode.leftnode = node) node.
                Parentnode.leftnode = null; Else node.
                Parentnode.rightnode = null;
            return;; ///Only Zuozi or only right subtree if node. Leftnode = = null && node. Rightnode!= null) {node. Data = node.
                Rightnode.data; Node.
                Rightnode = null;
   Return         } if (node. Leftnode!= null && node. Rightnode = = null) {node. Data = node.
                Leftnode.data; Node.
                Leftnode = null;
            Return There are bsnode temp = node in the left and right subtree.
            Rightnode; while (true) {if (temp. Leftnode!= null) {temp = temp.
                Leftnode;
                } else {break; } node. Data = temp.
            Data;
        Delete (temp); }///<summary>///node class///</summary> public class Bsnode {public int Dat

        a {get; set;}
        Public Bsnode Leftnode {get; set;}
        Public Bsnode Rightnode {get; set;}

        Public Bsnode parentnode {get; set;}
            Public Bsnode (int item) {Data = Item;
            Leftnode = null;Rightnode = null;
        ParentNode = null; }
    }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.