The basic algorithm of tree-two-fork sort tree __ algorithm

Source: Internet
Author: User

(1), Binary trees (Binary tree): Two fork tree each node at most only two Shang trees (no more than 2 nodes).
Meet the following characteristics:
The first layer of the binary tree has at most 2 (k-1) secondary nodes, and at most two forked trees with a depth of K (2 K-th)-1 nodes;
For any binary tree T, if its terminal node number is N0, the number of degrees 2 is N2. N0 = n2+1

(2), two fork (full tree): A tree with a depth of K, and have (2 K-th side)-1 nodes become full two fork trees

(3), Complete binary tree (Complete): The depth is k, there are n nodes of the two-tree, when and only if each of its nodes with the depth of K-two fork tree numbered from 1 to n nodes one by one pairs of time called complete binary tree.

(4), binary sort trees (binary sort tree) also known as binary lookup trees (binary search tree).
Meet the following characteristics:
(1) The Joz tree is not empty, the value of all nodes in the left subtree is less than the value of its root node;
(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;

(3) The left and right subtrees were also two-fork-sorted trees;


Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;

/**
* @Class BinaryTree
* @ Binary sort tree's basic algorithm, insert, find, delete, view and traverse (pre-sequence traversal, sequence traversal, sequential traversal) the whole tree
* @Company OpenData
* @author chenlly
* @Date 2009-03-10
* @version 1.0
*
*/
Class Persion {
private int id;
private String name;
private double height;

Constructors
Public persion (int ID, String name, double height) {
This.id = ID;
THIS.name = name;
This.height = height;
}

Overriding the Equals method
public boolean equals (Object obj) {
if (obj = = null) {
return false;
}

if (obj = = this) {
return true;
}

if (!) ( obj instanceof Persion)) {
return false;
}

Persion persion = (persion) obj;
return persion.id = = This.id && Persion.name = = THIS.name
&& Persion.height = = This.height;
}

Overriding the ToString Method
Public String toString () {
return "[ID: + ID +", Name: "+ name +", Height: "+ height +"] ";
}

public int getId () {
return ID;
}

public void setId (int id) {
This.id = ID;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public double getheight () {
return height;
}

public void SetHeight (double height) {
This.height = height;
}

}

Node class
Class Node {
Private Persion persion;
Private Node leftchild;
Private Node Rightchild;

Construction method
Public Node (Persion persion) {
This.persion = persion;
}

Public Persion getpersion () {
return persion;
}

public void Setpersion (Persion persion) {
This.persion = persion;
}

Public Node Getleftchild () {
return leftchild;
}

public void Setleftchild (Node leftchild) {
This.leftchild = Leftchild;
}

Public Node Getrightchild () {
return rightchild;
}

public void Setrightchild (Node rightchild) {
This.rightchild = Rightchild;
}
}

