Data structures and algorithms (C # implementation) series --- N-tree (2)

Source: Internet
Author: User

Data structures and algorithms (C # implementation) series --- N-tree (2)

Heavenkiller (original)

Public override uint Degree

{

Get

{

Return this. degree;

}

}

// Only used for empty Tree nodes

Public virtual void AttachKey (object _ obj)

{

If (! IsEmpty ())

Throw new Exception ("My: this node must be a empty tree node! ");

This. key = _ obj;

This. treeList = new ArrayList (); // generates a degree long array and initializes it as an empty tree.

This. treeList. Capacity = (int) this. degree;

 

For (int I = 0; I <this. degree; I ++)

{

TreeList. Add (new NaryTree (this. degree ));

}

/*

Foreach (object tmpObj in this. treeList)

{

TmpObj = new NaryTree (this. degree );

}

*/

}

// Only used for leaf nodes. The leaf node is changed to an empty node and the reference of the leaf node keyword is returned.

Public virtual object DetachKey ()

{

If (! IsLeaf ())

Throw new Exception ("My: this node must be a leaf node! ");

Object result = this. key; // store this leaf node temporary

This. key = null;

This. treeList = null;

 

Return result;

}

// Connect the subtree to the num node of the specified tree, provided that the node must be a null node with the same degree; otherwise, an exception is thrown.

Public virtual void AttachSubtree (uint num, NaryTree _ naryTree)

{

If (this. IsEmpty ())

Throw new Exception ("My: it cant be a empty tree! ");

If (! (This [num-1]. IsEmpty () | this. degree! = _ NaryTree. degree)

Throw new Exception ("My: this [I-1] must be empty and they shoshould have the same degree! ");

This [num-1] = _ naryTree;

}

// It is defined only as a non-empty tree. It is deleted from the given tree and connected to an empty tree. The degree is the same and the deleted subtree reference is returned.

Public virtual NaryTree DetachSubtree (uint num)

{

If (IsEmpty ())

Throw new Exception ("My: it cant be empty! ");

NaryTree tmpTree = this;

(NaryTree) this [num-1]). key = null;

(NaryTree) this [num-1]). treeList = null;

 

Return this;

}

}

}

 

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.