注:本程序针对三链二叉树,请参考二叉树结点类(有parent)
二叉树结点类,注:包含toArray()方法和toList()方法:
public class BinaryTreeNode {
private BinaryTreeNode leftChild;
private BinaryTreeNode rightChild;
private BinaryTreeNode parent;
private int value;
public BinaryTreeNode(){
}
public BinaryTreeNode( int value ){
this.value = value;
}
public BinaryTreeNode getLeftChild() {
return leftChild;
}
public void setLeftChild(BinaryTreeNode leftChild) {
this.leftChild = leftChild;
}
public BinaryTreeNode getRightChild() {
return rightChild;
}
public void setRightChild(BinaryTreeNode rightChild) {
this.rightChild = rightChild;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public BinaryTreeNode getParent() {
return parent;
}
public void setParent(BinaryTreeNode parent) {
this.parent = parent;
}
public LinkedList<BinaryTreeNode> toList(){
LinkedList<BinaryTreeNode> list = new LinkedList<BinaryTreeNode>();
toList( this , list );
return list;
}
private void toList( BinaryTreeNode root , List<BinaryTreeNode> list ){
if( root != null ){
list.add( root );
toList( root , list );
toList( root , list );
}
}
public int[] toArray(){
int[] array = new int[BinaryTree2Links.getSubNodeNum(this) + 1 ];
toArray( this , array , 0 );
return array;
}
private void toArray( BinaryTreeNode root , int[] array, int i ){
if( root != null ){
array[i++] = root.getValue();
toArray( root , array , i );
toArray( root , array , i );
}
}
}
二叉树操作类:
/**
* 三链结构二叉树常见操作(肯定不够完全,遇到再补充)
* 即左孩子,右孩子,值,父节点。
* 注:仅包含基础二叉树操作
*
* 特点:
* 区分左右,总结点数可以为0
* 二叉树第i层最多有2^(i-1)个结点
* 深度为k的二叉树最多有2^k-1个结点
* 如果右n个叶子结点,度为2的结点x个,则n=x+1
*
* @author CheN
*
*/
public class BinaryTree3Links {
/**
* 添加左孩子
* @param parent
* @param leftChild
* @return
*/
public static boolean addLeftChild( BinaryTreeNode parent , BinaryTreeNode leftChild ){
if( leftChild == null || parent == null ){
return false;
}else{
parent.setLeftChild(leftChild);
leftChild.setParent(parent);
return true;
}
}
/**
* 添加右孩子
* @param parent
* @param rightChild
* @return
*/
public static boolean addRightChild( BinaryTreeNode parent , BinaryTreeNode rightChild ){
if( rightChild == null || parent == null ){
return false;
}else{
parent.setRightChild(rightChild);
rightChild.setParent(parent);
return true;
}
}
/**
* 添加左右孩子
* @param parent
* @param leftChild
* @param rightChild
* @return
*/
public static boolean addChildren( BinaryTreeNode parent , BinaryTreeNode leftChild , BinaryTreeNode rightChild ){
if( parent == null ){
return false;
}else{
if( leftChild != null){
parent.setLeftChild(leftChild);
leftChild.setParent(parent);
}
if( rightChild != null){
parent.setRightChild(rightChild);
rightChild.setParent(parent);
}
return true;
}
}
/**
* 交换结点位置
* @param node1
* @param node2
* @return
*/
public static boolean exchange( BinaryTreeNode node1 , BinaryTreeNode node2 ){
BinaryTreeNode parent1 = node1.getParent();
BinaryTreeNode parent2 = node2.getParent();
BinaryTreeNode temp = new BinaryTreeNode();
//交换左孩子
temp.setLeftChild(node1.getLeftChild());
node1.setLeftChild(node2.getLeftChild());
node2.setLeftChild(temp.getLeftChild());
//交换右孩子
temp.setLeftChild(node1.getLeftChild());
node1.setLeftChild(node2.getLeftChild());
node2.setLeftChild(temp.getLeftChild());
//交换父结点
temp.setParent(node1.getParent());
node1.setParent(node2.getParent());
node2.setParent(temp.getParent());
//交换父节点的孩子
if (parent1 != null) {
if (parent1.getLeftChild() == node1) {
parent1.setLeftChild(node2);
} else {
parent1.setRightChild(node2);
}
}
if (parent2 != null) {
if (parent2.getLeftChild() == node2) {
parent2.setLeftChild(node1);
} else {
parent2.setRightChild(node1);
}
}
return true;
}
/**
* 删除结点
* @param node
* @return
*/
public static boolean delete( BinaryTreeNode node ){
BinaryTreeNode parent = node.getParent();
if( parent == null ){
return false;
}else{
if( parent.getLeftChild() == node ){
parent.setLeftChild(null);
}else{
parent.setRightChild(null);
}
}
return true;
}
/**
* 树的深度
* @param root
* @return
*/
public static int getDepth( BinaryTreeNode root ) {
int depth1 = 0 , depth2 = 0;
if( root == null ){
return 0;
}else{
depth1 = getDepth(root.getLeftChild());
depth2 = getDepth(root.getRightChild());
if( depth1 > depth2 ){
return depth1 + 1;
}else{
return depth2 + 1;
}
}
}
/**
* 结点的层
* @param node
* @return
*/
public static int getLevel( BinaryTreeNode node ) {
BinaryTreeNode parent = node.getParent();
if( parent == null ){
return 1;
}else{
return getLevel(parent) + 1 ;
}
}
/**
* 根结点
* @param node
* @return
*/
public static BinaryTreeNode getRoot( BinaryTreeNode node ) {
BinaryTreeNode parent = node.getParent();
if( parent == null ){
return node;
}else{
return getRoot(parent);
}
}
/**
* 获得树或子树的结点个数
* @param node
* @return
*/
public static int size( BinaryTreeNode node ) {
int num = 1;
if( node.getLeftChild() != null ){
num = num + getSubNodeNum(node.getLeftChild()) + 1;
}
if( node.getRightChild() != null ){
num = num + getSubNodeNum(node.getRightChild()) + 1;
}
return num;
}
/**
* 获得子孙结点个数
* @param node
* @return
*/
public static int getSubNodeNum( BinaryTreeNode node ) {
int num = 0;
if( node.getLeftChild() != null ){
num = num + getSubNodeNum(node.getLeftChild()) + 1;
}
if( node.getRightChild() != null ){
num = num + getSubNodeNum(node.getRightChild()) + 1;
}
return num;
}
/**
* 先序遍历:中左右
* @param root
*/
public static void preOrderTraverse( BinaryTreeNode root ) {
if( root != null ){
System.out.print( root.getValue()+" , " );
preOrderTraverse( root.getLeftChild() );
preOrderTraverse( root.getRightChild() );
}
}
/**
* 中序遍历:左中右
* @param root
*/
public static void inOrderTraverse( BinaryTreeNode root ) {
if( root != null ){
preOrderTraverse( root.getLeftChild() );
System.out.print( root.getValue()+" , " );
preOrderTraverse( root.getRightChild() );
}
}
/**
* 后序遍历:左右中
* @param root
*/
public static void postOrderTraverse( BinaryTreeNode root ) {
if( root != null ){
preOrderTraverse( root.getLeftChild() );
preOrderTraverse( root.getRightChild() );
System.out.print( root.getValue()+" , " );
}
}
/**
* 层序遍历
* @param root
*/
public static void levelOrderTraverse( BinaryTreeNode root ) {
Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();
queue.add( root );
while (!queue.isEmpty()) {
root = queue.poll();
System.out.print(root.getValue() + " , ");
if (root.getLeftChild() != null) {
queue.add(root.getLeftChild());
}
if (root.getRightChild() != null) {
queue.add(root.getRightChild());
}
}
}
/**
* 先序查找
* @param root
* @param target
* @return
*/
public static BinaryTreeNode preOrderSearch( BinaryTreeNode root , int target ) {
if( root == null )
return null;
if( root.getValue() == target )
return root;
BinaryTreeNode left = preOrderSearch( root.getLeftChild() , target);
if( left != null && left.getValue() == target )
return left;
BinaryTreeNode right = preOrderSearch( root.getRightChild() , target);
if( right != null && right.getValue() == target )
return right;
return null;
}
/**
* 中序查找
* @param root
* @param target
* @return
*/
public static BinaryTreeNode inOrderSearch( BinaryTreeNode root , int target ) {
if( root == null )
return null;
BinaryTreeNode left = preOrderSearch( root.getLeftChild() , target);
if( left != null && left.getValue() == target )
return left;
if( root.getValue() == target )
return root;
BinaryTreeNode right = preOrderSearch( root.getRightChild() , target);
if( right != null && right.getValue() == target )
return right;
return null;
}
/**
* 后序查找
* @param root
* @param target
* @return
*/
public static BinaryTreeNode postOrderSearch( BinaryTreeNode root , int target ) {
if( root == null )
return null;
BinaryTreeNode left = preOrderSearch( root.getLeftChild() , target);
if( left != null && left.getValue() == target )
return left;
BinaryTreeNode right = preOrderSearch( root.getRightChild() , target);
if( right != null && right.getValue() == target )
return right;
if( root.getValue() == target )
return root;
return null;
}
/**
* 层序查找
* @param root
* @param target
* @return
*/
public static BinaryTreeNode levelOrderSearch( BinaryTreeNode root , int target ) {
Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();
queue.add( root );
while (!queue.isEmpty()) {
root = queue.poll();
if( root.getValue() == target )
return root;
if (root.getLeftChild() != null) {
queue.add(root.getLeftChild());
}
if (root.getRightChild() != null) {
queue.add(root.getRightChild());
}
}
return null;
}
/**
* 是否是根
* @param root
* @return
*/
public static boolean isRoot( BinaryTreeNode root ) {
if( root.getParent() == null ){
return true;
}
return false;
}
/**
* 是否是叶子
* @param node
* @return
*/
public static boolean isLeaf( BinaryTreeNode node ) {
if( node.getLeftChild() == null && node.getRightChild() == null ){
return true;
}
return false;
}
/**
* 是否有左孩子
* @param node
* @return
*/
public static boolean hasLeftChild( BinaryTreeNode node ) {
if( node.getLeftChild() != null ){
return true;
}
return false;
}
/**
* 是否有右孩子
* @param node
* @return
*/
public static boolean hasRightChild( BinaryTreeNode node ) {
if( node.getRightChild() != null ){
return true;
}
return false;
}
/**
* 是否有孩子
* @param node
* @return
*/
public static boolean hasChild( BinaryTreeNode node ) {
if( node.getLeftChild() != null || node.getRightChild() != null ){
return true;
}
return false;
}
/**
* 是否有两个孩子
* @param node
* @return
*/
public static boolean hasTwoChilds( BinaryTreeNode node ) {
if( node.getRightChild() != null ){
return true;
}
return false;
}
/**
* 是否是孩子
* @param node
* @return
*/
public static boolean isChild( BinaryTreeNode node ) {
BinaryTreeNode parent = node.getParent();
if( parent == null ){
return false;
}else{
return true;
}
}
/**
* 是否是左孩子
* @param node
* @return
*/
public static boolean isLeftChild( BinaryTreeNode node ) {
BinaryTreeNode parent = node.getParent();
if( parent != null ){
if(parent.getLeftChild() == node){
return true;
}
}
return false;
}
/**
* 是否是右孩子
* @param node
* @return
*/
public static boolean isRightChild( BinaryTreeNode node ) {
BinaryTreeNode parent = node.getParent();
if( parent != null ){
if(parent.getRightChild() == node){
return true;
}
}
return false;
}
/**
* 是否是满二叉树(按照国内定义,即除最后一层外,每一层上的结点数都是最大结点数。)
*
* 总结点k:k = 2^h-1
* 树高h:h = log2 (k+1)
*
* @param root
* @return
*/
public static boolean isFullBinaryTreeForChina( BinaryTreeNode root ) {
int depth = getDepth( root );
int num = size( root );
if( num == Math.pow(2, depth) - 1 ){
return true;
}
return false;
}
/**
* 是否是满二叉树(按照国际定义,要么两个结点都有,要么没有结点)
* @param root
* @return
*/
public static boolean isFullBinaryTreeForInternational( BinaryTreeNode root ) {
BinaryTreeNode leftChild = root.getLeftChild();
BinaryTreeNode rightChild = root.getRightChild();
if( leftChild != null && rightChild != null ){
return isFullBinaryTreeForInternational(leftChild) && isFullBinaryTreeForInternational(rightChild);
}else if( leftChild == null && rightChild == null ){
return true;
}else{
return false;
}
}
/**
* 是否是完全二叉树(除最后一层外,每一层都是最大结点数,最后一层结点从左到右连续)
*
* 总结点k:2^(h-1) < k < 2^h-1
* 树高h:h = log2 k +1
*
* @param root
* @return
*/
public static boolean isCompleteBinaryTree(BinaryTreeNode root) {
Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();
boolean last = false; // 判断当前是否已经找到了第一个不含有两个子节点的节点。
queue.add( root );
while ( !queue.isEmpty() ) {
root = queue.poll();
if( !last ) { // 前面所有的节点都含有两个子节点
if (root.getLeftChild() != null && root.getRightChild() != null) { // 当前节点也含有两个子节点
queue.add(root.getLeftChild());
queue.add(root.getRightChild());
}else if(root.getLeftChild() != null && root.getRightChild() == null) { // 当前节点只含有左子节点,则在其后面的所有节点都必须是叶子节点
last = true;
queue.add(root.getLeftChild());
}else if(root.getLeftChild() == null && root.getRightChild() != null) { // 当前节点只含有右子节点,该树不是完全的。
return false;
}else{ // 当前节点不含有任何的子节点。
last = true;
}
} else {// 已经找到了第一个不含有两个子节点的节点,当前扫描的节点必须是叶子节点。
if (root.getLeftChild() != null || root.getRightChild() != null) {
return false;
}
}
}
return true;
}
/********************************************************************************
* 二叉排序树
* 定义:左子树若不为空,左子树均小于根节点;若右子树不为空,右子树均大于根节点。左右子树分别为二叉排序树。
* 特点:
* 复杂度为:O(logn)
* 最坏情况:O(n)
* 由此引申出来的概念:平衡二叉树、红黑树
********************************************************************************/
/**
* 将数组转化为二叉排序树
* @param array
*/
public static void buildBinarySortingTree( int[] array ){
BinaryTreeNode root = new BinaryTreeNode();
root.setValue(array[0]);
for( int i = 1 ; i < array.length ; i++ ){
addBinarySortingTreeNode( root , array[i] );
}
}
/**
* 将二叉树转化为二叉排序树简单二叉排序树
* @param root
*/
public static void buildBinarySortingTree( BinaryTreeNode root ){
List<BinaryTreeNode> list = root.toList();
for(int i = 0 ; i < list.size() ; i++ ){
if( list.get(i) == root )
continue;
addBinarySortingTreeNode( root , list.get(i).getValue() );
}
}
/**
* 将数组转化为二叉排序树,注:array中不要包含root已赋值给root的值,否则会多出一个值
* @param root
* @param array
*/
public static void buildBinarySortingTree( BinaryTreeNode root , int[] array ){
for(int i = 0 ; i < array.length ; i++ ){
addBinarySortingTreeNode( root , array[i] );
}
}
/**
* 将二叉树转化为二叉排序树简单二叉排序树:newRoot须为原树中的某一结点
* @param root
* @param newRoot
*/
public static void buildBinarySortingTree( BinaryTreeNode root , BinaryTreeNode newRoot ){
List<BinaryTreeNode> list = root.toList();
for(int i = 0 ; i < list.size() ; i++ ){
if( list.get(i) == newRoot )
continue;
addBinarySortingTreeNode( newRoot , list.get(i).getValue() );
}
}
/**
* 将指定的元素插入二叉排序树中
* @param root
* @param target
*/
private static void addBinarySortingTreeNode( BinaryTreeNode root , int target ){
int value = root.getValue();
if( target < value ){//插入右子树
if(root.getLeftChild()==null){
BinaryTreeNode node=new BinaryTreeNode(target);
root.setLeftChild(node);
}else{
addBinarySortingTreeNode( root.getLeftChild() , target );
}
}else if(target>value){
if(root.getRightChild()==null){
BinaryTreeNode node=new BinaryTreeNode(target);
root.setRightChild(node);
}else{
addBinarySortingTreeNode( root.getRightChild() , target );
}
}
}
/**
* 查找二叉排序树
* @param root
* @param target
*/
public static BinaryTreeNode BinarySerch( BinaryTreeNode root,int target ){
if( root == null ){
return null;
}else if(root.getValue()==target){
return root;
}else if(root.getValue()>target){
return BinarySerch(root.getLeftChild(),target);
}else{
return BinarySerch(root.getRightChild(),target);
}
}
/**
* 获取二叉排序树最小值(即最左侧结点)
* @param root
* @return
*/
public static BinaryTreeNode getMinFromBinarySortingTree( BinaryTreeNode root ){
if( root.getLeftChild() == null ){
return root;
}else{
return getMinFromBinarySortingTree( root.getLeftChild() );
}
}
/**
* 获取二叉排序树最大值(即最右侧结点)
* @param root
* @return
*/
public static BinaryTreeNode getMaxFromBinarySortingTree( BinaryTreeNode root ){
if( root.getRightChild() == null ){
return root;
}else{
return getMaxFromBinarySortingTree( root.getRightChild() );
}
}
/**
* 在二叉排序树上增加结点
* @param root
* @param node
* @return
*/
public static boolean addNodeToBinarySortingTree( BinaryTreeNode root , BinaryTreeNode node ){
if( root.getValue() > node.getValue() ){
if( root.getLeftChild() == null ){
addLeftChild(root, node);
return true;
}
return addNodeToBinarySortingTree( root.getLeftChild() , node );
}else{
if( root.getRightChild() == null ){
addRightChild(root, node);
return true;
}
return addNodeToBinarySortingTree( root.getRightChild() , node );
}
}
/**
* 在二叉排序树上删除节点
* @param node
* @return
*/
public static boolean deleteNodeFromBinarySortingTree( BinaryTreeNode node ){
BinaryTreeNode parent = node.getParent();
BinaryTreeNode rightChild = node.getRightChild();
BinaryTreeNode leftChild = node.getLeftChild();
if( isLeaf(node) ){//如果是叶子结点,则直接删了
if( isLeftChild(node))
parent.setLeftChild(null);
else
parent.setRightChild(null);
node.setParent(null);
return true;
}else{
if( leftChild == null ){//如果只有右结点,则将右结点与父结点相连
if( isLeftChild(node)){
parent.setLeftChild(rightChild);
}else{
parent.setLeftChild(leftChild);
}
rightChild.setParent(parent);
node.setRightChild(null);
node.setParent(null);
return true;
}else if(rightChild == null ){//如果只有左结点,则将左结点与父结点相连
if( isLeftChild(node)){
parent.setRightChild(rightChild);
}else{
parent.setRightChild(leftChild);
}
leftChild.setParent(parent);
node.setLeftChild(null);
node.setParent(null);
return true;
}else{//若左右子树都存在,则在左子树中找到最大的一个结点替代这个结点,若该节点有左子树,则将其左子树与其父结点连接
BinaryTreeNode temp = leftChild;
while( temp.getRightChild() != null ){
temp = temp.getRightChild();
}
if( temp.getLeftChild() != null ){
BinaryTreeNode tempParent = temp.getParent();
temp.getLeftChild().setParent(tempParent);
tempParent.setRightChild(temp.getLeftChild());
}
temp.setParent(parent);
temp.setLeftChild(leftChild);
temp.setRightChild(rightChild);
node.setParent(null);
node.setLeftChild(null);
node.setRightChild(null);
}
}
return false;
}
//森林转化为二叉树,简单叙述就是,横向串起来,然后左孩子右兄弟
}
二链结构二叉树常见操作(肯定不够完全,遇到再补充), 若有错误或不妥之处,敬请谅解并指点。