Java代码实现在一棵二叉链表表示的二叉树中,实现1.输入叶子结点、2.求二叉树中叶子结点个数、3.将每个结点的左子树与右子树交换、4.验证二叉树的性质3:n0=n2+1、5.输出值大于k的结点、6.已知先根和中根次序遍历序列构造二叉树、7.以广义表表示构造二叉树、8.判断两颗二叉树是否相等、9.求结点所在的层次、10.求一颗二叉树在后根次序遍历下第一个访问的结点、11.复制一颗二叉树、12.判断一颗二叉树是否为完全二叉树、13.实现二叉树后根次序遍历的非递归算法的操作,并每一个操作分别采用先根、中根、后根、层次遍历算法
时间: 2023-07-10 08:36:07 浏览: 101
java语言实现的二叉树的各种操作(包括递归与非递归遍历二叉树,求二叉树的高度,节点总数,叶子节点等)
5星 · 资源好评率100%
很好,我会尽力回答你的问题。这是一组比较全面的二叉树问题,需要分开回答。以下是我对每个问题的回答:
1. 输入叶子节点
可以通过以下代码实现:
```java
public void insertLeaf(Object data) {
Node newNode = new Node(data);
if (root == null) {
root = newNode;
} else {
Node current = root;
while (true) {
if (current.getLeftChild() == null) {
current.setLeftChild(newNode);
break;
} else if (current.getRightChild() == null) {
current.setRightChild(newNode);
break;
} else {
current = current.getLeftChild();
}
}
}
}
```
2. 求二叉树中叶子结点个数
可以通过以下代码实现:
```java
public int countLeaves(Node node) {
if (node == null) {
return 0;
}
if (node.getLeftChild() == null && node.getRightChild() == null) {
return 1;
} else {
return countLeaves(node.getLeftChild()) + countLeaves(node.getRightChild());
}
}
```
3. 将每个结点的左子树与右子树交换
可以通过以下代码实现:
```java
public void swapChildren(Node node) {
if (node == null) {
return;
}
Node temp = node.getLeftChild();
node.setLeftChild(node.getRightChild());
node.setRightChild(temp);
swapChildren(node.getLeftChild());
swapChildren(node.getRightChild());
}
```
4. 验证二叉树的性质3:n0=n2+1
可以通过以下代码实现:
```java
public boolean verifyProperty3(Node node) {
if (node == null) {
return true;
}
int n0 = countLeaves(node);
int n2 = countNodesWithTwoChildren(node);
return n0 == n2 + 1;
}
public int countNodesWithTwoChildren(Node node) {
if (node == null) {
return 0;
}
int count = 0;
if (node.getLeftChild() != null && node.getRightChild() != null) {
count = 1;
}
count += countNodesWithTwoChildren(node.getLeftChild()) + countNodesWithTwoChildren(node.getRightChild());
return count;
}
```
5. 输出值大于k的结点
可以通过以下代码实现:
```java
public void printNodesGreaterThanK(Node node, int k) {
if (node == null) {
return;
}
if ((int) node.getData() > k) {
System.out.print(node.getData() + " ");
}
printNodesGreaterThanK(node.getLeftChild(), k);
printNodesGreaterThanK(node.getRightChild(), k);
}
```
6. 已知先根和中根次序遍历序列构造二叉树
可以通过以下代码实现:
```java
public Node buildTreeFromPreorderAndInorder(List preorder, List inorder) {
if (preorder.isEmpty() || inorder.isEmpty()) {
return null;
}
Object rootValue = preorder.get(0);
int rootIndex = inorder.indexOf(rootValue);
List leftInorder = inorder.subList(0, rootIndex);
List rightInorder = inorder.subList(rootIndex + 1, inorder.size());
List leftPreorder = preorder.subList(1, 1 + leftInorder.size());
List rightPreorder = preorder.subList(1 + leftInorder.size(), preorder.size());
Node rootNode = new Node(rootValue);
rootNode.setLeftChild(buildTreeFromPreorderAndInorder(leftPreorder, leftInorder));
rootNode.setRightChild(buildTreeFromPreorderAndInorder(rightPreorder, rightInorder));
return rootNode;
}
```
7. 以广义表表示构造二叉树
可以通过以下代码实现:
```java
public Node buildTreeFromGeneralizedList(String s) {
Stack<Node> stack = new Stack<>();
Node root = null;
Node parent = null;
boolean isLeftChild = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') {
if (parent != null) {
stack.push(parent);
}
parent = null;
isLeftChild = true;
} else if (c == ')') {
if (!stack.isEmpty()) {
parent = stack.pop();
}
} else if (Character.isDigit(c) || c == '-') {
int j = i + 1;
while (j < s.length() && (Character.isDigit(s.charAt(j)) || s.charAt(j) == '-')) {
j++;
}
int value = Integer.parseInt(s.substring(i, j));
Node newNode = new Node(value);
if (root == null) {
root = newNode;
} else if (parent != null) {
if (isLeftChild) {
parent.setLeftChild(newNode);
} else {
parent.setRightChild(newNode);
}
}
parent = newNode;
i = j - 1;
} else if (c == ',') {
isLeftChild = false;
}
}
return root;
}
```
8. 判断两颗二叉树是否相等
可以通过以下代码实现:
```java
public boolean isSameTree(Node p, Node q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (!p.getData().equals(q.getData())) {
return false;
}
return isSameTree(p.getLeftChild(), q.getLeftChild()) && isSameTree(p.getRightChild(), q.getRightChild());
}
```
9. 求结点所在的层次
可以通过以下代码实现:
```java
public int getLevel(Node node, Object data) {
return getLevelHelper(node, data, 1);
}
private int getLevelHelper(Node node, Object data, int level) {
if (node == null) {
return -1;
}
if (node.getData().equals(data)) {
return level;
}
int downLevel = getLevelHelper(node.getLeftChild(), data, level + 1);
if (downLevel == -1) {
downLevel = getLevelHelper(node.getRightChild(), data, level + 1);
}
return downLevel;
}
```
10. 求一颗二叉树在后根次序遍历下第一个访问的结点
可以通过以下代码实现:
```java
public Node getFirstNodeInPostorder(Node node) {
if (node == null) {
return null;
}
Node left = getFirstNodeInPostorder(node.getLeftChild());
Node right = getFirstNodeInPostorder(node.getRightChild());
if (left != null) {
return left;
}
if (right != null) {
return right;
}
return node;
}
```
11. 复制一颗二叉树
可以通过以下代码实现:
```java
public Node copyTree(Node node) {
if (node == null) {
return null;
}
Node newNode = new Node(node.getData());
newNode.setLeftChild(copyTree(node.getLeftChild()));
newNode.setRightChild(copyTree(node.getRightChild()));
return newNode;
}
```
12. 判断一颗二叉树是否为完全二叉树
可以通过以下代码实现:
```java
public boolean isCompleteTree(Node node) {
if (node == null) {
return true;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(node);
boolean end = false;
while (!queue.isEmpty()) {
Node current = queue.poll();
if (end && (current.getLeftChild() != null || current.getRightChild() != null)) {
return false;
}
if (current.getLeftChild() == null && current.getRightChild() != null) {
return false;
}
if (current.getLeftChild() != null) {
queue.offer(current.getLeftChild());
}
if (current.getRightChild() != null) {
queue.offer(current.getRightChild());
} else {
end = true;
}
}
return true;
}
```
13. 实现二叉树后根次序遍历的非递归算法的操作,并每一个操作分别采用先根、中根、后根、层次遍历算法
可以通过以下代码实现:
```java
public List postorderTraversal(Node root) {
List result = new ArrayList<>();
if (root == null) {
return result;
}
Stack<Node> stack1 = new Stack<>();
Stack<Node> stack2 = new Stack<>();
stack1.push(root);
while (!stack1.isEmpty()) {
Node node = stack1.pop();
stack2.push(node);
if (node.getLeftChild() != null) {
stack1.push(node.getLeftChild());
}
if (node.getRightChild() != null) {
stack1.push(node.getRightChild());
}
}
while (!stack2.isEmpty()) {
result.add(stack2.pop().getData());
}
return result;
}
public List inorderTraversal(Node root) {
List result = new ArrayList<>();
if (root == null) {
return result;
}
Stack<Node> stack = new Stack<>();
Node current = root;
while (current != null || !stack.isEmpty()) {
while (current != null) {
stack.push(current);
current = current.getLeftChild();
}
current = stack.pop();
result.add(current.getData());
current = current.getRightChild();
}
return result;
}
public List preorderTraversal(Node root) {
List result = new ArrayList<>();
if (root == null) {
return result;
}
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
Node node = stack.pop();
result.add(node.getData());
if (node.getRightChild() != null) {
stack.push(node.getRightChild());
}
if (node.getLeftChild() != null) {
stack.push(node.getLeftChild());
}
}
return result;
}
public List levelOrderTraversal(Node root) {
List result = new ArrayList<>();
if (root == null) {
return result;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
Node node = queue.poll();
result.add(node.getData());
if (node.getLeftChild() != null) {
queue.offer(node.getLeftChild());
}
if (node.getRightChild() != null) {
queue.offer(node.getRightChild());
}
}
return result;
}
```
阅读全文