Sort binary tree and its Java implementation

Source: Internet
Author: User

Defined

The definition of a sort binary tree is also defined recursively and needs to be met:

(1) If its left subtree is not empty, the values of all nodes on the left subtree are smaller than the value of the root node;

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

(3) The left and right sub-trees are also sorted binary trees

For a sort binary tree, an ordered sequence can be obtained from small to large if it is traversed by the middle sequence.

Create

The procedure for creating a sort binary tree is to continually add a new node (p) As a sort of binary tree:

(1) Start the search with the root node (root) for the current node.

(2) Comparing the value of the new node P with the value of current;

(3) If p.data>current.data, then current=current.right; if P.data<current.data, then current=current.left;

(4) Repeat (2) (3) until the appropriate leaf node position is found;

(5) Add p to the appropriate location found above, if the new node is larger, then add to the right child node; otherwise, add the left Dial hand node

Delete a node

When you delete a node from a sorted binary tree, you must maintain it as it remains a two-fork tree:

The right subtree of the left subtree of the p,p parent node of the node to be deleted q,p PR of the tree pl,p

(1) p is a leaf node that is removed directly from its parent node;

(2) P only left (right) subtree, add pl (PR) to P's parent node Q's Left (right) subtree;

(3) The left and right subtree are not empty, there are two methods of treatment:

    • Set PL to the left or right sub-node of q (depending on p is the left and right child node of its parent node Q), set PR to the right child node of the middle sequence precursor of P s (S is the most right node of PL, which is the largest node in PL)
    • Substituting p for the middle sequence precursor or successor of P, and then deleting the middle sequence precursor or successor node from the original sort binary tree (that is, using the minimum node greater than P or the maximum node less than p to replace the P node)

Java implementation code:

Package Com.liuhao.datastructures;import Java.util.arraydeque;import Java.util.arraylist;import java.util.List; Import Java.util.queue;public class Sortedbintree <t extends Comparable>{static class node{object data; Node parent; Node left;  Node Right;public node (Object data, node parent, node left, node right) {This.data = Data;this.parent = Parent;this.left = Left;this.right = right;} Public String toString () {return "[data=" + Data + "]";} public boolean equals (Object obj) {if (this = = obj) {return true;} if (obj.getclass () = = Node.class) {node target = (node) obj;return data.equals (target.data) && left = = Target.left & amp;& right = = Target.right && parent = = target.parent;} return false;}} Private Node root;public Sortedbintree () {root = null;} Public Sortedbintree (T o) {root = new Node (o, NULL, NULL, NULL);} /** * Add node * @param ele element of the new node */public void Add (T ele) {if (root = null) {root = new node (ele, NULL, NULL, NULL);} Else{node current = root; Node parent = null;int cmp;//SearchThe appropriate leaf node to add a new node to the leaf node for the parent node do{parent = current;cmp = Ele.compareto (current.data);//If the value of the new node is greater than the value of the current node if (CMP > 0) {// Takes the right child node of the current node as the current node: current.right;} Else{current = Current.left;}} while (current! = NULL);//Creates a new node NewNode = new Nodes (ele, parent, NULL, NULL);//If the value of the new node is greater than the value of the parent node if (CMP > 0) {parent.righ t = NewNode;} Else{parent.left = NewNode;}}} /** * Delete node * @param ele */public void Remove (T ele) {node target = getnode (ele); if (target = = null) {return;} Both left and right subtrees are empty if (Target.left = = NULL && Target.right = = null) {if (target = = root) {root = null;} else{//the deleted node is the parent node's left child node if (target = = Target.parent.left) {//sets the parent node of target to nulltarget.parent.left = null;} Else{target.parent.right = null;} target.parent = null;}} Zoling right not empty else if (Target.left = = NULL && target.right! = null) {if (target = = root) {root = Target.right;} else{//the deleted node is the parent node's left child node if (target = = target.parent.left) {target.parent.left = Target.right;} Else{target.parent.right = Target.right;} The parent of Target's right subtree points to Target's parenttarget.right.pareNT = target.parent;}} else if (target.left! = null && Target.right = = null) {if (target = = root) {root = Target.left;} else{//the deleted node is the parent node's left child node if (target = = target.parent.left) {target.parent.left = Target.left;} Else{target.parent.right = Target.left;} Let the parent of Target's right subtree point to target's parenttarget.left.parent = Target.parent;}} Left and right are not empty else{//leftmaxnode:target the value of the node Leftmaxnode = target.left;//The largest node in the left subtree of the target. Leftmaxnode.right = null) {Leftmaxnode = Leftmaxnode.right;} Remove the Leftmaxnode node from the original subtree leftMaxNode.parent.right = Null;leftmaxnode.parent = target.parent;if (target = = Target.parent.left) {target.parent.left = Leftmaxnode;} Else{target.parent.right = Leftmaxnode;} Leftmaxnode.left = Target.left;leftmaxnode.right = Target.right;target.parent = Target.left = Target.right = null;}} /** * Search node by specified value * @param ele specified value * @return node */public nodes getnode (T ele) {//Start search from root node p = root;while (P! = null) {int cm p = Ele.compareto (p.data); if (CMP < 0) {p = p.left;} else if (cmp > 0) {p = P.right;} Else{return p;}} return null;} /** * Breadth-First traversal * @return */public list<node> breadthfirst () {queue<node> Queue = new arraydeque<node> (); list<node> list = new arraylist<node> (); if (root! = null) {Queue.offer (root);} while (!queue.isempty ()) {//Adds the queue's "tail" element to the list List.add (Queue.peek ());//pop-up tail node nodes P = queue.poll ();//If the left Dial hand node is not NULL, Add it to "queue" if (p.left! = null) {Queue.offer (p.left);} if (p.right! = null) {Queue.offer (p.right);}} return list;} /** * Middle Sequence traversal * @return */public list<node> initerator () {return initerator (root);} Private list<node> Initerator (node node) {list<node> List = new arraylist<node> ();//recursively handle left subtree if ( Node.left = null) {List.addall (Initerator (Node.left));} Handle root node List.add (node),//recursively handle right subtree if (node.right! = null) {List.addall (Initerator (Node.right));} return list;}}


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.