Tree-Complete two-fork tree

Source: Internet
Author: User

The advantages of the tree:
(1): Represents a domain hierarchy, and linked lists and arrays are the current one-dimensional, can not represent the hierarchical structure
(2): Two fork sorting tree to find more quickly than the linked list. But this condition is not always tenable, depending on the structure of the tree, if the tree is highly balanced (the height difference of the two subtree of any node)
is 0 or 1), the Lookup object is fast and if the objects in the tree are unevenly distributed, the
Complete binary tree (complete binary) or fully balanced tree: When the tree is balanced and all leaf nodes are on one or two levels, the tree is a fully balanced tree.
If you store 10,000 elements in a fully balanced tree, the height of the tree is lg[10001] = 14, which means that you can find an element with a maximum of 14 nodes to check. than the linked list needs
10000 This check has a huge gap.

The advantage of the algorithm is that there is no need to open additional space for storing nodes, and the time complexity is linear O (n).
The algorithm is carried out in two stages: first, any binary tree is converted into a single chain structure via a sequence of right rotations (called Vine, that is, each non-leaf node has only right child, no left child);
Then a complete binary tree is generated for the single chain structure through a few batches of left rotations in the second phase. In order to generate a complete binary tree,
In the second phase of the left rotation, only a subset of the nodes are rotated left (this is the first batch left rotation),
Then, in turn, do the left rotation for the remaining single chain nodes (the node for the left rotation of each batch is one node in the remaining Vine),
Until the left rotation of a batch is not greater than 1.

Two-fork sorting tree balanced into complete binary tree algorithm by DSW algorithm

To define a node data field:

Package cn.com.chenlly;
	public class Persion {private int id;
	private String name;

	private double height;
		Constructor public persion (int ID, String name, double height) {this.id = ID;
		THIS.name = name;
	This.height = height;
		//overriding 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 ToString method public String toString () {return "[ID: + ID +", Name: "+ name +", Height: "+ height +]";
	public int getId () {return id;
	The 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;
 }
}


To define a node:

Package cn.com.chenlly;

public class Node {
	private persion persion;  Data domain
	private Node leftchild;
	Private Node rightchild;
	
	Public Node (Persion p) {
		this.persion = p; 
	}
	
	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;
	}

}


One-time balanced binary tree (DSW algorithm)

Package cn.com.chenlly; 
	public class BinaryTree {private static int[] IDArray = new int[]{64,32,100,90,120,130,150,10};

	private Node root;
	Public Node Getroot () {return root; Public BinaryTree () {}/** * Inserts a new node into the two-fork sort tree * @param newNode/public void Insert (Node newNode) {if (root = null)
		{root = NewNode;
			else {Node current = root;
			Node parent;
				while (true) {parent = current;
					if (Newnode.getpersion (). GetId () <current.getpersion (). GetId ()) {//The current node moves down the present = Current.getleftchild ();
						if (current==null) {//to the leaf node parent.setleftchild (newNode);
					return;
					} else {current = Current.getrightchild ();
						if (current==null) {parent.setrightchild (NewNode);
					Return
			public void Creatbt () {//Constructed two fork sort tree for (int id:idarray) {persion p = new Persion (id,null,0.0);
			Node node = new node (p);
		Insert (node); }/** * Pre-sequence Traversal * @param node */Public void Preorder (node node) {if (node!=null) {System.out.print (Node.getpersion (). GetId () + ",");
			Preorder (Node.getleftchild ());
		Preorder (Node.getrightchild ());  }/** * Any binary tree is converted to a single chain structure through a sequence of right rotations (called Vine, that is, each non-leaf node is only a right child, no left child), * that is, the node is sorted by small to large sort/public
		Node Createvine (node root) {//Create a minimal node node Minnode = new node (new Persion ( -100,null,0.0));
		Node grandpar = Minnode;
		Node temp = root;
				while (Temp!=null) {if (Temp.getleftchild ()!=null) {Node child = Temp.getleftchild ();
				Temp.setleftchild (Child.getrightchild ());
				Child.setrightchild (temp);
			temp = child;
				else {grandpar.setrightchild (temp);
				Grandpar = temp;
			temp = Temp.getrightchild ();
		} root = Minnode.getrightchild ();
	return root;
		}/** * @param args */public static void main (string[] args) {BinaryTree bt = new BinaryTree ();
		Create a lesson binary sort tree (BST) BT.CREATBT ();
		Pre-sequence Traversal System.out.println ("pre-sequence Traversal result:"); Bt.preorder (Bt.getRoot ());
		Right rotations is converted to a single chain structure Node ROOT1 = Bt.createvine (Bt.getroot ());
		System.out.println ();
		System.out.println ("Right Turn Result:");
	Bt.preorder (ROOT1);
 }

}


Output results:

Pre-sequence Traversal results:
64,32,10,100,90,120,130,150,
Right Turn result:
10,32,64,90,100,120,130,150,

BT.CREATBT (); two-fork sort tree diagram created

Bt.createvine (Bt.getroot ()); Two-fork sort tree diagram after right turn


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.