"Data Structure"--sorting algorithm--1.3, binary tree sort

Source: Internet
Author: User

"Data Structure"--sorting algorithm--1.2, binary tree sort
First, on the wiki diagram: two fork Tree sort wikifigure one or two sorting a fork treeIi. Description

two fork Find Tree ( English:binarysearch tree), also known as the binary searching trees, ordered binary trees ( English:ordered binarytree), Sort binary trees ( English:sorted binarytree) refers to an empty tree or a two-fork tree with the following properties:

    1. If the left subtree of any node is not empty, the value of all nodes on the left subtree is less than or equal to the value of its root node;
    2. If the right subtree of any node 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 of any node are also two-fork lookup trees, respectively.
    4. No nodes with key values equal ( English:No Duplicate Nodes). Binary lookup trees have the advantage over other data structures in finding and inserting less time complexity. is O (log n). Binary lookup tree is a basic data structure used to construct more abstract data structures, such as collections, multiset, associative arrays, and so on.

The process of finding X in binary find tree B is:

    1. If B is an empty tree, the search fails, otherwise:
    2. If x equals the value of the data field of the root node of B, the lookup succeeds;
    3. If x is less than the value of the data field of the root node of B, the left subtree is searched; otherwise:
    4. Find the right subtree.

Third, Java program
public class Ordertree {public static void main (string[] args) {/** * ***4*** * *2***6* * 1*3*5*7 * */int[] A = {4        , 2,1,3,6,5,7,8,9};        BTree root = new BTree ();        Createorder (A, root);        System.out.println ("\npre Order:");        Preorder (root);        System.out.println ("\nmid Order:");        Midorder (root);        System.out.println ("\nlast Order:");        Lastorder (root);        List<point> points = new arraylist<point> ();        Convert the node of the tree to the coordinate int floors = Totalfloor (root);        System.out.println ("\nfloors:" +floors);                 Printtree (root, points, 0, -1,floors);        Sort coordinates according to Row,col easy to print collections.sort (points);        Print at 5 characters per point (coordinate system conversion required), justifies when data==-1 * int row = 0;        StringBuilder sb = new StringBuilder ();        int totallength = 1* (Squat (2, floors)-1); ForPoint p:points) {if (row = = P.row) {sb.append (Printtimes ("*", 1*p.col-sb.length ()));            Sb.append (PrintWidth ("+p.data, 1)"); }else{if (Sb.length () < Totallength) {Sb.append (Printtimes ("*", Totallength-sb.length (                )));                } System.out.println (Sb.tostring ());                SB = new StringBuilder ();                row++;                Sb.append (Printtimes ("*", 1*p.col-sb.length ()));            Sb.append (PrintWidth ("+p.data, 1)");        }} if (Sb.length () < Totallength) {Sb.append (Printtimes ("*", Totallength-sb.length ()));                      } System.out.println (Sb.tostring ());        } static int Totalfloor (BTree root) {if (root = null) return 0;    return Internalloop (root, 0);            } static int Internalloop (BTree root,int floor) {if (root! = null) {floor++; int left = Internalloop (Root.left,Floor);            int right = Internalloop (root.right, floor);        Return Math.max (left, right);    } else return floor;        } static string Printtimes (string s,int time) {StringBuilder sb = new StringBuilder ();        for (int i=0;i<time;i++) {sb.append (s);    } return sb.tostring ();        } static string PrintWidth (string s, int width) {if (s = = null) {return printtimes ("", width); }else{if (s.length () < width) {return new StringBuilder (s). Append (Printtimes ("*", width-s.            Length ()). ToString ();            }else{return s.substring (0,width);            }}} Static class point implements comparable<point>{public point (int row,int col,int data) {            This.row = row;            This.col = col;        This.data = data;        } int row = 0;        int col = 0;        int data =-1; @Override Public INT CompareTo (Point O) {if (This.row = = O.row) {if (col = = O.col) return 0;                else if (Col < o.col) {return-1;                }else{return 1;            }}else if (Row < O.row) {return-1;            }else{return 1;        }}} static void Printtree (BTree root,list<point> points,int row,int col,int floors) {            if (root = null) {int inter = squat (2, floors-1)-1;            if (col = =-1) {col = Inter;             }/** * ***0*** * *0***0* * 0*0*0*0 * */Points.Add (new Point (Row, col, Root.data))            ;            Printtree (Root.left,points,row+1, col-((inter-1)/2) -1,floors-1); Printtree (Root.right,points,row+1,col +(inter-1)/2+1, floors-1);        }} static int squat (int s,int b) {if (b = = 0) return 1;        int result = 1;        for (int i=0;i<b; i++) {result *=s;    } return result;        } static void preorder (BTree root) {if (root = = null) {return;        } System.out.print ("-" +root.data);        Preorder (Root.left);    Preorder (root.right);        } static void Midorder (BTree root) {if (root = null) return;        Midorder (Root.left);        System.out.print ("-" +root.data);    Midorder (Root.right);        } static void Lastorder (BTree root) {if (root = null) return;        Lastorder (Root.left);        Lastorder (Root.right);    System.out.print ("-" +root.data); }//Left small, right large, equal when put left static void Createorder (int[] A,btree root) {if (a = = NULL | | a.length        = = 0) return;            for (int i=0;i<a.length;i++) {if (i==0) {root.data = A[i]; }else{BTree leaf = new BTree ();                Leaf.data = A[i];            Insert (ROOT,LEAF); }}}} static void Insert (BTree root,btree leaf) {if (Root.data = = Leaf.data) {//= drop left if (r            Oot.left = = null) {root.left = leaf;            }else{Insert (root.left,leaf); }}else if (Root.data > Leaf.data) {//> put left if (root.left = = null) {Root.left = leaf            ;            }else{Insert (root.left,leaf);            }}else{//< put right if (root.right = = null) {root.right = leaf;            }else{Insert (root.right,leaf);        }}} static class btree{BTree left;        BTree right;    int data; }}

Favorite:Implementation of the Java two fork tree(This simple point is easier to read);
Java Basics Review Notes 10 data structure-sort binary tree

"Data Structure"--sorting algorithm--1.3, binary tree sort

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.