"Java" Big Talk Data structure (11) lookup Algorithm (2) (binary sort tree/two fork search tree)

Source: Internet
Author: User

in this paper, according to the "Big talk Data Structure" a book, the Implementation of Java version of the two-fork sorting tree/Two-fork search tree .

Binary Sorting Tree Introduction

In the previous blog, the efficiency of inserting and deleting sequential tables is also possible, but the search efficiency is very low, while in ordered linear tables, it is possible to use binary, interpolation, Fibonacci and other lookup methods, but because it is time-consuming to keep order, its insertion and deletion operations.

The two-fork sort tree, also known as a binary search tree, can be efficiently searched, while keeping the insertion and deletion operations high. For a typical two-fork sort tree.

Binary search trees have 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 the value of its root node;
(2) The right subtree of any node is not empty, then the value of all nodes on the right subtree is greater than the value of its root node;
(3) The left and right sub-trees of any node are also two-fork search trees respectively.

Find operations

Idea: The lookup value is compared to the node data, and the next comparison is made according to the size of the left subtree or right sub-tree.

Recursive lookup algorithm is used

/* * Find */public boolean searchbst (int key) {return Searchbst (key, root);} Private Boolean searchbst (int key, node node) {if (node = = null) return false;if (Node.data = = key) {return true;} else if (Node.data < key) {return Searchbst (key, node.rchild);} else {return Searchbst (key, Node.lchild);}}

 

Using a non-recursive lookup algorithm

/* Lookup, non-recursive */public boolean SearchBST2 (int key) {Node p = root;while (P! = null) {if (P.data > key) {p = p.lchild;} El Se if (P.data < key) {p = P.rchild;} else {return true;}} return false;}

  

Insert operation

Idea: Similar to lookup, but requires a parent node to be assigned.

Using a non-recursive insertion algorithm:

