二叉树的创建和四种遍历(前序、先序、后序、层次、结点的层数、深度、叶子数等)—java描述
packagejavab;
//树的结点类
publicclassTreeNode {
Stringdata;
TreeNodeleftChild,rightChild,next;
publicTreeNode(String data){
this.data=data;
}
publicTreeNode(String data,TreeNode left,TreeNode right){
leftChild=left;
rightChild=right;
}
publicString getdata(){
returndata;
}
publicvoidsetleftChild(TreeNode left){
leftChild=left;
}
publicvoidsetrightChild(TreeNode right){
rightChild=right;
}
}
packagejavab;
//二叉树结构
publicclassBinaryTree {
TreeNoderoot;
publicBinaryTree(String data){
setTree(data);
}
publicBinaryTree(String data,BinaryTree left,BinaryTree right){
setTree(data,left,right);
}
voidsetTree(String data){
root=newTreeNode(data);
}
voidsetTree(String data,BinaryTree left,BinaryTreeright){
root=newTreeNode(data);
if(left!=null){
root.setleftChild(left.root);
}
if(right!=null){
root.setrightChild(right.root);
}
}
}
packagejavab;
//遍历
publicclassTree {
staticBinaryTree create(){
BinaryTree tree7=newBinaryTree("7");
BinaryTree tree8=newBinaryTree("8");
BinaryTree tree9=newBinaryTree("9");
BinaryTree tree10=newBinaryTree("10");
BinaryTree tree11=newBinaryTree("11");
BinaryTree tree12=newBinaryTree("12");
BinaryTree tree4=newBinaryTree("4",tree8,tree9);
BinaryTree tree5=newBinaryTree("5",tree10,tree11);
BinaryTree tree6=newBinaryTree("6",tree12,null);
BinaryTree tree3=newBinaryTree("3",tree6,tree7);
BinaryTree tree2=newBinaryTree("2",tree4,tree5);
BinaryTree tree1=newBinaryTree("1",tree2,tree3);
returntree1;
}
staticvoidpreorder(TreeNode t){
if(t!=null){
System.out.print(t.data+" ");
preorder(t.leftChild);
preorder(t.rightChild);
}
}
staticvoidinorder(TreeNode t){
if(t!=null){
inorder(t.leftChild);
System.out.print(t.data+" ");
inorder(t.rightChild);
}
}
staticvoidpostorder(TreeNode t){
if(t!=null){
postorder(t.leftChild);
postorder(t.rightChild);
System.out.print(t.data+" ");
}
}
staticvoidlevelorder(TreeNode t){
TreeNode head=t;
TreeNode tail=head;
while(head.leftChild!=null&&head.rightChild!=null){
tail.next=head.leftChild;
tail=tail.next;
tail.next=head.rightChild;
tail=tail.next;
System.out.print(head.data+" ");
head=head.next;
}
while(head!=null){
System.out.print(head.data+" ");
head=head.next;
}
}
//二叉树的非递归先序遍历
staticvoidpreorder2(TreeNode root){
TreeNode stack[]=newTreeNode[13];
inttop=0;
do{
while(root!=null){
System.out.print(root.data+" ");
top++;
stack[top]=root;
root=root.leftChild;
}
if(top!=0){
root=stack[top];
top--;
root=root.rightChild;
}
}while(top!=0||root!=null);
}
//求树的深度的递归算法
staticintdepth(TreeNode root){
intm,n;
if(root==null)return0;
else{
m=depth(root.leftChild);
n=depth(root.rightChild);
return(m>n?m:n)+1;
}
}
//求树中以值为x的节点为根的子树深度
staticvoidSubdepth(TreeNode root,String x){
if(root.data==x)
System.out.println(depth(root));
else{
if(root.leftChild!=null)
Subdepth(root.leftChild,x);
if(root.rightChild!=null)
Subdepth(root.rightChild,x);
}
}
//在二叉树root中求值为ch的结点所在的层数
staticintfloor(TreeNode root,String ch){
intlev,m,n;
if(root==null)
lev=0;
elseif(root.data==ch)
lev=1;
else{
m=floor(root.leftChild,ch);
n=floor(root.rightChild,ch);
if(m==0&&n==0) lev=0;
elselev=((m>n)?m:n)+1;
}
return(lev);
}
//求树的叶子数
staticintcountleaf(TreeNode root){
inti;
if(root==null)
i=0;
elseif((root.leftChild==null)&&(root.rightChild==null))
i=1;
elsei=countleaf(root.leftChild)+countleaf(root.rightChild);
return(i);
}
publicstaticvoidmain(String[] args) {
//TODOAuto-generated method stub
BinaryTree tree1=create();
System.out.println("对从1到12按层次顺序建立的二叉树先序遍历的结果为:");
preorder(tree1.root);
System.out.println();
System.out.println("二叉树的非递归先序遍历的结果为:");
preorder2(tree1.root);
System.out.println();
System.out.println("中序遍历的结果为:");
inorder(tree1.root);
System.out.println();
System.out.println("后序遍历的结果为:");
postorder(tree1.root);
System.out.println();
System.out.println("层次遍历的结果为:");
levelorder(tree1.root);
System.out.println();
System.out.println("该树的深度为:"+depth(tree1.root));
System.out.println("值为3的结点为根的子树的深度为:");
Subdepth(tree1.root,"3");
System.out.println("值为3的结点所在的层数为:"+floor(tree1.root,"3"));
System.out.println("此树的叶子数为:"+countleaf(tree1.root));
}
}