二叉树 常用函数 小结

实现功能如下
insertLeft 左插
insertRight 右插
delLeft 左删
delRight 右删
preorder 前序遍历
inorder 中序遍历
postorder 后序遍历
levelOrder 层序遍历
traversal 遍历汇总
getHeight 树高
size 树的结点个数
movetree 将a树移到b树的左侧
preCreate 人机交互创建树
reverse 将树的结点翻转

BitreeNode 类代码如下:

package tree;

class BiTreeNode {
	public BiTreeNode lchild, rchild;
	private Object data;

	public Object getData() {
		return data;
	}

	@Override
	public String toString() {
		return "BiTreeNode [data=" + data + "]";
	}

	public void setData(Object data) {
		this.data = data;
	}

	public BiTreeNode(Object data) {
		this.data=data;	
	}

	public BiTreeNode() {
		lchild=rchild=null;
		data=null;
	}

	public BiTreeNode(BiTreeNode lchild, BiTreeNode rchild, Object data) {
		this.lchild = lchild;
		this.rchild = rchild;
		this.data = data;
	}
	
}

Bitree类 代码如下

package tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

import java.util.Scanner;

public class BiTree {
	public BiTreeNode root;
	Scanner in=new Scanner(System.in);
	
	public BiTree() {
		root = new BiTreeNode();
		
	}

	public BiTree(Object x) {
		root = new BiTreeNode(x);
	}

	public boolean insertLeft(Object x, BiTreeNode parent) {
		if (parent == null) {
			return false;
		}
		BiTreeNode p = new BiTreeNode(x);
		if (parent.lchild != null) {
			p.lchild = parent.lchild;
		}
		parent.lchild = p;
		return true;
	}

	public boolean insertRight(Object x, BiTreeNode parent) {
		if (parent == null) {
			return false;
		}
		BiTreeNode p = new BiTreeNode(x);
		if (parent.rchild != null) {
			p.rchild = parent.lchild;
		}
		parent.rchild = p;
		return true;
	}

	public boolean delLeft(BiTreeNode parent) {
		if (parent == null) {
			return false;
		}
		parent.lchild = null;
		return true;
	}

	public boolean delRight(BiTreeNode parent) {
		if (parent == null) {
			return false;
		}
		parent.rchild = null;
		return true;
	}

	public int preorder() {
		return preorder(root);
	}

	public int preorder(BiTreeNode p) {
		if (p == null) {
			return 0;
		}
		System.out.print(p.getData() + " ");
		preorder(p.lchild);
		preorder(p.rchild);
		return 1;
	}


	public void preorder2() {
		Stack<BiTreeNode> myStack = new Stack<BiTreeNode>();
		BiTreeNode p = root;
		if (p != null) {
			myStack.push(p);
			while (!myStack.isEmpty()) {
				p = myStack.pop();
				System.out.print(p.getData() + " ");
				if (p.rchild != null) {
					myStack.push(p.rchild);
				}
				if (p.lchild != null) {
					myStack.push(p.lchild);
				}
			}
		}

	}

	public LinkedList<Integer> preorder3() {
		BiTreeNode p = root;
		Stack<BiTreeNode> myStack = new Stack<BiTreeNode>();
		LinkedList<Integer> list = new LinkedList<Integer>();
		while (p != null || !myStack.isEmpty()) {
			if (p != null) {
				myStack.push(p);
				list.addLast((int) p.getData());
				p = p.lchild;
			} else {
				p = myStack.pop();
				p = p.rchild;
			}
		}
		return list;
	}

	public int inorder() {
		return inorder(root);
	}

	public int inorder(BiTreeNode p) {
		if (p == null) {
			return 0;
		}
		inorder(p.lchild);
		System.out.print(p.getData() + " ");
		inorder(p.rchild);
		return 1;
	}


	public LinkedList<Integer> inorder2() {
		BiTreeNode p = root;
		Stack<BiTreeNode> myStack = new Stack<BiTreeNode>();
		LinkedList<Integer> list = new LinkedList<Integer>();
		while (p != null || !myStack.isEmpty()) {
			if (p != null) {
				myStack.push(p);
				p = p.lchild;
			} else {
				p = myStack.pop();
				list.addLast((int) p.getData());
				p = p.rchild;
			}
		}
		System.out.println(list.toString());
		return list;
	}

	public int postorder() {
		return postorder(root);
	}

	public int postorder(BiTreeNode p) {
		if (p == null) {
			return 0;
		}
		postorder(p.lchild);
		postorder(p.rchild);
		System.out.print(p.getData() + " ");
		return 1;
	}

