1.简介
想要分析好HashMap,必须要把红黑树的操作讲清楚。因此先依次弄清二叉树、二插搜索树以及平衡二叉树的操作,最后在分析红黑树的操作。
2.二叉树遍历操作
二叉树:
2.1 先序遍历
先序:A->B->E->F->C
递归:
public void preRecOrder(TreeNode root) {
if (root == null) return;
System.out.println(root.val);
preRecOrder(root.left);
preRecOrder(root.right);
}
非递归:
public void preOrder(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
if(root == null)
return;
stack.push(root);
while(!stack.isEmpty()){
TreeNode curNode = stack.pop();
System.out.println(curNode.val);
if(curNode.right != null){
stack.push(curNode.right);
}
if(curNode.left != null){
stack.push(curNode.left);
}
}
}
2.2 中序遍历
中序遍历:E->B->F->A-C
递归
public void inRecOrder(TreeNode root) {
if (root == null) return;
inRecOrder(root.left);
System.out.println(root.val);
inRecOrder(root.right);
}
非递归