. Net generic binary tree implementation and sequential Traversal

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace binarytree
{
Public class tree <t> where T: icomparable <t>
{
# Region Field

Private t data;
Private tree <t> left;
Private tree <t> right;

# Endregion

# Region attributes

/// <Summary>
/// Node data
/// </Summary>
Public t nodedata
{
Get {return this. Data ;}
Set {This. Data = value ;}
}
/// <Summary>
/// Left subtree
/// </Summary>
Public tree <t> lefttree
{
Get {return this. Left ;}
Set {This. Left = value ;}
}
/// <Summary>
/// Right subtree
/// </Summary>
Public tree <t> righttree
{
Get {return right ;}
Set {right = value ;}
}

# Endregion

# Region Method

/// <Summary>
/// Custom Constructor
/// </Summary>
/// <Param name = "nodevalue"> </param>
Public tree (T nodevalue)
{
This. Data = nodevalue;
This. Left = NULL;
This. Right = NULL;
}

/// <Summary>
/// Insert a new item into a binary tree
/// </Summary>
/// <Param name = "newitem"> new item </param>
Public void insert (T newitem)
{
T currentnodevalue = This. nodedata;
If (currentnodevalue. compareto (newitem)> 0)
{
// Insert a new item into the left subtree
If (this. lefttree = NULL)
{
// Use the new item to create a new left subtree
This. lefttree = new tree <t> (newitem );
}
Else
{
// Recursively call the insert method to insert a new item into the existing left subtree.
This. lefttree. insert (newitem );
}
}
Else
{
// Insert a new item into the right subtree
If (this. righttree = NULL)
{
// Use the new item to create a new right subtree
This. righttree = new tree <t> (newitem );
}
Else
{
// Recursively call the insert method to insert a new item into the existing right subtree
This. righttree. insert (newitem );
}
}
}

/// <Summary>
/// Traverse a binary tree in the middle order
/// </Summary>
Public void upload tree ()
{
If (this. lefttree! = NULL)
{
// Traverse the left subtree
This. lefttree. spanning tree ();
}
Console. writeline (this. nodedata. tostring ());
If (this. righttree! = NULL)
{
// Traverse the right subtree
This. righttree. Gradient tree ();
}
}

# Endregion
}
}

 

 

Test:

 

 

 

Using system;
Using system. Collections. Generic;
Using system. text;

Using binarytree;

Namespace binarytreetest
{
Class Program
{
Static void main (string [] ARGs)
{
Tree <int> tree1 = new tree <int> (10 );
Tree1.insert (5 );
Tree1.insert (11 );
Tree1.insert (5 );
Tree1.insert (-12 );
Tree1.insert (15 );
Tree1.insert (0 );
Tree1.insert (14 );
Tree1.insert (-8 );
Tree1.insert (10 );
Tree1.insert (8 );
Tree1.insert (8 );
Tree1.tree ();

Tree <string> tree2 = new tree <string> ("hello ");
Tree2.insert ("world ");
Tree2.insert ("how ");
Tree2.insert ("are ");
Tree2.insert ("you ");
Tree2.insert ("today ");
Tree2.insert ("I ");
Tree2.insert ("you ");
Tree2.insert ("are ");
Tree2.insert ("feeling ");
Tree2.insert ("well ");
Tree2.tree ();

String [] test = new string [3];
Test [0] = "hello ";
Test [1] = "world ";
Test [2] = "Haha ";
Tree <string> temp = buildtree <string> (test );
Temp. Tree ();
Tree <string> array = buildtree <string> ("hello", "world", "Haha ");
Array. Sort tree ();
}
Public static tree <t> buildtree <t> (Params T [] data) where T: icomparable <t>
{
Tree <t> sorttree = new tree <t> (data [0]);
For (INT I = 1; I <data. length; I ++)
{
Sorttree. insert (data [I]);
}
Return sorttree;
}
}
}

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.