Java implementation two Fork sort tree

Source: Internet
Author: User

What is a two-fork sort tree: A two-fork sort tree or an empty tree, or a two-fork tree with the following properties:

(1) If its left subtree is not empty, then the value of all nodes on the left subtree is less than the value of his parent node;

(2) if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of his parent node;

(3) Its left and right sub-tree is also a two-fork sorting tree;

Java instance:

Package Com.test.linked;public class Heapsort {public class Node{private int data;private node leftchildren;private node r Ightchildren;public Node (int data) {This.data=data;} Public String toString () {return ' "+data;}} Public node root;/** * INSERT Nodes * @param in */public void Insert (node in) {if (root==null) {///When the node is root=in as the root when empty tree;} Else{node Current=root; Node parent;//as Parentwhile (current!=null) {//when looping to empty end parent=current;if (in.data<current.data) {// A little farther to the left than the current node. Current=current.leftchildren;if (Current==null) {//When the left child node is empty means that the location to be inserted is found parent.leftchildren=in;}} else{//otherwise go to the right current=current.rightchildren;if (current==null) {parent.rightchildren=in;}}}} /** * Find element * @param key */public void find (int key) {Node current=root;int deep=1;if (current!=null) {///determine if the root node is empty while (Curre Nt.data!=key) {if (key<current.data) {//the same is smaller than the node to the left or go to the right current=current.leftchildren;} Else{current=current.rightchildren;} deep++;//Find the node in the first layer, if (current==null) {System.out.println ("Node can ' t be found"); return;}} System.out.println ("This NoDe was: "+current+" at the Deep: "+deep);} Else{system.out.println ("This tree is null");}} /** * Traversing a binary tree (here is the first order traversal) uses recursion to traverse itself until all nodes are traversed * @param node */public void Display (node node) {if (node!=null) { System.out.println (Node.data);d isplay (Node.leftchildren);d isplay (Node.rightchildren);}} public static void Main (string[] args) {heapsort sort=new heapsort (); Node N1=sort.new node (60); Node N2=sort.new node (50); Node n3=sort.new node (80); Node N4=sort.new node (20); Node N5=sort.new node (55); Node N6=sort.new node (62); Node N7=sort.new node, Sort.insert (N1); Sort.insert (n2); Sort.insert (n3); Sort.insert (N4); Sort.insert (N5); Sort.insert (N6); Sort.insert (N7); Sort.find (Sort.display);}}


Java implementation two Fork sort tree

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.