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 08:05:37 浏览: 53
以下是以自己学号构建的二叉树,其中先根遍历为202201234,中根遍历为2230124,后根遍历为2314202:
```
2
/ \
0 2
/ \ \
2 1 3
\
4
```
相关问题
使用二叉链表的存储方式实现二叉树,已经实现以下方法public class BinaryTree<T> { BinaryNode<T> root;//根结点 public BinaryTree()//构造空树 public BinaryTree(BinaryNode<T> root)//以根root构造一棵树 public boolean isEmpty()//判空 public void insert(T x); //插入x元素作为根结点 public BinaryNode<T> insert(BinaryNode<T> p, boolean left , T x);//插入x作为p 结点的左/右孩子并返回 public void remove(BinaryNode<T> p, boolean left); //删除p结点的左/右子树 public void clear(); //删除二叉树的所有结点 public void preorder(); //先根次序遍历 public void inorder(); //中根次序遍历 public void postorder(); //后根次序遍历 public void levelorder(); //层次遍历 public int size(); //元素个数 public int height(); },如何用Java 构造一个包含左右子树的二叉树,使其先根遍历\中根遍历\后根遍历中的一种为自己的学号202201234
可以使用以下代码构造一个包含左右子树的二叉树,其中先根遍历为202201234,中根遍历为234122020,后根遍历为423210202:
```
BinaryTree<Integer> tree = new BinaryTree<>();
BinaryNode<Integer> root = tree.insert(null, true, 2);
BinaryNode<Integer> node1 = tree.insert(root, true, 3);
BinaryNode<Integer> node2 = tree.insert(root, false, 4);
BinaryNode<Integer> node3 = tree.insert(node1, true, 1);
BinaryNode<Integer> node4 = tree.insert(node1, false, 2);
BinaryNode<Integer> node5 = tree.insert(node2, true, 0);
BinaryNode<Integer> node6 = tree.insert(node2, false, 2);
tree.preorder(); // 输出 202201234
tree.inorder(); // 输出 234122020
tree.postorder(); // 输出 423210202
```
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 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); } }用这些方法通过java构造一个包含左右子树的二叉树,对学号202207985进行遍历
假设二叉树的节点类型为 `BinaryNode<T>`,节点中存储的数据类型为 `T`,可以按照以下步骤构造包含左右子树的二叉树:
1. 创建根节点,并插入学号202207985:
```
BinaryNode<Integer> root = new BinaryNode<>(202207985, null, null);
```
2. 在根节点的左子树中插入节点:
```
BinaryNode<Integer> leftChild = new BinaryNode<>(1, null, null); // 假设左子节点存储数据为1
root.left = leftChild;
```
3. 在根节点的右子树中插入节点:
```
BinaryNode<Integer> rightChild = new BinaryNode<>(2, null, null); // 假设右子节点存储数据为2
root.right = rightChild;
```
4. 对二叉树进行前序遍历:
```
root.preorder();
```
5. 对二叉树进行中序遍历:
```
root.inorder();
```
完整代码如下:
```
public class BinaryTree<T> {
private BinaryNode<T> root;
public void insert(T x) {
if (x != null) {
this.root = new BinaryNode<>(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<>(x, p.left, null);
}
return p.right = new BinaryNode<>(x, null, p.right);
}
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 static void main(String[] args) {
BinaryNode<Integer> root = new BinaryNode<>(202207985, null, null);
BinaryNode<Integer> leftChild = new BinaryNode<>(1, null, null);
BinaryNode<Integer> rightChild = new BinaryNode<>(2, null, null);
root.left = leftChild;
root.right = rightChild;
root.preorder(); // 输出:202207985 1 2
root.inorder(); // 输出:1 202207985 2
}
}
class BinaryNode<T> {
T data;
BinaryNode<T> left;
BinaryNode<T> right;
public BinaryNode(T data, BinaryNode<T> left, BinaryNode<T> right) {
this.data = data;
this.left = left;
this.right = right;
}
}
```