package demo2;
public class BinaryTree {
TreeNode root=null;
int j=0;
int arr[]=new int[100];
public TreeNode getRoot() {
return root;
}
//普通插入
public void insert(Integer value) {
//判空
TreeNode newTreeNode=new TreeNode(value);
//判断root是否非空
if(root==null) {
root=newTreeNode;
return;
}
TreeNode tempNode=root;
while(true) {
TreeNode parentNode=tempNode;
if(tempNode.getValue()<value) {
tempNode=tempNode.getRightTreeNode();
if(tempNode==null) {
parentNode.setRightTreeNode(newTreeNode);
return;
}
}else {
tempNode=tempNode.getLeftTreeNode();
if(tempNode==null) {
parentNode.setLeftTreeNode(newTreeNode);
return;
}
}
}
}
//递归
public TreeNode digiu(Integer value,TreeNode t1) {
TreeNode newTreeNode=new TreeNode(value);
if(root==null) {
root=newTreeNode;
return root;
}
if(t1.getValue()>value) {
if(t1.getLeftTreeNode()==null) {
t1.setLeftTreeNode(newTreeNode);
return root;
}
return digiu(value,t1.getLeftTreeNode());
}else{
if(t1.getRightTreeNode()==null) {
t1.setRightTreeNode(newTreeNode);
return root;
}
return digiu(value,t1.getRightTreeNode());
}
}
//计算二叉树长度
public int len(TreeNode t1) {
if(t1==null) {
return j;
}else {
if(t1.getLeftTreeNode()!=null&&t1.getRightTreeNode()==null) {
j++;
return len(t1.getLeftTreeNode());
}else if(t1.getLeftTreeNode()==null&&t1.getRightTreeNode()!=null) {
j++;
return len(t1.getRightTreeNode());
}else {
j++;
len(t1.getLeftTreeNode());
len(t1.getRightTreeNode());
}
}
return j;
}
//层次遍历
public void levelOrder(){
TreeNode treeNode = root;
//定义好队列
ArrayQueueDemo queueDemo = new ArrayQueueDemo();
queueDemo.insert(treeNode.getValue());
while (queueDemo.isEmpty()){
Integer val = queueDemo.remove();
System.out.println(val);
treeNode = find(val);
if(treeNode.getLeftTreeNode() !=null){
queueDemo.insert(treeNode.getLeftTreeNode().getValue());
}
if(treeNode.getRightTreeNode() !=null){
queueDemo.insert(treeNode.getRightTreeNode().getValue());
}
}
}
//遍历
//(先序)
public void bianli(TreeNode a) {
if(a==null) {
return;
}else {
System.out.print(a.getValue());
bianli(a.getLeftTreeNode());
bianli(a.getRightTreeNode());
}
}
//找到删除节点的父节点
public TreeNode serchParent(TreeNode node,Integer value) {
if(root==null) {
return null;
}
//判断当前节点是否是我们要删除的节点
if((node.getLeftTreeNode()!=null&&node.getLeftTreeNode().getValue()==value)||(node.getRightTreeNode()!=null&&node.getRightTreeNode().getValue()==value)) {
return node;
}else {
//如果当前节点的值小于我们要查的值
if(node.getValue()<value&&node.getRightTreeNode()!=null) {
return serchParent(node.getRightTreeNode(), value);
}else if(node.getValue()>value&&node.getLeftTreeNode()!=null) {
return serchParent(node.getLeftTreeNode(),value);
}else {
return null;//没有父节点
}
}
}
//获取删除该节点的右子树的最小值
public int min(TreeNode a1) {
TreeNode target = a1;
while(a1.getLeftTreeNode()!=null) {
a1=a1.getLeftTreeNode();
}
delete(root,target.getValue());
return a1.getValue();
}
//删除子节点
public void delete(TreeNode node,Integer val) {
if(root==null) {
return;
}
//找到要删除的节点
TreeNode del=find(val);
//没有找到del(删除)节点
if(del==null) {
return;
}
//找到删除的父节点
TreeNode fu=serchParent(root,val);
//如果我们要删除节点的是叶子节点
if(del.getLeftTreeNode()==null&&del.getRightTreeNode()==null) {
//判断del是fu节点的左子树还是右子树
if(fu.getLeftTreeNode()!=null&&fu.getLeftTreeNode().getValue()==val) {
fu.setLeftTreeNode(null);
System.out.println("删除成功");
}else if(fu.getRightTreeNode()!=null&&fu.getRightTreeNode().getValue()==val){
fu.setRightTreeNode(null);
System.out.println("删除成功");
}
}else if(del.getLeftTreeNode()!=null&&del.getRightTreeNode()!=null) {
int min =min(del.getRightTreeNode());
del.setValue(min);
System.out.println("删除成功");
}else {
if(del.getValue()>fu.getValue()) {
if(del.getLeftTreeNode()==null&&del.getRightTreeNode()!=null) {
fu.setRightTreeNode(del.getRightTreeNode());
}else {
fu.setRightTreeNode(del.getLeftTreeNode());
}
}else {
if(del.getLeftTreeNode()==null&&del.getRightTreeNode()!=null) {
fu.setLeftTreeNode(del.getRightTreeNode());
}else {
fu.setLeftTreeNode(del.getLeftTreeNode());
}
}
}
}
//查找节点
public TreeNode find(int value){
if (root == null){
return null;
}
//定义一个指针
TreeNode tempNode = root;
while (tempNode.getValue()!=value){
if(value <tempNode.getValue()){
tempNode = tempNode.getLeftTreeNode();
}else {
tempNode = tempNode.getRightTreeNode();
}
if(tempNode == null){
return null;
}
}
return tempNode;
}
//查找值
public Integer Search(Integer value) {
TreeNode a1;
a1=root;
if(a1==null) {
return null;
}else {
while(a1!=null) {
if(value>a1.getValue()) {
a1=a1.getRightTreeNode();
}else if(value<a1.getValue()) {
a1=a1.getLeftTreeNode();
}else {
return value;
}
}
return null;
}
}
//中序
public void bianli1(TreeNode a) {
if(a!=null) {
bianli1(a.getLeftTreeNode());
System.out.println(a.getValue());
bianli1(a.getRightTreeNode());
}
}
//后序
public void bianli2(TreeNode a) {
if(a==null) {
return;
}else {
bianli2(a.getLeftTreeNode());
bianli2(a.getRightTreeNode());
System.out.println(a.getValue());
}
}
}