[aligncenter]先序是个什么概念?二叉树又是个什么概念?怎么用先序来创建二叉树???[align] [align=center] 先序是个什么概念? 二叉树又是个什么概念? 怎么用先序来创建二叉树???
           
        
        [aligncenter]先序是个什么概念?二叉树又是个什么概念?怎么用先序来创建二叉树???[align]                                                                                         [align=center] 先序是个什么概念? 二叉树又是个什么概念?  怎么用先序来创建二叉树???[/align]                                                                           
9 个解决方案
                                                            #1
              数据结构里面的哈!                                                                
#2
              汗,楼主这个题问的强悍!光是二叉树就可以跟你说上个大半天的了,你说在帖子里怎么回?                                                                
#3
              递归方法前序遍历  publicclassBinaryTree{//二叉树类   publicBinaryNoderoot;//根结点  publicEdata;//数据元素  publicBinaryNodeleft,right;//分别指向左、右孩子的结点   publicBinaryTree(){//构造空二叉树   this.root=null;  }   publicBinaryTree(BinaryNoderoot){//构造指定根结点的二叉树   this.root=root;  }   publicbooleanisEmpty(){//判断是否是空二叉树   returnthis.root==null;  }   publicBinaryNodegetRoot(){//返回二叉树的根结点   returnthis.root;  }   //三种次序遍历的成员方法  publicvoidpreOrder(){//先根次序遍历二叉树   System.out.print("\n先根序列:");  preOrder(root);  }   privatevoidpreOrder(BinaryNodep){//先根次序遍历以p结点为根的子树   if(p!=null){//若二叉树不空   System.out.print(p.data+"");//访问当前结点  preOrder(p.left);//访问根次序遍历当前结点的左子树  preOrder(p.right);//访问根次序遍历当前结点的右子树  }  }   //非递归方法中序遍历二叉树  publicvoidinOrder(){   System.out.print("\n中根序列(非递归):");  LinkedStack
stack=newLinkedStack();   BinaryNodep=this.root;  while(p!=null||!stack.isEmpty()){//p非空或栈非空时   if(p!=null){  stack.push(p);//p结点入栈  p=p.left;//进入左子树  }else{  p=stack.pop();//p指向出栈结点  System.out.print(p.data+"");  p=p.right;//进入右子树  }  }  }   /**  *递归方法中序二叉树遍历  *publicvoidinOrder(){//中根次序遍历二叉树  *  * System.out.print("\n中根序列:");  * inOrder(root);  *}  *  *privatevoidinOrder(BinaryNodep){//中根次序遍历以p结点为根的子树  *  * if(p!=null){//若二叉树不空  *  * inOrder(p.left);//访问根次序遍历当前结点的左子树  * System.out.print(p.data+"");//访问当前结点  * inOrder(p.right);//访问根次序遍历当前结点的右子树  *}  *}  */    publicvoidpostOrder(){//后根次序遍历二叉树   System.out.print("\n后根序列:");  postOrder(root);  }   privatevoidpostOrder(BinaryNodep){//后根次序遍历以p结点为根的子树   if(p!=null){//若二叉树不空   postOrder(p.left);//访问根次序遍历当前结点的左子树  postOrder(p.right);//访问根次序遍历当前结点的右子树  System.out.print(p.data+"");//访问当前结点  }  }   //求结点个数  publicintcount(){//返回二叉树的结点个数   returncount(root);  }   privateintcount(BinaryNodep){//返回以p结点为根的子树的结点个数   if(p!=null)  return1+count(p.left)+count(p.right);  else  return0;  }   //求高度  publicintheight(){//返回二叉树的高度   returnheight(root);  }   privateintheight(BinaryNodep){//返回以p结点为根的子树高度,后根次序遍历    if(p!=null){   intld=height(p.left);//返回左子树的高度  intrd=height(p.right);//返回右子树的高度  return(ld>=rd)?ld+1:rd+1;//当前子树的高度为较高子树的高度加1  }else  return0;    }   //获得父母结点  publicBinaryNodegetParent(BinaryNodenode){//返回node的父母结点,若为空树、未找到或node为根,返回null   if((root==null)||(node==null)||(node==root))  returnnull;   else  returngetParent(root,node);  }   privateBinaryNodegetParent(BinaryNodep,BinaryNodenode){//在以p为根结点的子树中查找并返回node结点的父母结点   BinaryNodefind=null;  if(p!=null){   if(p.left==node||p.right==null)  find=p;//查找成功   else{   find=getParent(p.left,node);//在左子树中查找  if(find==null)  find=getParent(p.right,node);//若左子树中未找到,则继续在右子树中查找  }  }  returnfind;//返回找到的父母结点  }   //获得左、右孩子  publicBinaryNodegetLChild(BinaryNodenode){//返回node的左孩子,若为空树、未找到或node为叶子,返回null    if((root==null)||(node==null)||(node.left==null)||(node.right==null))  returnnull;  else  returnnode.left;  }   publicBinaryNodegetRChild(BinaryNodenode){//返回node的右孩子,若为空树、未找到或node为叶子,返回null    if((root==null)||(node==null)||(node.left==null)||(node.right==null))  returnnull;  else  returnnode.right;  }   //查找  publicBinaryNodesearch(Evalue){//查找值为value的结点   returnsearch(root,value);  }   privateBinaryNodesearch(BinaryNodep,Evalue){//在以p为根的子树中查找值为value结点,先根次序遍历,返回查找的结点,若未找到返回null   BinaryNodefind=null;//记载找到的结点  if(p!=null//查找成功  else{   find=search(p.left,value);//在左子树中查找  if(find==null)  find=search(p.right,value);//若左子树中未找到,则继续在右子树中查找  }  }  returnfind;//返回找到结点  }   //插入一个结点  publicvoidinsert(BinaryNodep,Eelement,booleanleftChild){//插入元素element作为p的孩子,若leftChild为true,插入结点作为左孩子,否则作为右孩子   if(p!=null)  if(leftChild)  p.left=newBinaryNode(element,p.left,null);  else  p.right=newBinaryNode(element,null,p.right);  }   publicvoidinsert(BinaryNodep,Eelement){//插入元素element作为p结点的左孩子   insert(p,element,true);  }   //删除一课子树  publicvoidremove(BinaryNodep,booleanleftChild){//删除p结点的左/右子树,若leftChild为true,删除左子树,否则删除右子树   if(p!=null)  if(leftChild)  p.left=null;  else   p.right=null;  }   publicvoidremove(BinaryNodep){   remove(p,true);  }  }                                                                #4
              二叉树递归前序遍历的流程图http://hi.csdn.net/space-3316705-do-album-picid-519456.html                                                                #5
              好长好长……                                                                #6
              二叉树:1||---------------|---------------|23|||---------------||---------------|4567先序先从根开始输出结果1245367                                                                #7
              二叉树:1||---------------|23|||-------||-------|4567先序先从根开始输出结果1245367                                                                #8
              卡,人家需要的先序创建二叉树,你们都整个先序遍历,真有意思                                                                #9
              lz具体看http://www.doc88.com/p-301888047022.html吧