Build Binary Tree Code

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace kZ
{
// The Implementation of the node class of the Binary Tree binary linked list is as follows:
Public class node <t>
{
Private t data; // data domain
Private node <t> lchild; // left child
Private node <t> rchild; // right child

// Constructor
Public node (T Val, node <t> LP, node <t> RP)
{
Data = val;
Lchild = RP;
Rchild = RP;
}

// Constructor
Public node (node <t> LP, node <t> RP)
{
Data = default (t );
Lchild = LP;
Rchild = RP;
}

// Constructor
Public node (T Val)
{
Data = val;
Lchild = NULL;
Rchild = NULL;
}

// Constructor
Public node ()
{
Data = default (t );
Lchild = NULL;
Rchild = NULL;
}

// Data attributes
Public t data
{
Get
{
Return data;
}
Set
{
Data = value;
}
}

// Left child attributes
Public node <t> lchild
{
Get
{
Return lchild;
}
Set
{
Lchild = value;
}
}

// Right child attributes
Public node <t> rchild
{
Get
{
Return rchild;
}
Set
{
Rchild = value;
}
}
}

// Bitree of binary tree with no leading Node
Public class bitree <t>
{
Private node <t> head; // header reference

// Header reference attribute
Public node <t> head
{
Get
{
Return head;
}
Set
{
Head = value;
}
}

// Constructor
Public bitree ()
{
Head = NULL;
}

// Constructor
Public bitree (T Val)
{
Node <t> P = new node <t> (VAL );
Head = P;
}

// Constructor
Public bitree (T Val, node <t> LP, node <t> RP)
{
Node <t> P = new node <t> (Val, LP, RP );
Head = P;
}

// Determine whether it is an empty Binary Tree
Public bool isempty ()
{
If (Head = NULL)
{
Return true;
}
Else
{
Return false;
}
}

// Obtain the root node
Public node <t> root ()
{
Return head;
}

// Obtain the left child node of the node
Public node <t> getlchild (node <t> P)
{
Return P. lchild;
}

// Obtain the right child node of the node
Public node <t> getrchild (node <t> P)
{
Return P. rchild;
}

// Insert the left subtree of node P into a new vertex with the value of Val
// The original left subtree becomes the left subtree of the new node
Public void insertl (T Val, node <t> P)
{
Node <t> TMP = new node <t> (VAL );
TMP. lchild = P. lchild;
P. lchild = TMP;
}

// Insert the right subtree of node P into a new vertex with the value of Val
// The original right subtree becomes the right subtree of the new node
Public void insertr (T Val, node <t> P)
{
Node <t> TMP = new node <t> (VAL );
TMP. rchild = P. rchild;
P. rchild = TMP;
}

// If P is not empty, delete the left subtree of P.
Public node <t> deletel (node <t> P)
{
If (P = NULL) | (P. lchild = NULL ))
{
Return NULL;
}

Node <t> TMP = P. lchild;
P. lchild = NULL;

Return TMP;
}

// If P is not empty, delete the right subtree of P.
Public node <t> deleter (node <t> P)
{
If (P = NULL) | (P. rchild = NULL ))
{
Return NULL;
}

Node <t> TMP = P. rchild;
P. rchild = NULL;

Return TMP;
}

// Determine whether the node is a leaf node
Public bool isleaf (node <t> P)
{
If (P! = NULL) & (P. lchild = NULL) & (P. rchild = NULL ))
{
Return true;
}
Else
{
Return false;
}
}

}
}

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.