Binary search Tree of Java data structure

Source: Internet
Author: User

Java data Structure binary search tree 1, binary search tree compositionBinary search tree, also known as binary sorting tree, it is either an empty tree, or a non-empty binary tree with the following characteristics, you need to meet a few three conditions:(1) If its left subtree is not empty, then all the nodes on the left subtree are small keywords of the root node;(2) if its right subtree is non-empty, then all nodes on the right subtree have keywords that are greater than (can be equal to) the root node.(3) Zuozi the right subtree itself is a binary search treein the algorithm description, the comparison of the node value is represented by the comparison of its keywords, because if the value of the node is a class type, the class must implement the system-providedJava.lang.comparableinterface and define aCompareTomember methods to compare objects. Two fork search tree 2, Binary search tree implementationThe operation of the two-fork search tree is important in three ways: 1, inserting a node, 2, deleting a node, 3, finding out if there is a node;The two-fork search tree is implemented through chained storage, first defining the node:
Defines the node type class Node{object obj; Node left; Node Right;public node (Object obj,node Left,node right) {this.obj=obj;this.left=left;this.right=right;}}
1. Insert a nodewhen inserting node operation in binary search tree, it is necessary to ensure the property of binary search tree is invariant, that is, the binary search tree must be ordered according to Zuozi, root node and right subtree after insert operation. The process of inserting a two-fork search tree is to first find the insertion position of the new node, and then insert the link. Find the insertion position starting from the root node, if the root pointer is empty, the new node is the root node; otherwise, if obj equals the root node, the value node with obj already exists, the insertion position is not allowed, and false indicates that the insertion failed; The left pointer along the root node continues to find the insertion position on the left subtree, and if obj is greater than the root node, the right pointer along the root continues to find the insertion position on the right subtree. The position of the null pointer is the insertion position of the new node of obj when the left pointer or the right pointer of a node is found to be empty.when inserting a link, the Wakahara tree is empty, then assigns the new node pointer to boot, which becomes the root node of the entire binary search tree, otherwise assigns the new node to the left pointer field of the PT node or to the right pointer field as the left child or right child of the node. Returns True when the insert is complete. The specific implementation code is as follows:
Insert an element, insert success Returns True Failure returns False@overridepublic Boolean insert (Object obj) {Node rt=root,pt=null;while (rt!=null) {pt= rt;//the previous node to the record if ((integer) (rt.obj) > (integer) obj) {rt=rt.left;} else if ((integer) (rt.obj) < (integer) obj) {rt=rt.right;} Else{return false;}} Node node=new node (obj,null,null);//Create a new node if (pt==null) {Root=node;} else if ((integer) (pt.obj) < (integer) obj) {pt.right=node;} Else{pt.left=node;} return true;}
2. Find a nodebecause of the particularity of the binary search tree, the lookup process can be expressed as: if the binary search tree is empty, it indicates that the lookup failed, should return null, otherwise, if obj equals the value of the current root node, then the lookup succeeds, should return the complete value of the node, if obj is less than the current root node value , the root node of the right subtree continues to be compared. The root of the tree along the left child or right child by layer down, each comparison, down one layer, when the value is found to the OBJ node, indicating that the lookup succeeds, should return the value of the node, or find the null pointer value, the search failed, should return null. The implementation code is as follows:
Finding an element to  find will return the element and cannot find any return @overridepublic object (Object obj) {if (root==null) return null; Node Rt=root;while (rt!=null) {if ((integer) (rt.obj) > (integer) obj) {rt=rt.left;} else if ((integer) (rt.obj) < (integer) obj) {rt=rt.right;} Else{return rt.obj;}} return null;}
3. Delete a nodeDelete the node (element) from the two-fork search tree, it may delete the leaf node, it may delete the branch node, when the branch node is deleted, the link relationship between the original nodes is destroyed, and the pointer needs to be re-modified so that it remains a binary search tree after deletion.1. Delete leaf nodesThis kind of deletion is simple, as long as the pointer that links its parent node to it is removed.2. Delete single nodeThis deletion is also relatively simple, because the node only Zuozi or right subtree one, that is, its buddy only left child or right child one. When you delete the node, simply link the unique successor pointer to the link location where it is located.3. Delete the double nodeThis deletion is complicated because the node to be deleted has two successive pointers, which need to be handled properly, one way to delete this node is to first assign its middle order precursor to the value range of the node, and then delete its middle order precursor node, because the right pointer of the middle order precursor node is empty. So just connect the left pointer of the middle sequence precursor node to the link location where the middle sequence precursor node is located.The implementation code is as follows:
Delete a node from a two-fork search tree, divided into three classes//1, delete leaf nodes, 2, delete single node; 3, delete double node @overridepublic boolean delete (Object obj) {// Deletes the node of obj equal to the given value from the two-fork search tree, returns True if successful, or Falseif (Root==null) {return false;} Node Rt=root,pt=null;while (rt!=null) {if ((integer) (rt.obj) = = (integer) obj) {break;} else if ((integer) (rt.obj) > (integer) obj) {pt=rt;rt=rt.left;} else {pt=rt;rt=rt.right;}} if (rt==null) return false;//If the element is not found, return empty//divide three cases to find the node rt//delete the leaf node if (rt.left==null && rt.right==null) {// Delete leaf node, direct delete operation if (rt==root) Root=null;else if (pt.left==rt) Pt.left=null;else pt.right=null;} When you delete an RT node that is a single branch, handle the IF (Rt.left==null | | rt.right==null) {if (rt==root) {////////////Rt.left==null) root= if the Rt junction is the root node Rt.right;else Root=rt.left;} else if (Pt.left==rt && rt.left==null) {//Processing single node, divided into four cases pt.left=rt.right;} else if (Pt.left==rt && rt.right==null) {pt.left=rt.left;} else if (Pt.right==rt && rt.left==null) {pt.right=rt.right;} else if (Pt.right==rt && rt.right==null) {pt.right=rt.left;} else;} Processing has a Gemini node else if (Rt.left!=null && Rt.right!=null) {Node s1=rt,s2=rt.left;while (s2.right!=null) {s1=s2;s2=s2.right;} rt.obj=s2.obj;//the value of the middle sequence precursor node S2 to the RT node if (S1==RT) {//to the RT's central sequence precursor node is RT's left child's situation to process rt.left=s2.left;} else{//the central sequence precursor node of RT to the right subtree of the left Haizi to deal with S1.right=s2.left;}} Return true;//after delete succeeds}
The output Search binary tree code is as follows:
Output binary tree public void Inordertravers (node node) {if (node!=null) {inordertravers (node.left); System.out.print (node.obj+ ""); Inordertravers (node.right);} else;}








Binary search Tree of Java data structure

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.