Balanced binary tree Various operations Java version __ Balanced binary Tree

Source: Internet
Author: User
Package avlbinarytree;


Import Java.util.Stack;


public class Avlsorttree<t extends Comparable<t>> {


private avlnode<t> root;


Public Avlsorttree () {
}
Public Avlsorttree (avlnode<t> root) {
This.root = root;
}


Public avlnode<t> Getroot () {
return root;
}

Rotate Left
private void Leftrotate (avlnode<t> node) {
if (node!=null) {
Avlnode<t> Rnode=node.getrightchild ();
Node.setrightchild (Rnode.getlchild ());
if (Rnode.getlchild ()!=null) {
Rnode.getlchild (). setparent (node);
}
Rnode.setparent (Node.getparent ());
if (Node.getparent () ==null) {
This.root=rnode;
}else if (Node.getparent (). Getlchild (). Equals (node)) {
Node.getparent (). Setlchild (Rnode);
}else{
Node.getparent (). Setrightchild (Rnode);
}
Rnode.setlchild (node);
Node.setparent (Rnode);
}
}
Rotate Right
private void Rightrotate (avlnode<t> node) {
if (node!=null) {
Avlnode<t> Lnode=node.getlchild ();
Node.setlchild (Lnode.getrightchild ());
if (Lnode.getrightchild ()!=null) {
Lnode.getrightchild (). setparent (node);
}
Lnode.setparent (Node.getparent ());
if (Node.getparent () ==null) {
This.root=lnode;
}else if (Node.getparent (). Getlchild () ==node) {
Node.getparent (). Setlchild (Lnode);
}else{
Node.getparent (). Setrightchild (Lnode);
}
Lnode.setrightchild (node);
Node.setparent (Lnode);
}
}

public boolean leftbalance (avlnode<t> node) {
Boolean highlower=true;
Avlnode<t> Lnode=node.getlchild ();
Switch (LNODE.GETBF ()) {
Case LH:
NODE.SETBF (EH);
LNODE.SETBF (EH);
This.rightrotate (node);
Break
Case RH:
Avlnode<t> Rd=lnode.getrightchild ();
Switch (RD.GETBF ()) {
Case EH:
NODE.SETBF (EH);
LNODE.SETBF (EH);
Break
Case LH:
LNODE.SETBF (EH);
NODE.SETBF (RH);
Break
Case RH:
NODE.SETBF (EH);
LNODE.SETBF (LH);
Break
}
RD.SETBF (EH);
This.leftrotate (Lnode);
This.rightrotate (node);
Break
Case EH:
LNODE.SETBF (RH);
NODE.SETBF (LH);
Highlower=false;
This.rightrotate (node);
Break
}
return highlower;
}

public boolean rightbalance (avlnode<t> node) {
Boolean heightlower=false;
Avlnode<t> Rnode=node.getrightchild ();
Switch (RNODE.GETBF ()) {
Case RH:
RNODE.SETBF (EH);
NODE.SETBF (EH);
This.leftrotate (node);
Break
Case LH:
Avlnode<t> Ld=rnode.getlchild ();
Switch (LD.GETBF ()) {
Case EH:
NODE.SETBF (EH);
RNODE.SETBF (EH);
Break
Case LH:
NODE.SETBF (EH);
RNODE.SETBF (RH);
Break
Case RH:
NODE.SETBF (LH);
RNODE.SETBF (EH);
Break
}
LD.SETBF (EH);
This.rightrotate (Rnode);
This.leftrotate (node);
Break
Case EH:
NODE.SETBF (RH);
RNODE.SETBF (LH);
Heightlower=false;
This.leftrotate (node);
Break
}
return heightlower;
}

private void Afterfixinsertion (avlnode<t> node) {
if (NODE.GETBF () ==2) {
Leftbalance (node);
}
if (NODE.GETBF () ==-2) {
Rightbalance (node);
}
}

public void Addavlnode (T data) {
Addavlnode (root,new avlnode (data,null));
}
Private avlnode<t> Addavlnode (avlnode<t> root,avlnode<t> Element) {
Avlnode<t> Node=root;
Avlnode<t> Parent=null;
while (Node!=null) {
Parent=node;
if (Element.getdata (). CompareTo (Node.getdata ()) <0) {
Node=node.getlchild ();
}else{
Node=node.getrightchild ();
}
}
Element.setparent (parent);
if (parent==null) {
This.root=element;
}else if (Element.getdata (). CompareTo (Parent.getdata ()) <0) {
Parent.setlchild (Element);
}else{
Parent.setrightchild (Element);
}
while (Parent!=null) {
if (Element.getdata (). CompareTo (Parent.getdata ()) <0) {
parent.bf++;
}else{
parent.bf--;
}
if (PARENT.GETBF () ==0) break;
if (Math.Abs (PARENT.GETBF ()) ==2) {
Afterfixinsertion (parent);
Break
}
Parent=parent.getparent ();
}
return root;
}


public void Prenode () {
Prenode (root);
}
private void Prenode (avlnode<t> root) {
if (root!=null) {
System.out.print (Root.getdata () + "");
Prenode (Root.getlchild ());
Prenode (Root.getrightchild ());
}
}

public void Nrinordertraverse () {
Nrinordertraverse (root);
}
private void Nrinordertraverse (avlnode<t> root) {
Stack<avlnode<t>> stack=new stack<avlnode<t>> ();
Avlnode<t> Node=root;
while (Node!=null | |!stack.isempty ()) {
while (Node!=null) {
System.out.print (Node.getdata () + "");
Stack.push (node);
Node=node.getlchild ();
}
Node=stack.pop ();
Node=node.getrightchild ();
}
}

Replace u tree with v tree
private void Transplant (avlnode<t> u,avlnode<t> v) {
if (U.getparent () ==null) {
This.root=v;
}else if (U.equals (U.getparent (). Getlchild ())) {
U.getparent (). Setlchild (v);
}else{
U.getparent (). Setrightchild (v);
}
if (v!=null) {
V.setparent (U.getparent ());
}
}
Public T Getminmum () {
return This.getminmum (Root). GetData ();
}
Find the current smallest node
Private Avlnode<t> getminmum (avlnode<t> root) {
Avlnode<t> Node=root;
while (Node.getlchild ()!=null) {
Node=node.getlchild ();
}
return node;
}

Public T Getmaxmum () {
return This.getmaxmum (Root). GetData ();
}
Find the current smallest node
Private Avlnode<t> getmaxmum (avlnode<t> root) {
Avlnode<t> Node=root;
while (Node.getrightchild ()!=null) {
Node=node.getrightchild ();
}
return node;
}

Public T Getsuccessordata (t data) {
Avlnode<t> node=this.getsuccessor (Root,data);
if (node!=null) {
return Node.getdata ();
}else{
return null;
}
}
Get a successor to a node
Private avlnode<t> getsuccessor (avlnode<t> root,t data) {
Avlnode<t> Element=this.getavlnode (root, data)//Get current node
if (element==null) return null;
if (Element.getrightchild ()!=null) {
Return Getminmum (Element.getrightchild ());
}
Avlnode<t> parent=element.getparent ();
while (Parent!=null && parent.getrightchild (). Equals (Element)) {
Element=parent;
Parent=parent.getparent ();
}
return to parent;
}

private void Deleteavlnode (avlnode<t> root,t data) {
Avlnode<t> P=null;
Avlnode<t> Current=this.getavlnode (root, data)//Get current node
if (current==null) return;
Avlnode<t> parent=current.getparent ()//Get the parent node of the current node
P=current;
if (Current.getlchild () ==null) {
This.transplant (Current,current.getrightchild ());
}else if (Current.getrightchild () ==null) {
This.transplant (Current, Current.getlchild ());
}else{
Avlnode<t> y=this.getsuccessor (Root,data);
if (!current.equals (Y.getparent ())) {
This.transplant (Y, Y.getrightchild ());
Y.setrightchild (Current.getrightchild ());
Y.getrightchild (). SetParent (y);
}
System.out.println (Current.getparent ());
This.transplant (current, y);
Y.setlchild (Current.getlchild ());
Y.getlchild (). SetParent (y);
}
Boolean h=true;
while (parent!=null&&h) {
if (P.equals (Parent.getlchild ())) {
parent.bf--;
}else{
parent.bf++;
}
if (Math.Abs (PARENT.GETBF ()) ==1) break;
Avlnode<t> r=parent;
if (PARENT.GETBF () ==2) {
H=this.leftbalance (R);
}else{
H=this.rightbalance (R);
}
P=parent;
Parent=parent.getparent ();
}
Current.setparent (NULL);
Current.setlchild (NULL);
Current.setrightchild (NULL);
Current=null;
}

public boolean Deleteavlnode (T data) {
Deleteavlnode (Root,data);
if (this.root!=null) {
return true;
}else{
return false;
}
}


Private avlnode<t> Getavlnode (avlnode<t> root,t data) {
Avlnode<t> Node=root;
while (Node!=null&&!node.getdata (). Equals (data)) {
if (Data.compareto (Node.getdata ()) <0) {
Node=node.getlchild ();
}else{
Node=node.getrightchild ();
}
}
if (node!=null) {
return node;
}else{
return null;
}
}



private static final int lh=1;
private static final int eh=0;
private static final int rh=-1;

Static class Avlnode<t extends Comparable<t>> implements comparable<avlnode<t>>{


Private T data;
Private avlnode<t> Parent,lchild,rightchild;
private int BF;

Public Avlnode () {}

Public Avlnode (T data,avlnode<t> parent) {
This (data,parent,null,null,0);
}
Public Avlnode (T data, avlnode<t> parent, avlnode<t> Lchild,
avlnode<t> rightchild, int bf) {
This.data = data;
This.parent = parent;
This.lchild = Lchild;
This.rightchild = Rightchild;
THIS.BF = BF;
}
@Override
public int hashcode () {
return Data.hashcode ();
}
@Override
public boolean equals (Object obj) {
if (this = obj)
return true;
if (obj = null)
return false;
if (GetClass ()!= obj.getclass ())
return false;
Avlnode other = (avlnode) obj;
if (data = = NULL) {
if (other.data!= null)
return false;
else if (!data.equals (Other.data))
return false;
return true;
}
Public T GetData () {
return data;
}
public void SetData (T data) {
This.data = data;
}
Public avlnode<t> GetParent () {
return to parent;
}
public void SetParent (avlnode<t> parent) {
This.parent = parent;
}
Public avlnode<t> Getlchild () {
return lchild;
}
public void Setlchild (avlnode<t> lchild) {
This.lchild = Lchild;
}
Public avlnode<t> Getrightchild () {
return rightchild;
}
public void Setrightchild (avlnode<t> rightchild) {
This.rightchild = Rightchild;
}
public int GETBF () {
return BF;
}
public void SETBF (int bf) {
THIS.BF = BF;
}
@Override
public int compareTo (avlnode<t> o) {
Return This.data.compareTo (O.getdata ());
}
}

public static void Main (string[] args) {
Avlsorttree<integer> a=new avlsorttree<integer> ();
A.addavlnode (1);
A.addavlnode (2);
A.addavlnode (3);
A.addavlnode (4);
A.addavlnode (5);
A.addavlnode (6);
A.prenode ();
System.out.println ();
A.nrinordertraverse ();
System.out.println (A.deleteavlnode (6));
System.out.println ();
A.prenode ();
System.out.println (A.getminmum ());
System.out.println (A.getmaxmum ());
System.out.println (A.getsuccessordata (5));
A.deleteavlnode (4);
A.prenode ();



}



}

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.