public class BinaryTree<T> { public BinaryNode<T> root; public BinaryTree(){ this.root=null; } public boolean isEmpty(){ return this.root==null; } public void insert(T x){ if(x!=null){ this.root=new BinaryNode<T>(x,this.root,null); } } public BinaryNode<T> insert(BinaryNode<T> p,boolean left,T x){ if(x==null||p==null){ return null; } if(left){ return p.left=new BinaryNode<T>(x, p.left, null); } return p.right=new BinaryNode<T>(x, null, p.right); } public void remove(BinaryNode<T> p,boolean left){ if(p!=null) { if(left) { p.left=null; }else{ p.right=null; } } } public void clear(){ this.root=null; } public void preorder(){ preorder(this.root); System.out.println(); } public void preorder(BinaryNode<T> p){ if(p!=null){ System.out.print(p.data.toString()+" "); preorder(p.left); preorder(p.right); } } public void inorder(){ inorder(this.root); System.out.println(); } public void inorder(BinaryNode<T> p){ if(p!=null){ inorder(p.left); System.out.print(p.data.toString() + " "); inorder(p.right); } } public void postorder(){ postorder(this.root); System.out.println(); } public void postorder(BinaryNode<T> p){ if(p!=null){ postorder(p.left); postorder(p.right); System.out.print(p.data.toString()+" "); } } public void levelorder(){ if(this.root==null){ return; } Queue<BinaryNode<T>> que=new LinkedTransferQueue<BinaryNode<T>>(); que.add(this.root); while(!que.isEmpty()){ BinaryNode<T> p=que.poll(); System.out.print(p.data+" "); if(p.left!=null){ que.add(p.left); } if(p.right!=null){ que.add(p.right); } } System.out.println(); } } class BinaryNode<T>{ BinaryNode<T> left; BinaryNode<T> right; T data; public BinaryNode(T data,BinaryNode<T> left,BinaryNode<T> right){ this.data=data; this.left=left; this.right=right; } public BinaryNode(T data){ } public String toString(){ return this.data.toString(); } public boolean isLeaf(){ return false; } },用Java语言构造一个包含左右子树的二叉树,使其先根遍历\中根遍历\后根遍历中的一种为自己的学号202201234
时间: 2023-06-20 10:05:37 浏览: 88
以下是以自己学号构建的二叉树,其中先根遍历为202201234,中根遍历为2230124,后根遍历为2314202:
```
2
/ \
0 2
/ \ \
2 1 3
\
4
```
阅读全文