Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void delete(String name) {
- if(root==null) {return;}
- reader=root;
- Node branch=null;
- int swish,path=0;
- while(true) {
- swish=name.compareTo(reader.name);
- if(swish>0) {
- if(reader.rightChild!=null) {
- branch=reader;
- path=1;
- reader=reader.rightChild;
- } else {
- return;
- }
- } else if(swish<0) {
- if(reader.leftChild!=null) {
- path=-1;
- branch=reader;
- reader=reader.leftChild;
- } else {
- return;
- }
- } else {
- //thing was found, delete it
- Node left=reader.leftChild;
- Node right=reader.rightChild;
- Node put=null;
- if(left==null&&right==null) {
- put=null;
- } else if(right==null) {
- put=left;
- } else if(left==null) {
- put=right;
- } else {
- //find inorder successor
- Node successor=right;
- Node presuccessor=reader;
- while(successor.leftChild!=null) {
- presuccessor=successor;
- successor=successor.leftChild;
- }
- successor.leftChild=reader.leftChild;
- presuccessor.leftChild=null;
- if(successor!=right) {
- Node place=successor;
- while(place.rightChild!=null) {
- place=place.rightChild;
- }
- place.rightChild=right;
- }
- put=successor;
- }
- if(path==1) {
- branch.rightChild=put;
- } else if(path==-1) {
- branch.leftChild=put;
- } else {
- root=put;
- }
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement