Tree-balance Binary Tree insertion and search JAVA Implementation, binary tree search java

Source: Internet
Author: User

Tree-balance Binary Tree insertion and search JAVA Implementation, binary tree search java

Package com. tomsnail. data. tree; /*** AVL binary balancing tree * @ author tomsnail * @ date 4:35:50 on January 1, March 30, 2015 */public class AVLTree {/*** root node * @ author tomsnail * @ date 4:36:54 on January 1, March 30, 2015 * /private AVLNode rootNode; private String bucket type = "";/*** add a node * @ author tomsnail * @ date 4:36:08 on January 1, March 30, 2015 */public void add (int value) {AVLNode subNode = null; if (rootNode = null) {subNode = new AVLNode (value); RootNode = subNode;} else {subNode = addNode (rootNode, value);} reBuild (subNode);} private AVLNode addNode (AVLNode node, int value) {AVLNode subNode = null; if (node. getValue ()> value) {if (node. getLeftNode () = null) {subNode = new AVLNode (value); node. setLeftNode (subNode);} else {subNode = addNode (node. getLeftNode (), value) ;}} if (node. getValue () <value) {if (node. getRightNode () = null) {subNode = ne W AVLNode (value); node. setRightNode (subNode);} else {subNode = addNode (node. getRightNode (), value) ;}} return subNode;}/*** rebalance tree * @ author tomsnail * @ date 5:42:00 on January 1, March 30, 2015 */private void reBuild (AVLNode node) {if (node! = Null) {AVLNode tempRootNode = findTempRootNode (node); if (tempRootNode! = Null) {if (bucket type. equals ("ll") {Lrotate (node, tempRootNode);} else if (bucket type. equals ("rr") {Rrotate (node, tempRootNode);} else if (bucket type. equals ("lr") {Rrotate (node, tempRootNode. getLeftNode (); Lrotate (node, tempRootNode);} else if (bucket type. equals ("rl") {Lrotate (node, tempRootNode. getRightNode (); Rrotate (node, tempRootNode) ;}}}/*** left-handed * @ author tomsnail * @ date 9:23:28 on January 1, March 30, 2015 * /Private void Rrotate (AVLNode node, AVLNode tempRootNode) {AVLNode rotateNode = tempRootNode. getRightNode (); AVLNode rootNode = tempRootNode. getRootNode (); String type = ""; if (rootNode! = Null) {if (rootNode. getLeftNode () = tempRootNode) {type = "l";} else {type = "r" ;}} AVLNode adjustNode = rotateNode. getLeftNode (); rotateNode. setLeftNode (tempRootNode); tempRootNode. setRightNode (null); if (adjustNode! = Null) {tempRootNode. setRightNode (adjustNode);} if (rootNode = null) {rotateNode. setRootNode (null); this. rootNode = rotateNode;} if (type. equals ("r") {rootNode. setRightNode (rotateNode);} else if (type. equals ("l") {rootNode. setLeftNode (rotateNode);}/*** * @ author tomsnail * @ date 9:23:28 on January 1, March 30, 2015 */private void Lrotate (AVLNode node, AVLNode tempRootNode) {AVLNode rotateNode = tempRootNod E. getLeftNode (); AVLNode rootNode = tempRootNode. getRootNode (); String type = ""; if (rootNode! = Null) {if (rootNode. getLeftNode () = tempRootNode) {type = "l";} else {type = "r" ;}} AVLNode adjustNode = rotateNode. getRightNode (); rotateNode. setRightNode (tempRootNode); tempRootNode. setLeftNode (null); if (adjustNode! = Null) {tempRootNode. setLeftNode (adjustNode);} if (rootNode = null) {rotateNode. setRootNode (null); this. rootNode = rotateNode;} if (type. equals ("r") {rootNode. setRightNode (rotateNode);} else if (type. equals ("l") {rootNode. setLeftNode (rotateNode);}/*** find the root node with the minimum imbalance * @ author tomsnail * @ date 5:40:55 on January 1, March 30, 2015 */private AVLNode findTempRootNode (AVLNode node) {AVLNode noB = getNoBalance (node ); If (noB = null) {return null;} if (isTypeLL (noB) {bucket type = "ll";} else if (isTypeRR (noB )) {bucket type = "rr";} else if (isTypeLR (noB) {bucket type = "lr";} else if (isTypeRL (noB) {bucket type = "rl ";} return noB;} private boolean isTypeLL (AVLNode noB) {try {if (noB. getRightNode () = null & noB. getLeftNode (). getRightNode () = null &&! NoB. getLeftNode (). getLeftNode (). hasSubNode () {return true;} if (noB. getRightNode ()! = Null & noB. getLeftNode (). getRightNode ()! = Null & noB. getLeftNode (). getLeftNode (). hasSubNode () {return true;} catch (Exception e) {} return false;} private boolean isTypeRR (AVLNode noB) {try {if (noB. getLeftNode () = null & noB. getRightNode (). getLeftNode () = null &&! NoB. getRightNode (). getRightNode (). hasSubNode () {return true;} if (noB. getLeftNode ()! = Null & noB. getRightNode (). getLeftNode ()! = Null & noB. getRightNode (). getRightNode (). hasSubNode () {return true;} catch (Exception e) {} return false;} private boolean isTypeLR (AVLNode noB) {try {if (noB. getRightNode () = null & noB. getLeftNode (). getLeftNode () = null &&! NoB. getLeftNode (). getRightNode (). hasSubNode () {return true;} if (noB. getRightNode ()! = Null & noB. getLeftNode (). getLeftNode ()! = Null & noB. getLeftNode (). getRightNode (). hasSubNode () {return true;} catch (Exception e) {} return false;} private boolean isTypeRL (AVLNode noB) {try {if (noB. getLeftNode () = null & noB. getRightNode (). getRightNode () = null &&! NoB. getRightNode (). getLeftNode (). hasSubNode () {return true;} if (noB. getLeftNode ()! = Null & noB. getRightNode (). getRightNode ()! = Null & noB. getRightNode (). getLeftNode (). hasSubNode () {return true;} catch (Exception e) {} return false;} private AVLNode getNoBalance (AVLNode node) {if (node. getRootNode () = null) {return null;} else {if (! IsBalance (node. getRootNode () {return node. getRootNode ();} else {return getNoBalance (node. getRootNode () ;}}/ *** find a node * @ author tomsnail * @ date 4:36:35 on January 1, March 30, 2015 */public AVLNode find (int value) {return findWith2 (rootNode, value);} private AVLNode findWith2 (AVLNode node, int value) {if (node = null) {return null;} System. out. println (node. getValue (); if (node. getValue ()> value) {return find With2 (node. getLeftNode (), value);} else if (node. getValue () <value) {return findWith2 (node. getRightNode (), value);} else {return node;} public void midScan (AVLNode node) {if (node = null) {return;} midScan (node. getLeftNode (); System. out. println (node. getValue (); midScan (node. getRightNode ();} public AVLNode getRootNode () {return rootNode;} public static void main (String [] args) {int [] is = new Int [] {, 80, 19,7, 50, 60, 40, 55, 65,53, 80 AVLTree tree = new AVLTree (); for (int I = 0; I <is. length; I ++) {tree. add (is [I]);} System. out. println (tree. getRootNode (). getValue (); System. out. println ("----------------------------"); tree. midScan (tree. getRootNode (); System. out. println (isBalance (tree. getRootNode ();} public s Tatic int depth (AVLNode node) {if (node = null) {return 0;} else {int ld = depth (node. getLeftNode (); int rd = depth (node. getRightNode (); return 1 + (ld> rd? Ld: rd) ;}} public static boolean isBalance (AVLNode node) {if (node = null) return true; int dis = depth (node. getLeftNode ()-depth (node. getRightNode (); if (dis> 1 | dis <-1) return false; else return isBalance (node. getLeftNode () & isBalance (node. getRightNode () ;}} class AVLNode {private int value; private AVLNode leftNode; private AVLNode rightNode; private AVLNode rootNode; public int getVa Lue () {return value;} public void setValue (int value) {this. value = value;} public AVLNode getLeftNode () {return leftNode;} public void setLeftNode (AVLNode leftNode) {this. leftNode = leftNode; if (leftNode! = Null) {leftNode. setRootNode (this) ;}} public AVLNode getRightNode () {return rightNode;} public void setRightNode (AVLNode rightNode) {this. rightNode = rightNode; if (rightNode! = Null) {rightNode. setRootNode (this) ;}} public AVLNode getRootNode () {return rootNode;} public void setRootNode (AVLNode rootNode) {this. rootNode = rootNode;} public boolean hasSubNode () {if (this. leftNode! = Null | this. rightNode! = Null) {return true;} else {return false ;}} public AVLNode () {}public AVLNode (int value) {this. value = value ;}}

 

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.