	public LinkedList<Integer> postorder2() {
		Stack<BiTreeNode> myStack = new Stack<BiTreeNode>();
		BiTreeNode p = root;
		LinkedList<Integer> list = new LinkedList<Integer>();
		while (p != null || !myStack.isEmpty()) {
			if (p != null) {
				myStack.push(p);
				list.addFirst((int) p.getData());
				p = p.rchild;
			} else {
				p = myStack.pop();
				p = p.lchild;
			}
		}
		return list;

	}
	public void levelOrder() {
		levelOrder(root);
	}
	public void levelOrder(BiTreeNode p) {
		
		Queue<BiTreeNode> queue = new LinkedList<BiTreeNode>();
		queue.add(p);
		while(!queue.isEmpty()) {
			int n=queue.size();
			for(int i=0;i<n;i++) {
				BiTreeNode temp=queue.poll();
				System.out.print((int)temp.getData()+" ");
				if(temp.lchild!=null) {
					queue.add(temp.lchild);
				}
				if(temp.rchild!=null) {
					queue.add(temp.rchild);
				}	
			}
				
		}
		
	}

	public void traversal(int i) {
		switch (i) {
		case 1:
			preorder();
			System.out.println();
			break;
		case 2:
			inorder();
			System.out.println();
			break;
		case 3:
			postorder();
			System.out.println();
			break;
		case 4:
			levelOrder();
			System.out.println();
		default:
			break;
		}
	}

	public int getHeight() {
		return getHeight(root);
	}

	public int getHeight(BiTreeNode p) {
		if (p == null) {
			return 0;
		}
		return getHeight(p.lchild) > getHeight(p.rchild) ? getHeight(p.lchild) + 1 : getHeight(p.rchild) + 1;

	}

	public int size() {
		return size(root);
	}

	public int size(BiTreeNode p) {
		if (p == null) {
			return 0;
		}
		return 1 + size(p.lchild) + size(p.rchild);

	}
	
	public static void movetree(BiTreeNode a, BiTreeNode b) {
		if (a == null || b == null) {
			return;
		}

		while (b.lchild != null) {
			b = b.lchild;
		}
		b.lchild = a;

	}
	public static BiTreeNode movetree2(BiTreeNode a, BiTreeNode b) {
		if (a == null || b == null) {
			return null;
		}

		if(b.lchild==null) {
			b.lchild=a;
			return b;
		}
		b.lchild=movetree2(a, b.lchild);
		return b;
	}
	public void preCreate() {
		System.out.println("请输入根节点的值:");
		int x=in.nextInt();
		root=new BiTreeNode(x);
		preCreate(root, x);
	}
	public BiTreeNode preCreate(BiTreeNode p,int x) {
		System.out.println(x+"的左节点:");
		int l = in.nextInt();
		if(l!=-1) {
			p.lchild=new BiTreeNode(l);
			preCreate(p.lchild,l);
		}else {
			p.lchild=null;
		}
		System.out.println(x+"的右节点:");
		int r= in.nextInt();
		if(r!=-1) {
			p.rchild=new BiTreeNode(r);
			preCreate(p.rchild, r);
			
		}else {
			p.rchild=null;
		}
		return p;
	}
	public void preCreate2() {
		System.out.println("请输入根节点的值:");
		int x=in.nextInt();
		root=new BiTreeNode(x);
		preCreate2(root);
	}
	public void preCreate2(BiTreeNode p) {
		LinkedList<BiTreeNode> queue=new LinkedList<BiTreeNode>();
		queue.add(p);
		while(!queue.isEmpty()) {
			BiTreeNode temp=queue.poll();
			int x=(int)temp.getData();
			System.out.println(x+"的左结点:");
			int l = in.nextInt();
			if(l!=-1) {
				temp.lchild=new BiTreeNode(l);
				queue.add(temp.lchild);
			}else {
				temp.lchild=null;
			}
			System.out.println(x+"的右结点:");
			int r = in.nextInt();
			if(r!=-1) {
				temp.rchild=new BiTreeNode(r);
				queue.add(temp.rchild);
			}else {
				temp.rchild=null;
			}
			
		}
	}
	public BiTreeNode reverse(BiTreeNode p) {
		if(p==null) {
			return p;
		}else {
			BiTreeNode l=p.lchild;
			BiTreeNode r=p.rchild;
			p.lchild=r;
			p.rchild=l;
		}
		reverse(p.lchild);
		reverse(p.rchild);
		return p;
	}
	public void reverse2(BiTreeNode p) {
		Queue<BiTreeNode> queue=new LinkedList<BiTreeNode>();
		queue.add(p);
		while(!queue.isEmpty()) {
			BiTreeNode temp = queue.poll();
			BiTreeNode l=temp.lchild;
			BiTreeNode r=temp.rchild;
			temp.rchild=l;
			temp.lchild=r;
			if(temp.lchild!=null) {
				queue.add(temp.lchild);
				
			}
			if(temp.rchild!=null) {
				queue.add(temp.rchild);
			}
		}
	
	}
	
}

上一篇:数据结构难题


下一篇:UE4 Hello World 创建第一个UE4工程