创建一个二叉树的结点属性
class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
}
class BinarySortTree{
public void add(Node node){
if (root==null){
root=node;
}else {
root.add(node);
}
}
加入二叉树的结点
class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
public void add(Node no){
if (no==null){
return;
}
if (no.value<this.value){
if (this.left==null){
this.left=no;
}else {
this.left.add(no);
}
}else {
if (this.right==null){
this.right=no;
}else {
this.right.add(no);
}
}
}
}
class BinarySortTree{
public void add(Node node){
if (root==null){
root=node;
}else {
root.add(node);
}
public void add(Node node){
if (root==null){
root=node;
}else {
root.add(node);
}
}
}
中序排列二叉树
class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
public void add(Node no){
if (no==null){
return;
}
if (no.value<this.value){
if (this.left==null){
this.left=no;
}else {
this.left.add(no);
}
}else {
if (this.right==null){
this.right=no;
}else {
this.right.add(no);
}
}
}
public void infixorder(){
if (this.left!=null){
this.left.infixorder();
}
System.out.println(this);
if (this.right!=null){
this.right.infixorder();
}
}
}
class BinarySortTree{
private Node root;
public void add(Node node){
if (root==null){
root=node;
}else {
root.add(node);
}
public void add(Node node){
if (root==null){
root=node;
}else {
root.add(node);
}
}
public Node search(int value){
if (root==null){
return null;
}else {
return root.search(value);
}
}
public Node searchParent(int value){
if (root==null){
return null;
}else {
return root.searchParent(value);
}
}
}
删除二叉树的结点
1. 第一种情况
删除子节点
思路:
1,需求先去找到要删除的结点targetNode
2,找到targetNode的父节点pa'rent
3,确定targetNode是parent的左子节点还是右子节点
4,根据前面对应情况来删除
左子节点:parent.left=null
右子节点:parent.right=null
2,第二种情况:删除只有一颗子树的结点
1,需求先去找到要删除的结点targetNode
2,找到targetNode的父节点pa'rent
3,确定targetNode是parent的左子节点还是右子节点
4,如果targetNode是parent的左子节点
parent.left=target.left
5,如果targetNode是parent的右子节点
parentright=targetright
3,第三种情况:删除有两颗子树的结点
1,
1,需求先去找到要删除的结点targetNode
2,找到targetNode的父节点pa'rent
3,确定targetNode是parent的左子节点还是右子节点
4,用一个临时变量,将最小值的结点保存下来 temp
5,删除该结点
总代码
//创建二叉排序树
class BinarySortTree{
private Node root;
public Node search(int value){
if (root==null){
return null;
}else {
return root.search(value);
}
}
public Node searchParent(int value){
if (root==null){
return null;
}else {
return root.searchParent(value);
}
}
//编写方法
/**
*
* @param node 传入的结点(当作二叉排序树的跟结点)
* @return:放回的是以node为根节点的二叉排序树的最小值
*/
public int delRightTreeMin(Node node){
Node target=node;
//循环的查找左子节点,就会找到最小值
while (target.left!=null){
target=target.left;
}
//这时target就指向了最小值
delNode(target.value);
return target.value;
}
//删除结点
public void delNode(int value){
if (root==null){
return;
}else {
Node targetNode = search(value);
if (targetNode==null){
return;
}
//如果发现没有父节点
if (root.left==null && root.right==null){
root=null;
return;
}
//去查找父节点
Node node = searchParent(value);
//如果删除的结点是叶子节点
if (targetNode.left==null && targetNode.right==null){
//判断targetParent是父节点的左子节点还是右子节点
if (node.left!=null && node.left.value==value){
node.left=null;
}else if (node.right!=null && node.right.value==value) {
node.right = null;
}
}else if (targetNode.left!=null && targetNode.right!=null){//删除有两颗子树结点
int minvalue= delRightTreeMin(targetNode.right);
targetNode.value=minvalue;
}else {//删除只有一颗子树的结点
//如果要删除的是结点的左子节点
if (targetNode.left!=null){
//如果targentNode是parent的做左子节点
if (node.left.value==value){
node.left=targetNode.left;
}else {
node.right=targetNode.left;
}
}else {//如果要删除的结点有右子节点
//如果targetNode是parent的做左子节点
if (node.left.value==value){
node.left=targetNode.right;
}else { //如果targetNode是parent的做右子节点
node.right=targetNode.right;
}
}
}
}
}
public void add(Node node){
if (root==null){
root=node;
}else {
root.add(node);
}
}
//遍历
public void infixorder(){
if (root!=null){
root.infixorder();
}else {
System.out.println("当前二叉排序树为空");
}
}
}
//创建结点
class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
public Node search(int value){
if (value==this.value){
return this;
}else if (value<this.value){
if (this.left==null){
return null;
}
return this.left.search(value);
}else{
if (this.right==null){
return null;
}
return this.right.search(value);
}
}
//查找删除结点的父节点
public Node searchParent(int value){
if ((this.left!=null && this.left.value==value ) || (this.right!=null && this.right.value==value)){
return this;
}else {
//如果查找的值小于当前结点的值,并且当前结点的左子结点不为空
if (value<this.value && this.left!=null){
return this.left.searchParent(value);
}else if (value>=this.value && this.right!=null){
return this.right.searchParent(value);
}else {
return null;
}
}
}
public void add(Node no){
if (no==null){
return;
}
if (no.value<this.value){
if (this.left==null){
this.left=no;
}else {
this.left.add(no);
}
}else {
if (this.right==null){
this.right=no;
}else {
this.right.add(no);
}
}
}
public void infixorder(){
if (this.left!=null){
this.left.infixorder();
}
System.out.println(this);
if (this.right!=null){
this.right.infixorder();
}
}
@Override
public String toString() {
return "Node{value="+value+"}";
}
}