Binary tree data structure learning --Java delete nodes

Like a long time, is really bad think (Behind his hand)

Three cases to consider:

1, the node is a leaf node, no children

To delete the leaf nodes, only need to change the reference value of the node's parent node, the node will point to the reference to null it.

 

 

2, the node has a child node

Changing the parent node references, which point directly to the child node to remove the node.

 

 

3, the node has two child nodes

To delete a node has two child nodes, you need to use it in order to replace the successor node.

 

 Code

com.example.deer Package; 

public class Tree {
// root
public the Node the root;

/ **
* insertion node
* @param value
* /
public void INSERT (value Long, String sValue) {
// package node
Node newNode = new the node (value, sValue);
// reference current node
the node current = root;
// reference to a parent node
the node parent;
// if the root is null, i.e. when the first insert
IF (root == null) {
root = the newNode;
return;
} {the else
the while (to true) {
// parent node points to the current node
parent = current;
// if the current node pointed to data is larger than the insert, is left to go
IF (current.data> value) {
Current = current.leftChild;
IF (Current == null) {
parent.leftChild = the newNode;
return;
}
the else {}
Current = current.rightChild;
IF (Current == null) {
parent.rightChild = the newNode;
return;
}
}
}
}
}
/ **
* find nodes
* /
Find the Node public (Long value) {
// reference current node, the root node from
the Node Current = the root;
// cycle, as long as the lookup value is not equal to the current node's data item
the while (value = current.data!) {
// for comparison, and the comparison to find the size of the current node value
IF (current.data> value) {
current = current.leftChild;
} the else {
current = current.rightChild;
}
IF (current == null) {
return null;
}
}
return current ;
}

/ **
* delete nodes
* /
public Boolean delete (Long value) {
// reference current node, starting from the root
Current = root the Node;
// use parent of the current node
the Node parent = root;
// if the left node
boolean = isleftChild to true;

the while (current.data = value!) {
Parent = Current;
// compare, compare Find value and the current size of the node
IF (current.data> value) {
current = current.leftChild;
isleftChild = to true;
} the else {
current = current.rightChild;
isleftChild = to false;
}
IF (current == null) {
return to false;
}
}
// delete the leaf node, that is, the node has no children
if(current.leftChild == null && current.rightChild == null){
if(current == root){
root = null;
}else if(isleftChild){//如果它是父节点的左子节点
parent.leftChild = null;
}else{
parent.rightChild = null;
}
}else if (current.rightChild == null){
if(current == root){
root = current.leftChild;
}else if(isleftChild){
parent.leftChild = current.leftChild;
}else{
parent.rightChild = current.leftChild;
}
}else if (current.leftChild == null){
if(current == root){
root = current.rightChild;
}else if(isleftChild){
parent.leftChild = current.rightChild;
}else{
parent.rightChild = current.rightChild;
}
} else {
Node successor = getSuccessor(current);
if(current == root){
root = successor;
} else if(isleftChild) {
parent.leftChild = successor;
}else{
parent.rightChild = successor;
}
successor.leftChild = current.leftChild;
}
to true return;
}

/ **
* Find the relay node
* @param delNode
* @return
* /
public getSuccessor the Node (the Node delNode) {
the Node by successor = delNode;
the Node successorParent = delNode;
the Node Current = delNode.rightChild;
the while (Current! null =) {
successorParent = by successor;
by successor = Current;
Current = current.leftChild;
}
IF (= delNode.rightChild by successor) {!
//
successorParent.leftChild = successor.rightChild;
whole // right subtree of the node to delete linked carrier to the relay node right subtree
= delNode.rightChild successor.rightChild;
}
return by successor;
}
/ **
* preorder traversal
* /
public void frontOrder (the Node localNode) {
IF (localNode = null!) {
// root access
System.out.println (localNode .data + "," + localNode.sData) ;
pre-order traversing the left subtree //
frontOrder (localNode.leftChild);
preorder front right subtree //
frontOrder (localNode.rightChild);
}
}

/ **
* in sequence traversing
* /
public void inOrder (the Node localNode) {
IF (localNode! = null) {
inorder traversal // left subtree
inOrder (localNode.leftChild);
// root access
System.out.println (localNode.data + "," + localNode.sData);
// right subtree preorder
inOrder (localNode.rightChild);
}
}
/ * *
* postorder
* /
public void afterOrder (the Node localNode) {
IF (localNode = null!) {
preorder left subtree after //
afterOrder (localNode.leftChild);
post-order traversal // right subtree
afterOrder (localNode. rightChild);
// root access
System.out.println (localNode.data + "," + localNode.sData);
}
}
}

Guess you like

Origin www.cnblogs.com/xiaohualu/p/11815207.html