JAVA implementation Two-fork tree (chained storage structure) _java

Source: Internet
Author: User
Tags int size

Classification of binary trees (by storage structure)

Classification of trees (by storage structure)

Sequential storage (with an array representation (static binary tree))
Chained-type storage

Some special two fork roots:

Complete binary tree, balanced binary tree (AVL), Clue two fork tree, Triple fork (with Father's pointer)
Two-fork search tree or Binary lookup tree (BST)

The binary tree is as shown in the following illustration:

Java implementation of binary tree (chained storage structure)

Class TreeNode {private int key = 0;
  Private String data = null;
  Private Boolean isvisted = false;
  Private TreeNode leftchild = null;
  
  Private TreeNode rightchild = null;
    Public TreeNode () {} public TreeNode (int key, String data) {This.key = key;
    This.data = data;
    This.leftchild = null;
  This.rightchild = null;
  public int Getkey () {return key;
  public void Setkey (int key) {This.key = key;
  Public String GetData () {return data;
  public void SetData (String data) {this.data = data;
  Public TreeNode Getleftchild () {return leftchild;
  public void Setleftchild (TreeNode leftchild) {this.leftchild = Leftchild;
  Public TreeNode Getrightchild () {return rightchild;
  public void Setrightchild (TreeNode rightchild) {this.rightchild = Rightchild;
  public Boolean isvisted () {return isvisted;
  } public void Setvisted (Boolean isvisted) {this.isvisted = isvisted;
}public class BinaryTree {private TreeNode root = null;
  Public BinaryTree () {root = new TreeNode (1, "RootNode (A)");
    public void Createbintree (TreeNode root) {//Manual creation (structure as shown) TreeNode Newnodeb = new TreeNode (2, "B");
    TreeNode Newnodec = new TreeNode (3, "C");
    TreeNode newnoded = new TreeNode (4, "D");
    TreeNode Newnodee = new TreeNode (5, "E");
    TreeNode newnodef = new TreeNode (6, "F");
    Root.setleftchild (Newnodeb);
    Root.setrightchild (NEWNODEC);
    Root.getleftchild (). Setleftchild (newnoded);
    Root.getleftchild (). Setrightchild (Newnodee);
  Root.getrightchild (). Setrightchild (Newnodef);
  public Boolean IsEmpty () {//The binary tree null no return root = = NULL;
  public int height () {//Tree height return (root);  public int height (TreeNode subtree) {if (subtree = null) return 0;//recursive end: Empty tree height 0 else {int i =
      Height (Subtree.getleftchild ());
      Int J = Height (Subtree.getrightchild ()); Return (I <j)?
    J + 1:i + 1;
  public int Size () {//Node return size (root);
    public int Size (TreeNode subtree) {if (subtree = null) return 0;
    else {return 1 + size (Subtree.getleftchild ()) + Size (Subtree.getrightchild ()); } public TreeNode Parent (TreeNode Element) {//Back to parent node return (root = null | | | root = = Element)? null:par
  ENT (root, Element);
    Public TreeNode Parent (TreeNode subtree, TreeNode Element) {if (subtree = null) return null; if (subtree.getleftchild () = = Element | | subtree.getrightchild () = Element)//Find, return parent node address returned SUBT
    Ree
    TreeNode p;  Look in the left subtree first, if not found in the Zoozi tree, to the right subtree to find if (P = Parent (Subtree.getleftchild (), Element)!= null)//recursive in left subtree search return
    P

  else//recursively searches the left subtree for return Parent (Subtree.getrightchild (), Element); Public TreeNode Leftchild (TreeNode Element) {//returns left subtree return (element!= null)? element.getlEftchild (): null; Public TreeNode Rightchild (TreeNode Element) {//back to right subtree return (element!= null)? Element.getrightchild (): N
  ull;
  The public TreeNode Getroot () {//Gets the root node return root; } public void Destroy (TreeNode subtree) {//Private function: Delete root subtree subtree if (subtree!= null) {Destroy (subtree.g Etleftchild ()); Delete the left subtree Destroy (Subtree.getrightchild ());       Delete right subtree//delete subtree;
    Delete root node subtree = null; } public void Traverse (TreeNode subtree) {System.out.println ("key:" + subtree.getkey () + "--name:" +
    Subtree.getdata ());
    Traverse (Subtree.getleftchild ());
  Traverse (Subtree.getrightchild ());
      } public void Preorder (TreeNode subtree) {//First root if (subtree!= null) {visted (subtree);
      Preorder (Subtree.getleftchild ());
    Preorder (Subtree.getrightchild ()); The root if (subtree!= null) {inorder (Subtree.getleftchi) in the public void Inorder (TreeNode subtree) {//LD ());
      visted (subtree);
    Inorder (Subtree.getrightchild ()); } public void Postorder (TreeNode subtree) {//posterior root if (subtree!= null) {Postorder (Subtree.getleftchil
      D ());
      Postorder (Subtree.getrightchild ());
    visted (subtree);
    } public void Levelorder (TreeNode subtree) {//Horizontal Edge} public boolean insert (TreeNode Element) {//Insert
  return true;
  public boolean find (TreeNode Element) {//lookup return true;
    } public void visted (TreeNode subtree) {subtree.setvisted (true);
  System.out.println ("key:" + subtree.getkey () + "--name:" + subtree.getdata ());
    public static void Main (string[] args) {BinaryTree bt = new BinaryTree ();
    Bt.createbintree (Bt.root); System.out.println ("The size of the" is "+ BT.)
    Size ()); System.out.println ("The height of the" is "+ BT.)
    Height ());
    System.out.println ("******* first (preface) [ABDECF] traverse *****************"); Bt.
    Preorder (bt.root); System.out. println ("******* in the root (in sequence) [DBEACF] traverse *****************"); Bt.
    Inorder (Bt.root);
    System.out.println ("******* after the root (subsequent) [DEBFCA] Traversal *****************"); Bt.
  Postorder (Bt.root); }

}

Result output:
The size of the is 6
The height of the tree is 3
First root (pre-order) [ABDECF] Traversal *****************
Key:1--name:rootnode (A)
Key:2--name:b
Key:4--name:d
Key:5--name:e
Key:3--name:c
Key:6--name:f
Zungen (in order) [DBEACF] Traversal *****************
Key:4--name:d
Key:2--name:b
Key:5--name:e
Key:1--name:rootnode (A)
Key:3--name:c
Key:6--name:f
posterior root (subsequent) [DEBFCA] Traversal *****************
Key:4--name:d
Key:5--name:e
Key:2--name:b
Key:6--name:f
Key:3--name:c
Key:1--name:rootnode (A)

I hope this article is helpful to students learning Java programming.

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.