Two fork Tree
Class Tree {
private Node root;

Construction method
Public tree () {
Do something
}

Insert new node
public void Insert (Node newNode) {
Because it's an empty tree.
if (root = = null) {
root = NewNode;
} else {
Node current = root;
Node parent;
while (true) {
parent = current;
This node should be inserted to the left of the parent node because the new node keyword is less than the parent node
if (Newnode.getpersion (). GetId () < Current.getpersion (). GetId ()) {
Current = Current.getleftchild ();
if (current = = null) {
Parent.setleftchild (NewNode);
Return Insert End Method Later
}
} else {
Current = Current.getrightchild ();
if (current = = null) {
Parent.setrightchild (NewNode);
Return
}
}//End If
}//End While
}//End Else
}//End Insert

Find nodes
Public Node findbykey (int key) {
Node current = root;
Handling of empty trees
if (current = = null) {
return null;
}
Continuous traversal and comparison
while (Current.getpersion (). GetId ()!= key) {
if (Key < Current.getpersion (). GetId ()) {
Current = Current.getleftchild ();
} else {
Current = Current.getrightchild ();
}
Because the leaves have not yet found the corresponding keyword
if (current = = null) {
return null;
}
}//End While
return to current;
}

Deletes a specified node
After finding this node, consider three cases, (1) the node to be deleted is a leaf node, (2) The node has a child node, (3) The node has two child nodes
public boolean Delete (int key) {
First find the node that needs to be deleted
Node current = root;
Node parent = root;
Boolean isleftchild = true;
Handling of empty trees
if (current = = null) {
return false;
}
Continuous traversal and comparison
while (Current.getpersion (). GetId ()!= key) {
parent = current;
if (Key < Current.getpersion (). GetId ()) {
Isleftchild = true;
Current = Current.getleftchild ();
} else {
Isleftchild = false;
Current = Current.getrightchild ();
}
Because the leaves have not yet found the corresponding keyword
if (current = = null) {
return false;
}
}//End While

(1) Because the node that needs to be deleted is a leaf node
if (current.getleftchild () = = null && current.getrightchild () = null) {
if (current = = root) {
root = null;
} else {
if (isleftchild) {
Parent.setleftchild (NULL);
} else {
Parent.setrightchild (NULL);
}
}
(2) The deleted node has a child node and has a right child node
else if (current.getleftchild () = null) {
if (current = = root) {
root = Current.getrightchild ();
} else {
if (isleftchild) {
Parent.setleftchild (Current.getrightchild ());
} else {
Parent.setrightchild (Current.getrightchild ());
}
}
(3) The deleted node has a child node and has a left child node
else if (current.getrightchild () = null) {
if (current = = root) {
root = Current.getleftchild ();
} else {
if (isleftchild) {
Parent.setleftchild (Current.getleftchild ());
} else {
Parent.setrightchild (Current.getleftchild ());
}
}
(4) The deletion of the node has two subnodes, this situation can not be replaced by Shirai, but to find the successor node in sequence. Because the sequence traversal is orderly.
The smallest value for the right left subtree of the node to be deleted is the successor of the node to be deleted.
} else {
Node successor = getsuccessor (current);
if (successor = = root) {
root = successor;
} else {
(Step three)
if (isleftchild) {
Parent.setleftchild (successor);
} else {
Parent.setrightchild (successor);
}
}
(Fourth Step)
Successor.setleftchild (Current.getleftchild ());
}
return true;
}

Find successor node
Private node Getsuccessor (node Delnode) {
Node parentsuccess = Delnode;
Node successor = Delnode;
Node current = Delnode.getrightchild ();
while (current!= null) {
Retains the parent node of the current node
Parentsuccess = successor;
successor = current;
Current = Current.getleftchild ();
}

if (successor!= Delnode.getrightchild ()) {
(First step)
Parentsuccess.setleftchild (Successor.getrightchild ());
(Step Two)
Successor.setrightchild (Delnode.getrightchild ());
}
return successor;
}

Find the largest value
Public Node Findmaxvalue () {
Node current = root;
Node max = null;//Storage Max node
while (current!= null) {
max = current;
Current = Current.getrightchild ();
}
return Max;
}

Find Minimum value
Public Node Findminvalue () {
Node current = root;
Node min = null;//Storage Minimum node
while (current!= null) {
min = current;
Current = Current.getleftchild ();
}
return min;
}

Traversing nodes
Pre-sequence traversal
public void Preorder (Node localroot) {
if (localroot!= null) {
System.out.println (Localroot.getpersion (). toString ());
Inorder (Localroot.getleftchild ());
Inorder (Localroot.getrightchild ());
}
}

In-sequence traversal
public void Inorder (Node localroot) {
if (localroot!= null) {
Inorder (Localroot.getleftchild ());
System.out.println (Localroot.getpersion (). toString ());
Inorder (Localroot.getrightchild ());
}
}

Pre-sequence traversal
public void Lastorder (Node localroot) {
if (localroot!= null) {
Inorder (Localroot.getleftchild ());
Inorder (Localroot.getrightchild ());
System.out.println (Localroot.getpersion (). toString ());
}
}

Public Node Getroot () {
return root;
}

public void Setroot (Node root) {
This.root = root;
}

}

Public class BinaryTree {
 //reads data from the keyboard
 public static String readstr () {
  inputstreamr Eader ir = new InputStreamReader (system.in);
  bufferedreader br = new BufferedReader (IR);
  string str = "";
  try {
   str = Br.readline ();
  } catch (Exception ex) {
 &nbs P; ex.printstacktrace ();
  }
  return str;
 }

Public Persion Createper () {
System.out.print ("ID:");
int id = integer.parseint (binarytree.readstr ());
System.out.print ("");
System.out.print ("Name:");
String name = Binarytree.readstr ();
System.out.print ("");
System.out.print ("Height:");
Double height = double.parsedouble (binarytree.readstr ());
Persion persion = new Persion (ID, name, height);
return persion;
}

public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN ("Please select Action sequence");
System.out.println ("1 insert Node");
System.out.println ("2 lookup Node");
System.out.println ("3 Find the largest node");
System.out.println ("4 Find the smallest node");
System.out.println ("5 delete node");
System.out.println ("6 pre-sequence traversal");
System.out.println ("7 sequence Traversal");
SYSTEM.OUT.PRINTLN ("8 subsequent traversal");
System.out.println ("9 exit system");
BinaryTree BT = new BinaryTree ();
Tree tree = new Tree ();
while (true) {
System.out.println ("Please enter the sequence of operations");
int op = integer.parseint (Binarytree.readstr ());
Switch (OP) {
Case 1:
System.out.println ("Please Enter personnel information:");
Persion persion = bt. Createper ();
Node NewNode = new node (persion);
Tree.insert (NewNode);
Break
Case 2:
System.out.println ("Enter the person ID you want to find:");
int id = integer.parseint (binarytree.readstr ());
Node node = tree.findbykey (ID);
if (node!= null) {
SYSTEM.OUT.PRINTLN ("The information of the person found by ID is:" +node.getpersion (). toString ());
} else {
SYSTEM.OUT.PRINTLN ("No node to look for!");
}
Break
Case 3:
Node Maxnode = Tree.findmaxvalue ();
System.out.println ("The largest node information is:"
+ maxnode.getpersion (). toString ());
Break
Case 4:
Node Minnode = Tree.findminvalue ();
System.out.println ("The Smallest node information is:"
+ minnode.getpersion (). toString ());
Break
Case 5:
System.out.println ("Please enter the person ID to be deleted:");
int key = Integer.parseint (Binarytree.readstr ());
if (Tree.delete (key)) {
System.out.println ("Delete is success");
} else {
System.out.println ("Delete is fail");
}
Break
Case 6:
System.out.println ("Pre-sequence traversal sequence is:");
Tree.preorder (Tree.getroot ());
Break
Case 7:
SYSTEM.OUT.PRINTLN ("Sequential traversal sequence is:");
Tree.inorder (Tree.getroot ());
Break
Case 8:
SYSTEM.OUT.PRINTLN ("Sequential traversal sequence is:");
Tree.lastorder (Tree.getroot ());
Break
Case 9:
Default
System.exit (0);
}//end switch
}//End While
}//End Main
}

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.