Detailed Java two fork sort tree _java

Source: Internet
Author: User
Tags class definition

One or two fork sort tree definition
1. Two-fork sort tree definition
Binary sort trees (Binary sort tree) also known as binary lookup (search) tree (Binary). It is defined as: a two-fork sort tree or an empty tree, or a two-fork tree that satisfies the following properties:
① if its left subtree is not empty, the value of all nodes in the left subtree is small at the root node;
② if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of the root node;
③ the left and right subtree itself is a binary sort tree.
These properties are referred to as binary sort tree properties (BST property), so the binary sort tree is actually satisfying the BST nature of the two-fork tree.


2. The nature of the two-fork sort tree
The sequential traversal sequence is an ascending sequential sequence by traversing the binary sort tree in the middle order.

3. Two-fork sort tree Insertion
Insert a new node in the binary sort tree to ensure that the inserted two-tree still conforms to the definition of the binary sort tree.
Insert procedure:
If the binary sort tree is empty, the node *s to be inserted into the empty tree as the root node;
When Non-null, will be inserted node keyword s->key and root keyword t->key to compare, if S->key = T->key, then do not need to insert, if s->key< T->key, then inserted into the root of the left subtree, if s >key> T->key, it is inserted into the right subtree of the root. The insert process in the subtree is the same as the insert process in the tree, until the node *s as a new leaf is inserted into the two-fork sort tree, or until a node is found that has the same key word.

4. Two-fork sort Tree Lookup
Assuming that the root node pointer of the binary sort tree is root and the given key value is K, the lookup algorithm can be described as:
① initial value: q = root;
② if K = Q-> Key, then find success, algorithm end;
③ otherwise, if K Q-> Key, and the left subtree of Q is not empty, then Q will be sent to the Zogen Q, step ②; otherwise, find failure, end algorithm;
④ otherwise, if K > Q-> Key, and Q's right subtree is non-empty, then the right Zishugen of Q is sent to Q, and the steps are ②. Otherwise, the lookup fails and the algorithm ends.

5. Two-fork Sorting tree deletion
If deleted node is *p, its parents are *f, without losing general, set *P is *f left child, the following three kinds of situation discussion:
⑴ If the node *p is a leaf node, simply modify the *f pointer of its parent node.
⑵ If the node *p only Zoozi Tree Pl or only the right subtree PR, so long as the PL or PR becomes its parent node of the left subtree.
⑶ if the left and right subtrees of the node *p are not empty, first find the *p (or subsequent) node *s (Note that *s is the lower-right node in the left subtree of *p, its right chain is empty), and then there are two approaches: ① makes the left subtree of the *p directly linked to the left chain of the *p parent node *f, and the right subtree of *p is linked to the * The *s of the middle order of P is on the right chain of the forward node. Instead of *p (that is, copying *s data to *p), the ② of the *p in the middle sequence of the *s is linked to the left (or right) chain of *s's parent node *q.
6, the traversal of the binary tree
There are three ways to traverse a binary tree, as follows:
(1) Pre-sequence traversal (DLR), first accessing the root node, then traversing the left subtree, and finally traversing the right subtree. Jenkgen-left-right.
(2) Sequence traversal (LDR), first traversing the left subtree, then accessing the root node, and finally traversing the right subtree. denoted left-root-right.
(3) subsequent traversal (LRD), first traversing the left subtree, and then traversing the right subtree, the last access to the root node. Denoted left-right-root.

Second, code writing
1, the tree node class definition 0

