Using Java to implement two-fork tree __ data structure and algorithm

Source: Internet
Author: User

The following code implements the two-fork tree storage, lookup, traversal (preface, middle order, sequential)

Package cn.itcast;
	public class Node {public int value;
	Public Node left;
	
	public Node right;
				public void Store (int value) {if (value < This.value) {if (left = null) {left = new Node ();
			Left.value = value;
			} else{Left.store (value);
				} else{if (right = = null) {right = new Node ();
			Right.value = value;
			} else{Right.store (value);
		}} public Boolean find (int value) {System.out.println (' find: ' +this.value);
		if (value = = This.value) return true;
			else if (value < This.value) {if (left = null) return false;
		return Left.find (value);
			else {if (right = = null) return false;
		return Right.find (value);
		} public void Prelist () {System.out.print (this.value+ ",");
		if (left!= null) left.prelist ();
	if (right!= null) right.prelist ();
		public void Middlelist () {if (left!= null) left.middlelist ();
		System.out.print (This.value + ","); if (right!= null) right.middlelist ();
	public void Afterlist () {if (left!= null) left.afterlist ();
		if (right!=null) right.afterlist ();
	System.out.print (This.value + ",");
		public static void Main (string[] args) {int[] data = new INT[20];
			for (int i = 0;i<data.length;i++) {data[i]= (int) (Math.random () *100) +1;
		System.out.print (data[i]+ ",");
		} System.out.println ();
		Node root = new node ();
		Root.value = data[0];
		for (int i = 1;i<data.length;i++) {root.store (data[i]);
		} root.find (data[19]);
		System.out.println ();
		Root.prelist ();
		System.out.println ();
		Root.middlelist ();
		System.out.println ();
	Root.afterlist ();
 }
}


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.