Binary Tree

1.Node节点
package BinaryTree;

public class Node{
int data;
Node right;
Node left;

Node(int data) {
super();
this.data = data;
}

Node(int data, Node right, Node left) {
super();
this.data = data;
this.right = right;
this.left = left;
}
}


2.
package BinaryTree;


public class BinaryTree {

Node root;

/**
* 镜像,交换子树
* @param node
*/
public void swap(Node node){
if(node == null)
return;
if(node.left == null && node.right == null)
return;

Node tmp = node.left;
node.left = node.right;
node.right = tmp;

swap(node.left);
swap(node.right);

}
/**
* 插入节点
* @param node
* @param data
* @return
*/
public Node insert(Node node,int data){
if(node == null){
node = new Node(data);
}else{
if(data <= node.data)
insert(node.left,data);
else
insert(node.right,data);
}

return node;

}
/**
* 遍历二叉树,中序
* @param node
*/
public void printNode(Node node){
if(node == null)
return;

printNode(node.left);
System.out.println(node.data);
printNode(node.right);
}
/**
* 二叉树元素个数
* @param node
* @return
*/
public int size(Node node){
if(node == null)
return 0;

return size(node.left)+1+size(node.right);

}
/**
* 二叉树深度
* @param node
* @return
*/
public int depth(Node node){
if(node == null)
return 0;

int childdepth = depth(node.left)>depth(node.right)?depth(node.left):depth(node.right);

return 1+childdepth;

}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值