/* insert, self-thinking, non-recursive */public boolean insertbst (int key) {Node NewNode = new Node (key), if (root = null) {root = Newnode;retur n true;} Node f = null; Points to the parent node, node p = root; The pointer of the current node while (P! = null) {if (P.data > key) {f = p;p = P.lchild;} else if (P.data < key) {f = p;p = P.rchild;} El SE {System.out.println ("the same data already in the tree, no longer inserted!") "); return false;}} if (F.data > key) {f.lchild = NewNode;} else if (F.data < key) {f.rchild = NewNode;} return true;}

  

Recursive insertion algorithm is used:

/* INSERT, refer to someone else's blog, recursive * idea: The null case is excluded by recursion, otherwise it cannot be assigned */public boolean InsertBST2 (int key) {if (root = null) {root = new Node (key); return true;} Return InsertBST2 (key, root);} Private Boolean InsertBST2 (int key, node node) {if (Node.data > key) {if (Node.lchild = = null) {Node.lchild = new Node ( key); return true;} else {return InsertBST2 (key, Node.lchild);}} else if (Node.data < key) {if (Node.rchild = = null) {Node.rchild = new node (key); return true;} else {return InsertBST2 ( Key, Node.rchild);}} else {System.out.println ("the same data already in the tree, no longer inserted!") "); return false;}}

Delete operation

Ideas:

(1) Delete the leaf node.

Direct deletion;

(2) Delete nodes that have only left or right sub-trees

The subtree moves to the point where the node is deleted;

(3) Delete the nodes of the left and right subtree.

Locate the direct precursor (or direct back drive) s of the Delete node p, replace the node p with S, and then delete the node s as shown in.

First find the deletion node location and its parent node.

/* Delete operation, first find delete node location and its parent node * Because of the need to have a parent node, so I did not think of the recursive method (except to make the node object with a Parent property) */public boolean deletebst (int key) {if (root = nul L) {System.out.println ("Empty table, delete failed"); return false;} Node f = null; Points to the parent node, node p = root; Points to the current node while (P! = null) {if (P.data > key) {f = p;p = P.lchild;} else if (P.data < key) {f = p;p = P.rchild;} els e {Delete (P, f); return true;}} SYSTEM.OUT.PRINTLN ("The data does not exist"); return false;}

  

Then the deletion of node p is based on the above ideas: (Note that the deletion node is the root node)

/* * Delete node P operation * must have parent node, because Java cannot directly get the address of the variable p (cannot use *p= (*p)->lchild) */private Boolean Delete (node p, node F) {//P for delete node, F for its Parent node if (P.lchild = = null) {///left dial hand tree is empty, re-connect right subtree if (p = = root) {//deleted node is root node, this case cannot ignore root = Root.rchild;} else {if (F.data > P . Data) {//The deleted node is the left node of the parent node, the same as F.lchild = P.rchild;p = null;} else {//deleted node is the right node of the parent node, the same as F.rchild = P.rchild;p = null;}}}  else if (P.rchild = = null) {//Right subtree is empty, re-connect left dial hand tree if (p = = root) {//deleted node is root node root = Root.lchild;} else {if (F.data > P.data) {f.lchild = P.lchild;p = null;} else {f.rchild = P.lchild;p = null;}}} else {///The subtree is not empty, the deletion location replaces node Q with the predecessor node, s;q = P;s = P.lchild;while (s.rchild! = null) {//finds the maximum precursor sq = S;s = S.rchild of the node to be deleted;} P.data = S.data; Change P's data to OKIF (q! = p) {q.rchild = S.lchild;} else {  //This case also don't ignore q.lchild = S.lchild;} s = null;} System.out.println ("Delete succeeded!"); return true;}

  

Complete code (with test code)
Package bst;/** * Binary sort tree (binary find tree) * If generic, requires the T extends comparable<t> static problem * @author Yongh * */class Node {int data ; Node Lchild, rchild;public node (int data) {This.data = Data;lchild = Null;rchild = null;}} public class Bstree {private Node root;public Bstree () {root = null;} /* * Find */public boolean searchbst (int key) {return Searchbst (key, root);} Private Boolean searchbst (int key, node node) {if (node = = null) return false;if (Node.data = = key) {return true;} else if (Node.data < key) {return Searchbst (key, node.rchild);} else {return Searchbst (key, Node.lchild);}} /* Lookup, non-recursive */public boolean SearchBST2 (int key) {Node p = root;while (P! = null) {if (P.data > key) {p = p.lchild;} El Se if (P.data < key) {p = P.rchild;} else {return true;}} return false;} /* insert, self-thinking, non-recursive */public boolean insertbst (int key) {Node NewNode = new Node (key), if (root = null) {root = Newnode;retur n true;} Node f = null; Points to the parent node, node p = root; The pointer of the current node while (P! = null) {if (P.data > key) {f = P;p = P.lchild;} else if (P.data < key) {f = p;p = P.rchild;} else {System.out.println ("The tree already has the same data and is no longer inserted!) "); return false;}} if (F.data > key) {f.lchild = NewNode;} else if (F.data < key) {f.rchild = NewNode;} return true;} /* INSERT, refer to someone else's blog, recursive * idea: The null case is excluded by recursion, otherwise it cannot be assigned */public boolean InsertBST2 (int key) {if (root = null) {root = new Node (key); return true;} Return InsertBST2 (key, root);} Private Boolean InsertBST2 (int key, node node) {if (Node.data > key) {if (Node.lchild = = null) {Node.lchild = new Node ( key); return true;} else {return InsertBST2 (key, Node.lchild);}} else if (Node.data < key) {if (Node.rchild = = null) {Node.rchild = new node (key); return true;} else {return InsertBST2 ( Key, Node.rchild);}} else {System.out.println ("the same data already in the tree, no longer inserted!") "); return false;}} /* Delete operation, first find delete node location and its parent node * Because of the need to have a parent node, so I did not think of the recursive method (except to make the node object with a Parent property) */public boolean deletebst (int key) {if (root = nul L) {System.out.println ("Empty table, delete failed"); return false;} Node f = null; Points to the parent node, node p = root; Points to the current node while(P! = null) {if (P.data > key) {f = p;p = P.lchild;} else if (P.data < key) {f = p;p = P.rchild;} else {delete (P, f); return true ;}} SYSTEM.OUT.PRINTLN ("The data does not exist"); return false;} /* * Delete node P operation * must have parent node, because Java cannot directly get the address of the variable p (cannot use *p= (*p)->lchild) */private Boolean Delete (node p, node F) {//P for delete node, F for its Parent node if (P.lchild = = null) {///left dial hand tree is empty, re-connect right subtree if (p = = root) {//deleted node is root node, this case cannot ignore root = Root.rchild;} else {if (F.data > P . Data) {//The deleted node is the left node of the parent node, the same as F.lchild = P.rchild;p = null;} else {//deleted node is the right node of the parent node, the same as F.rchild = P.rchild;p = null;}}}  else if (P.rchild = = null) {//Right subtree is empty, re-connect left dial hand tree if (p = = root) {//deleted node is root node root = Root.lchild;} else {if (F.data > P.data) {f.lchild = P.lchild;p = null;} else {f.rchild = P.lchild;p = null;}}} else {///The subtree is not empty, the deletion location replaces node Q with the predecessor node, s;q = P;s = P.lchild;while (s.rchild! = null) {//finds the maximum precursor sq = S;s = S.rchild of the node to be deleted;} P.data = S.data; Change P's data to OKIF (q! = p) {q.rchild = S.lchild;} else {q.lchild = S.lchild;} s = null;} System.out.println ("Delete succeeded!"); return true;} /* * Middle sequence traversal */public void Inorder () {inorder (root); System.out.println ();} public void Inorder (node node) {if (node = = null) Return;inorder (node.lchild); System.out.print (Node.data + ""); Inorder (node.rchild);} /* * Test code */public static void Main (string[] args) {Bstree atree = new Bstree (); Bstree bTree = new Bstree (); int[] arr = {$, n, N, N, a, A, N, a, A, a, a, a.};for (int a:arr) {atree.insertbst (a); b Tree.insertbst2 (a);} Atree.inorder (); Btree.inorder (); System.out.println (Atree.searchbst (35)); System.out.println (btree.searchbst2); atree.deletebst; Atree.inorder ();}}

  

 * Panax Notoginseng  - Wuyi  -  +  the  the  the  About  * Panax Notoginseng  - Wuyi  -  +  the  the  the  About truetrueDelete succeeded! * Panax Notoginseng Wuyi  -  +  the  the  the  About 
Bstree

"Java" Big Talk Data structure (11) lookup Algorithm (2) (binary sort tree/two fork search tree)

Related Article

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.