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

Source: Internet
Author: User

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

Heavenkiller (original)

Each node of the N-tree has the same degree, which is N
Using System;
Using System. Collections;
Namespace DataStructure

{

/// <Summary>

/// Summary of the NaryTree. ----- N-tree

/// </Summary>

Public class NaryTree: Tree

{

// Member variables

Protected object key;

Protected uint degree;

Protected ArrayList treeList = new ArrayList ();

// Protected uint height = 0; // The default value is 0.

 

// Create an empty tree whose attribute of degree is _ degree

Public NaryTree (uint _ degree)

{

//

// TODO: add the constructor logic here

//

This. key = null;

This. degree = _ degree;

This. treeList = null;

}

// Construct an N-tree with leaf nodes

Public NaryTree (uint _ degree, object _ key)

{

This. key = _ key;

This. degree = _ degree;

This. treeList = new ArrayList ();

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

 

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

{

This. treeList. Add (this. GetEmptyInstance (_ degree ));

}

}

//-----------------------------------------------------------------

Protected virtual object GetEmptyInstance (uint _ degree)

{Return new NaryTree (_ degree );}

//-------------------------------------------------------------------

// Judge whether the tree is an empty tree

Public override bool IsEmpty ()

{Return this. key = null ;}

// Determine whether the node is a leaf node. If it is not an empty tree and each subtree is an empty tree, it is a leaf node.

Public override bool IsLeaf ()

{

If (IsEmpty ())

Return false;

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

{

If (! (This [I]. IsEmpty ()))

Return false;

}

Return true;

}

// ------------------------------------- Inherited Attributes ---------------------------------

Public override object Key

{

Get

{

Return this. key;

}

}

// Indexer

Public override Tree this [uint _ index]

{

Get

{

If (_ index> = this. degree)

Throw new Exception ("My: out of index! "); // If it is out of bounds, an exception is thrown.

If (this. IsEmpty ())

Return null; // if it is an empty tree, the indexer returns a null value.

Return (Tree) this. treeList [(int) _ index];

}

Set

{

This. treeList [(int) _ index] = value;

&

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.