Binary Tree implementation Java Edition

Source: Internet
Author: User

After reading the binary tree data structure, I wrote it in java.

Node. java

Package test;

Public class Node <E> {
Node <E> Lnode;
Node <E> Rnode;
E e;
Public Node <E> getLnode (){
Return Lnode;
}
Public void setLnode (Node <E> lnode ){
Lnode = lnode;
}
Public Node <E> getRnode (){
Return Rnode;
}
Public void setRnode (Node <E> rnode ){
Rnode = rnode;
}
Public E getE (){
Return e;
}
Public void setE (E e ){
This. e = e;
}

}

PreOrderTree. java

Package test;

Import java. util. ArrayList;
Import java. util. List;

/**
* Add, delete, modify, and query for sequential Traversal
***/
Public class preOrderTree <E> {

Private Node <E> node;

/** Parent node **/
Private Node <E> prenode;
/** Current node **/
Private Node <E> curnode;


/** Sequential traversal after a node is deleted **/
Boolean delete (E e ){
// TODO Auto-generated method stub
Node <E> delnode = findByValue (node, e );

Return false;
}
/**
* Original root node
* Deleting a bgnode Node
*
* Return type: deleted Node
* Traverse and delete the subsequent traversal of the node after the specified node **/
Public Node <E> delNodes (Node <E> node, Node <E> bgnode)
{
While (node. Lnode! = Null)
{
Node = node. Lnode;
Prenode = node;
}
Return node;
}


/** Obtain the first node with the node value e **/
Public Node <E> findByValue (Node <E> node, E)
{
If (node. e = e)
{
Return node;
} Else if (node. Lnode! = Null &&! Node. Lnode. equals (e ))
{
Return findByValue (node. Lnode, e );
} Else if (node. Rnode! = Null &&! Node. Rnode. equals (e ))
{
Return findByValue (node. Rnode, e );
} Else if (node. Lnode! = Null & node. Lnode. equals (e )){
Return node. Lnode;
} Else if (node. Rnode! = Null & node. Rnode. equals (e ))
{
Return node. Rnode;
}
Return null;
}



/**
* Searches for all subnode sets after a Node and returns the result. If no, null is returned **/
Public List <E> findByNodeList (Node <E> node)
{
List <E> list = new ArrayList <E> ();
List. add (node. e );
Return prenode (list, node );
}

Void update (E e1, E e2 ){
// Todo auto-generated method stub

}




Public preordertree (){
Node = new node <E> ();
}
Public node <E> getnode (){
Return node;
}


/*************************************** ************************************
* Root node Traversal
**************************************** **********************************/
Public List <E> prenode (List <E> list, Node <E> node ){
If (node. Lnode! = Null ){
List. add (node. Lnode. e );
Prenode (list, node. Lnode );
}
If (node. Rnode! = Null)
{
List. add (node. Rnode. e );
Prenode (list, node. Rnode );
}
Return list;
}

/** Traverse all elements of a binary tree **/
Public List <E> listNode (){
List <E> list = new ArrayList <E> ();
List. add (node. e );
Return prenode (list, node );
}


Boolean isFind (E e ){
// TODO Auto-generated method stub
List <E> lists = listNode ();
For (int I = 0; I <lists. size (); I ++)
{
If (lists. get (I). equals (e ))
{
Return true;
}
}
Return false;
}


Void insert (E e ){
Insert (node, e );
}
/** Insert **/
Void insert (Node <E> node, E e)
{
If (node. e = null ){
Node. e = e;
} Else if (node. Lnode = null ){
Node. Lnode = new Node <E> ();
Insert (node. Lnode, e );
} Else if (node. Lnode = null ){
Node. Rnode = new Node <E> ();
Insert (node. Rnode, e );
} Else {
Insert (node. Lnode, e );
}
}

Public E getParent (E e)
{
Boolean flag = true;
Curnode = node;
// Prenode = node;
If (node. e = e)
{
Return null;
}
While (flag)
{
If (curnode. Lnode! = Null & curnode. Lnode. e! = E)
{
// Prenode = curnode;
Curnode = curnode. Lnode;
} Else if (curnode. Lnode! = Null & curnode. Lnode. e = e)
{
Return curnode. e;
} Else if (curnode. Rnode! = Null &&! Curnode. Rnode. equals (e ))
{
// Prenode = curnode;
Curnode = curnode. Rnode;
} Else if (curnode. Rnode! = Null & curnode. Rnode. equals (e ))
{
Return curnode. e;
} Else {
Return null;
}
}
Return null;
}
}

TreeTest. java

Package test;

Import java. util. List;

Public class TreeTest {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
PreOrderTree <Character> trees = new preOrderTree <Character> ();
String test = new String ("abcdefghijklmn ");
For (int I = 0; I <test. length (); I ++)
{
Trees. insert (test. charAt (I ));
}
List <Character> list = trees. listNode ();
For (char k: list)
{
System. out. println (k );
}
System. out. println (trees. getParent ('P '));
}
}

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.