Package Com.lin; 
/** 
 * Function Summary: * * Public 
class TreeNode {public 
   
  Integer data; 
   
  /* The parent node of the node 
  /public TreeNode parent; 
   
  /* The left child node of the node * 
  /public TreeNode ieft; 
   
  /* The right child node of the node * * 
   
  TreeNode Public TreeNode (Integer data) { 
    this.data = data; 
     
  } 
 
  @Override public 
  String toString () {return 
    "TreeNode [data=] + data +"] "; 
  } 
     
} 

2, the definition of binary sort tree

Package Com.lin; 
    
   /** * Feature Summary: sort/Balance binary tree/public class Searchtree {public TreeNode root; 
     
  public long size; 
 
    /** * Add node to tree * @param data * @return Boolean Insert successfully returns TRUE */Public Boolean Addtreenode (Integer data) { 
      if (null = = root) {root = new TreeNode (data); 
      SYSTEM.OUT.PRINTLN ("Data successfully inserted into the Balanced binary tree"); 
    return true; 
    } TreeNode TreeNode = new TreeNode (data);//impending insertion of the database TreeNode currentnode = root; 
 
    TreeNode parentnode; 
        while (true) {parentnode = currentnode;//Save parent node//Insert data than parent node small if (Currentnode.data > Data) { 
        CurrentNode = Currentnode.left; 
          The left child node of the current parent node is an empty if (null = = CurrentNode) {parentnode.left = TreeNode; 
          Treenode.parent = parentnode; 
          SYSTEM.OUT.PRINTLN ("Data successfully inserted into the two fork Lookup tree"); 
          size++; 
        return true; 
     //Insert data larger than parent node} else if (Currentnode.data < data) {   CurrentNode = Currentnode.right; 
          The right child node of the current parent node is an empty if (null = = CurrentNode) {parentnode.right = TreeNode; 
          Treenode.parent = parentnode; 
          SYSTEM.OUT.PRINTLN ("Data successfully inserted into the two fork Lookup tree"); 
          size++; 
        return true; 
        } else {System.out.println ("input data is the same as the node's data"); 
      return false; }}/** * @param data * @return TreeNode/public TreeNode Findtreenode (Integer data) 
    {if (null = = root) {return null; 
    } TreeNode current = root; 
      While (the current!= null) {if (Current.data > data) {current = Current.left; 
      }else if (Current.data < data) {current = Current.right; 
      }else {return current; 
  } return null; 
 } 
   
}

There is only one way to add and find here temporarily
3, front, middle, and back traversal

Package Com.lin; 
 
Import Java.util.Stack; 
   /** * Features Overview: * * Public class Treeorder {/** * Recursive implementation of the pre-sequence traversal * @author Linbingwen * @since August 29, 2015 
      * @param treeNode */public static void Preordermethodone (TreeNode treeNode) {if (null!= treeNode) { 
      System.out.print (Treenode.data + ""); 
      if (null!= treenode.left) {preordermethodone (treenode.left); 
 
      } if (null!= treenode.right) {preordermethodone (treenode.right); /** * Loop implementation Pre-sequence traversal * @param treeNode/public static void Preordermethodtwo (TreeNode Treeno 
      DE) {if (null!= treeNode) {stack<treenode> Stack = new stack<treenode> (); 
      Stack.push (TreeNode); 
        while (!stack.isempty ()) {TreeNode Tempnode = Stack.pop (); 
        System.out.print (Tempnode.data + ""); 
 The right child node is not NULL, and the right child node is first placed in the IF (null!= tempnode.right) {Stack.push (tempnode.right);       The left child node is placed on the right child node, the next fetch if (null!= tempnode.left) {Stack.push (tempnode.left); /** * Recursive implementation in sequence traversal * @param treeNode/public static void Medordermethodone (Tr Eenode TreeNode) {if (null!= treeNode) {if (null!= treenode.left) {Medordermethodone (Treenod 
      E.left); 
      } System.out.print (Treenode.data + ""); 
      if (null!= treenode.right) {medordermethodone (treenode.right); The/** * Loop implements the sequence traversal * @param treeNode/public static void Medordermethodtwo (Treenod  
    E treeNode) {stack<treenode> Stack = new stack<treenode> ();  
    TreeNode current = TreeNode;  
        While (the current!= null | |!stack.isempty ()) {while (current!= null) {Stack.push (current);  
      current = Current.left;  
        } if (!stack.isempty ()) {current = Stack.pop (); System.out.pRint (current.data+ "");  
      current = Current.right; /** * Recursive implementation of the subsequent traversal * @param treeNode */public static void Postordermethodone (Treenod E TreeNode) {if (null!= treeNode) {if (null!= treenode.left) {postordermethodone (TreeNode). 
      left); 
      } if (null!= treenode.right) {postordermethodone (treenode.right); 
    } System.out.print (Treenode.data + ""); }/** * Loop implementation subsequent traversal * @param treeNode/public static void Postordermethodtwo (TreeNode tree 
      Node) {if (null!= treeNode) {stack<treenode> Stack = new stack<treenode> (); 
      TreeNode current = TreeNode; 
      TreeNode rightnode = null;  
          While (the current!= null | |!stack.isempty ()) {while (current!= null) {Stack.push (current);  
        current = Current.left;  
        Current = Stack.pop (); While (the current!= null && (Current.right = = NULL | |  
          Current.right = = Rightnode)) {System.out.print (Current.data + "");  
          Rightnode = current;  
            if (Stack.isempty ()) {System.out.println ();  
          Return  
        Current = Stack.pop ();  
        } stack.push (current);  
      current = Current.right; 
 }  
       
       
       
    } 
  } 
   
}

4, using method

Package Com.lin; /** * Features Overview:/public class Searchtreetest {/** * @param args/public static void Main (string[) a 
    RGS) {Searchtree tree = new Searchtree (); 
    Tree.addtreenode (50); 
    Tree.addtreenode (80); 
    Tree.addtreenode (20);   
    Tree.addtreenode (60); 
    Tree.addtreenode (10); 
    Tree.addtreenode (30); 
    Tree.addtreenode (70);   
    Tree.addtreenode (90); 
    Tree.addtreenode (100); 
    Tree.addtreenode (40); 
    System.out.println ("==============================" + "recursive first-order traversal start" + "=============================="); 
    Treeorder.preordermethodone (Tree.root); 
    System.out.println (); 
    System.out.println ("==============================" + "cycle of the first sequence traversal start" + "=============================="); 
    Treeorder.preordermethodtwo (Tree.root); 
    System.out.println (); 
    System.out.println ("==============================" + "recursive" followed by the beginning of the traversal) + "=============================="); 
    Treeorder.postordermethodone (Tree.root); System.out.println ();
    System.out.println ("==============================" + "follow the cycle of the subsequent traversal start" + "=============================="); 
    Treeorder.postordermethodtwo (Tree.root); 
    System.out.println (); 
    System.out.println ("==============================" + "recursive sequence traversal start" + "=============================="); 
    Treeorder.medordermethodone (Tree.root); 
    System.out.println (); 
    System.out.println ("==============================" + "loop in the middle sequence traversal start" + "=============================="); 
 
  Treeorder.medordermethodtwo (Tree.root); 
 } 
 
}

Output results:


Again, the lookup process is as follows:

TreeNode node = tree.findtreenode (100); 


The result is correct.

The above is about the Java two fork sorting tree detailed introduction, I hope to learn Java program to help you.

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.