您的位置:首页 > 其它

二叉树的相关算法

2016-04-06 18:14 344 查看
/**
* 具体实现部分
* @param node
*/
//输出二叉树的所有叶子节点
public void yezi(Node<T> node)
{
if(node==null) return;
else
{
yezi(node.lChild);
yezi(node.rChild);
}
if (node.lChild==null&&node.rChild==null) {
System.out.print("叶子:"+node.getData());
}
}
//统计二叉树所有节点的总数
public int count(Node<T> node)
{
if(node==null) return count;
else
{
count++;
count(node.lChild);
count(node.rChild);
}
return count;
}

//在当前二叉树中查找数据x
public Node<T> search(Node<T> node,T x){
if(node==null)
return null;
else{
if((node.getData()).equals(x)){
return node;
}
else{
Node<T> s=search(node.lChild,x);
if(s!=null)//预防右孩子执行不了
return s;
else
return search(node.rChild,x);
}
}
}

//查找node的parent
public Node<T> search2(Node<T> node,T x){
if(node==null)
return null;
else{
if((node.getData()).equals(x)){
return node;
}
else{
Node<T> s=search2(node.lChild,x);
if(s!=null&&s.getData().equals(x))//预防右孩子执行不了
{
a[b]=node;
b++;
}

else{
s= search2(node.rChild,x);
if(s!=null&&s.getData().equals(x))
{
a[b] = node;
b++;
}
}
}
}
return (Node<T>) a[0];
}

//判断node是parent的左孩子还是右孩子
public String search3(Node<T> node,T x){
if(node==null)
return null;
else{
if((node.getData()).equals(x)){
return "Yes";
}
else{
String s=search3(node.lChild,x);
if(s!=null&&s.equals("Yes"))//预防右孩子执行不了
{
lr[c] = "left";
c++;
}

else
{
s=search3(node.rChild,x);
if(s!=null&&s.equals("Yes"))
{
lr[c] = "right";
c++;
}

}
}
}
return (String) lr[0];
}
//插入操作
public boolean insert(Node<T> a, Node<T> parent)
{
if(parent==null)	return false;
//Node<T> p= new Node<T>(x);
if(parent.lChild==null) parent.lChild = a;
else
{
insert(a,parent.lChild);
}
return true;
}
//插入操作
public boolean insertl(Node<T> a, Node<T> parent)
{
if(parent==null)	return false;
//Node<T> p= new Node<T>(x);
if(parent.lChild==null) parent.lChild = a;
else
{
insertl(a,parent.lChild);
}
return true;
}
//插入操作
public boolean insertr(Node<T> a, Node<T> parent)
{
if(parent==null)	return false;
//Node<T> p= new Node<T>(x);
if(parent.rChild==null) parent.rChild = a;
else
{
insertr(a,parent.lChild);
}
return true;
}
//缩进打印
public void suojin()
{
Node<T>[] queue= new Node[this.maxNodes];
int frontL,rearL;
if (this.root==null) return;
frontL=-1;
rearL=0;
queue[rearL]=this.root.lChild;
System.out.print("左分支:");
while(frontL!=rearL)
{
frontL++;
System.out.print(queue[frontL].getData());
if (queue[frontL].lChild!=null)
{
rearL++;
queue[rearL]=queue[frontL].lChild;
}
if (queue[frontL].rChild!=null)
{
rearL++;
queue[rearL]=queue[frontL].rChild;
}
}
System.out.print("  右分枝:");
int frontR,rearR;
if (this.root==null) return;
frontR=-1;
rearR=0;
queue[rearR]=this.root.rChild;
while(frontR!=rearR)
{
frontR++;
System.out.print(queue[frontR].getData());
if (queue[frontR].lChild!=null)
{
rearR++;
queue[rearR]=queue[frontR].lChild;
}
if (queue[frontR].rChild!=null)
{
rearR++;
queue[rearR]=queue[frontR].rChild;
}
}
}
/**主函数部分
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] aa= new int[]{1,2,3,4};
int[] bb= new int[]{5,6,7,8};
BinaryTree<Integer> tree = new BinaryTree<>(0);
Node<Integer> node = tree.root;
for (int i = 0; i < aa.length; i++) {
tree.insertLeft(aa[i], node);
tree.insertRight(bb[i], node);
}
System.out.println("原始二叉树:");
tree.traversal(0);

//2.输出二叉树的所有叶子节点
System.out.println("\n二叉树的所有叶子节点:");
tree.yezi(tree.root);

//3.统计节点数量
System.out.println("\n节点的数量为:"+tree.count(tree.root));

//4.交换二叉树任意两个节点(假设二叉树的每个节点的值都不一样)
Node<Integer> node1 = tree.search(tree.root, 3);
Node<Integer> node2 = tree.search(tree.root, 7);
int temp = node1.getData();
node1.setData(node2.getData());
node2.setData(temp);
System.out.print("节点3和节点7交换后:");
tree.traversal(0);
System.out.println("(先序遍历)");

//5.将现有二叉树的某个子树a移到其他子树b中
Node<Integer> a = tree.search(tree.root, 2);
Node<Integer> b = tree.search(tree.root, 3);

//5.1查找a的双亲
Node<Integer> parentOfa=tree.search2(tree.root,a.getData());
//System.out.println("a的双亲是:"+parentOfa.getData());
//5.2判断a是parent的左节点还是右节点
String s= tree.search3(tree.root,a.getData());
System.out.println("a在双亲的:"+s);
System.out.println();
//5.3插入操作将a插到b中
tree.insert(a, b);
//5.4将子树a置空
if (s.equals("left")) {
parentOfa.lChild=null;
}else if (s.equals("right")){
parentOfa.rChild=null;
}
//5.5遍历一下,检验是否正确
System.out.println("将a移到b以后:");
tree.traversal(0);
System.out.println("(先序)");
tree.traversal(1);
System.out.println("(中序)");

//6.删除一个节点
//6.1找到这个节点
Node<Integer> delete = tree.search(tree.root, 3);
//6.2找到这个节点的双亲节点
tree.b = 0;
Node<Integer> parentOfdelete=tree.search2(tree.root,delete.getData());
//System.out.println("delete的双亲是:"+parentOfdelete.getData());
//6.3判断这个节点是双亲的左孩子还是右
tree.c = 0;
//6.4判断delete是双亲节点的左孩子还是右孩子
s= tree.search3(tree.root,delete.getData());
if (s.equals("left")) {
parentOfdelete.lChild=null;
}
if (s.equals("right")){
parentOfdelete.rChild=null;
}
//6.5找到节点的左孩子和右孩子
Node<Integer> left = delete.lChild;
Node<Integer> right = delete.rChild;
//6.6插入操作
if (left != null) {
tree.insertl(left,parentOfdelete);
}
if (right != null) {
tree.insertr( right,parentOfdelete);
}
//6.7遍历一下,检验是否正确
System.out.println("删除节点以后:");
tree.traversal(0);
System.out.println("(先序)");
tree.traversal(1);
System.out.println("(中序)");

//7.缩进结构打印
System.out.println("缩进结构打印:");
tree.suojin();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: