二叉树的基本概念:树是一种类似于链表的数据结构,不过树的一个结点可以指向多个结点。树是一种典型的非线性结构。树是表示具有层次特性的图的结构的一种方法。
二叉树的结点结构用代码表示:
public class BinaryTreeNode{
private int data;
private BinaryTreeNode left;
private BinaryTreeNode right;
public BinaryTreeNode(int val){
this.data = val;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public BinaryTreeNode getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;
}
public BinaryTreeNode getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
this.right = right;
}
}
二叉树的遍历
- 前序遍历:
- 后序遍历:
- 中序遍历:
- 层次遍历:
前序遍历迭代实现
private void preOrder(BinaryTreeNode root){
System.out.print(""+root.getData());
preOrder(root.getLeft());
preOrder(root.getRight());
}
前序非迭代实现
private void preOrderNoRecursive(BinaryTreeNode root){
if (root == null) {
return;
}
Stack<BinaryTreeNode> stack = new Stack<>();
while (true) {
while (root != null) {
System.out.print(""+root.getData());
stack.push(root);
root = root.getLeft();
}
if (stack.isEmpty()) {
break;
}
root = stack.pop();
root = root.getRight();
}
return;
}
中序遍历迭代实现
private void inOrder(BinaryTreeNode root){
inOrder(root.getLeft());
System.out.print(""+root.getData());
inOrder(root.getRight());
}
中序非迭代实现
private void inOrderNonRecursive(BinaryTreeNode root){
if (root == null) {
return;
}
Stack<BinaryTreeNode> stack = new Stack();
while (true) {
while (root != null) {
stack.push(root);
root = root.getLeft;
}
if (stack.isEmpty()) {
break;
}
root = stack.pop();
System.out.print(""+root.getData());
root = root.getRight();
}
}
后序遍历迭代实现
private void postOrder(BinaryTreeNode root){
postOrder(root.getLeft());
postOrder(root.getRight());
System.out.print(""+root.getData());
}
后序非迭代实现
private void postOrderNonRecursive(BinaryTreeNode root){
Stack<BinaryTreeNode> stack = new Stack<>();
while (true) {
if (root != null) {
stack.push(root);
root = root.getLeft();
}else {
if (stack.isEmpty()) {
System.out.print("empty tree!");
return;
}else {
if(stack.peek().getRight() == null) {
root = stack.pop();
System.out.print(root.getData());
if (root == stack.peek().getRight()) {
System.out.print(root.getData());
stack.pop();
}
}
}
if (!stack.isEmpty()) {
root = stack.peek().getRight();
}else{
root = null;
}
}
}
}
层次遍历实现
private void levelOrder(BinaryTreeNode root){
if (root == null) {
return;
}
Queue<BinaryTreeNode> queue = new Queue<>();
BinaryTreeNode head;
queue.add(root);
while(!queue.isEmpty()){
head = queue.remove();
if (head.getLeft() != null) {
queue.add(head.getLeft());
}
if (head.getRight() != null) {
queue.add(head.getRight());
}
}
}
例题:
前序遍历结果:ABDEGCF
中序遍历结果:DBGEACF
求后序遍历结果?
public TreeNode createTree(String preOrder, String inOrder){
if (preOrder.isEmpty()){
return null;
}
char rootValut = preOrder.charAt(0);
int rootIndex = inOrder.indexOf(rootValut);
TreeNode root = new TreeNode(rootValue);
root.setLeft(createTree(preOrder.substring(1,1+rootIndex), inOrder.substring(0,rootIndex)));
root.setRight(createTree(preOrder.substring(1+rootIndex),inOrder.substring(1 + rootIndex)));
return root;
}
同理,得出后序遍历序列。迭代的核心始终是减小问题的规模,和初始情况值,也就是递归结束的情况的值。
public String postOrder(String preOrder, String inOrder){
if (preOrder.isEmpty()){
return null;
}
char rootValut = preOrder.charAt(0);
int rootIndex = inOrder.indexOf(rootValut);
return postOrder(preOrder.substring(1, 1+rootIndex), inOrder.substring(0, rootIndex)) +
postOrder(preOrder.substring(1+rootIndex), inOrder.substring(1 + rootIndex))
+ rootValut;
}
前序遍历结果:ABDEGCF
后序遍历结果:DBGEACF
?1、构建二叉树唯一吗?
?2、不唯一的话有多少个?
?3、能够输出全部可能中序吗?
例题2:
查找最大元素?
递归算法:
private int findMax(BinaryTreeNode root){
int rootValue,left,right,max = 0;
if (root != null) {
rootValue = root.getData();
left = findMax(root.getLeft());
right = findMax(root.getRight());
if (left > right) {
max = left;
}else {
max = right;
}
if (rootValue > max) {
max = rootValue;
}
}
return max;
}
非递归算法:
private int findMaxUsingLevelOrder(BinaryTreeNode root){
int max;
Queue<BinaryTreeNode> queue = new LinkedList<>();
BinaryTreeNode tmp;
queue.offer(root);
while (!queue.isEmpty()) {
tmp = queue.pop();
if (tmp.getData() > max) {
max = tmp.getData();
}
if (tmp.getLeft != null) {
queue.offer(tmp.getLeft());
}
if (tmp.getRight() != null) {
queue.offer(tmp.getRight());
}
}
return max;
}
例题3:
搜索某个元素?
递归算法:
private boolean findInBinaryTreeNodeRecursion(BinaryTreeNode root, int data){
if (root == null) {
return false;
}
if (root.getData() == data) {
return true;
}
return findInBinaryTreeNodeRecursion(root.getLeft(),data) || findInBinaryTreeNodeRecursion(root.getRight(),data);
}
非递归算法:
private boolean findInBinaryUsingLevelOrder(BinaryTreeNode root, int data){
Queue<BinaryTreeNode> queue = new LinkedList<>();
BinaryTreeNode tmp;
queue.offer(root);
while(!queue.isEmpth()){
tmp = queue.poll();
if (tmp.getData == data) {
return true;
}
if (tmp.getLeft() != null) {
queue.offer(tmp.getLeft());
}
if (tmp.getRight() != null) {
queue.offer(tmp.getRight());
}
}
return false;
}
例题4:
寻找中序遍历时的下一结点?
首先二叉树的数据结构里要加入一个parent变量,不然是无法解答的:
private BinaryTreeNode parent;
public void setLeft(BinaryTreeNode left) {
this.left = left;
if (left != null)
this.left.setParent(this);
}
public void setRight(BinaryTreeNode right) {
this.right = right;
if (right != null)
this.right.setParent(this);
}
public BinaryTreeNode getParent() {
return parent;
}
public void setParent(BinaryTreeNode parent) {
this.parent = parent;
}
分析如图
public BinaryTreeNode next(BinaryTreeNode node){
if (node.getRight() != null) {
node = node.getRight();
while(node.getLeft() != null){
node = node.getLeft();
}
return node;
}else {
if (node.getParent().getLeft() == node) {
return node.getParent();
}else {
while (node.getParent() != null && node.getParent().getLeft() != node) {
node = node.getParent();
}
return node.getParent;
}
}
}