The Java version of the two-fork tree in the sequence of clues __ two fork Tree

Source: Internet
Author: User
Package threadbinarytree;


public class Threadnode<t> {

Private threadnode<t> Leftnode;
Private threadnode<t> Rightnode;
private int lflag;
private int rflag;
Private T data;
Public Threadnode (threadnode<t> Leftnode, threadnode<t> Rightnode,
int lflag, int rflag) {
This.leftnode = Leftnode;
This.rightnode = Rightnode;
This.lflag = Lflag;
This.rflag = Rflag;
}
Public Threadnode () {
This.leftnode=null;
This.rightnode=null;
this.lflag=0;
this.rflag=0;
}
Public Threadnode (T data) {
This ();
This.data = data;
}
@Override
public int hashcode () {
return Data.hashcode ();
}
@Override
public boolean equals (Object obj) {
if (this = obj)
return true;
if (obj = null)
return false;
if (GetClass ()!= obj.getclass ())
return false;
Threadnode other = (threadnode) obj;
if (data = = NULL) {
if (other.data!= null)
return false;
else if (!data.equals (Other.data))
return false;
return true;
}
Public threadnode<t> Getleftnode () {
return leftnode;
}
public void Setleftnode (threadnode<t> leftnode) {
This.leftnode = Leftnode;
}
Public threadnode<t> Getrightnode () {
return rightnode;
}
public void Setrightnode (threadnode<t> rightnode) {
This.rightnode = Rightnode;
}
public int Getlflag () {
return lflag;
}
public void Setlflag (int lflag) {
This.lflag = Lflag;
}
public int Getrflag () {
return rflag;
}
public void Setrflag (int rflag) {
This.rflag = Rflag;
}
Public T GetData () {
return data;
}
public void SetData (T data) {
This.data = data;
}

}


Package threadbinarytree;


Import Java.util.Scanner;


public class Threadtree<t> {


protected threadnode<t> head=null;
public static final String endflag= "#";


Public Threadtree () {
}


Public Threadtree (threadnode<t> head) {
This.head = head;
}

Initializing the root node
public void Initthreadtree () {
String Item=null;
Scanner sc=new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Please input root node");
Item=sc.nextline ();
if (!item.equalsignorecase (Endflag)) {
Head=new Threadnode (item);
Init (head);
}
}
Initialize binary tree
private void Init (threadnode<t> head) {
String Item=null;
Scanner sc=new Scanner (system.in);
System.out.println ("Please enter" +head.getdata () + "left child node");
Item=sc.nextline ();
if (!item.equalsignorecase (Endflag)) {
Head.setleftnode (new Threadnode (item));
Init (Head.getleftnode ());
}
System.out.println ("Please enter" +head.getdata () + "right child node");
Item=sc.nextline ();
if (!item.equalsignorecase (Endflag)) {
Head.setrightnode (new Threadnode (item));
Init (Head.getrightnode ());
}
}

Create a Clue two fork tree
public void Createthreadtree () {

}

In-sequence traversal
public void Intraverse () {

}


First-order traversal
public void preorder () {
Preorder (head);
// }
private void Preorder (threadnode<t> head) {
if (head!=null) {
System.out.print (Head.getdata () + "");
Preorder (Head.getleftnode ());
Preorder (Head.getrightnode ());
// }
// }


}


Package threadbinarytree;


Middle order Clue two fork Tree class
public class Inthreadtree<t> extends threadtree<t>{


The precursor to the global
Private threadnode<t> Pre=null;

Public Inthreadtree () {
Super ();
}


@Override
public void Initthreadtree () {
Super.initthreadtree ();
}




Create a Clue two fork tree
@Override
public void Createthreadtree () {
Inthread (head);
}

To lead the two-fork tree
public void Inthread (threadnode<t> root) {
if (root!= null) {
Inthread (Root.getleftnode ());/Clue left child
if (null = = Root.getleftnode () && (0==root.getlflag ()) {//left child is empty
Root.setlflag (1)//set left child as clue
Root.setleftnode (pre);
}
if (Pre!=null&&null = = Pre.getrightnode ()) {//right child is empty
Pre.setrflag (1);
Pre.setrightnode (root);
}
Pre = root;
Inthread (Root.getrightnode ());/Clue the right child
}
}


/**
* Sequence traversal clue two fork Tree
* @param root
*/
@Override
public void Intraverse () {
Threadnode<t> Root=this.head;
if (root!= null) {
while (Root!=null && (Root.getlflag () ==0)) {//If the left child is not a clue
root = Root.getleftnode ();//
}


do{
System.out.print (Root.getdata () + ",");
if (Root.getrflag () ==1) {//If the right child is a clue
root = Root.getrightnode ();
}else{//has a right child.
root = Root.getrightnode ();
while (Root!=null && (Root.getlflag () ==0)) {
root = Root.getleftnode ();
}
}
}while (root!= null);
}
}

public static void Main (string[] args) {
Threadtree<string> tree=new inthreadtree<string> ();
Tree.initthreadtree ();
Tree.createthreadtree ();
Tree.intraverse ();
}

}


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.