二叉树的遍历(递归,非递归,层次)
2017-01-17 17:03
489 查看
非递归中序遍历:
// 对于结点root;
// 1)若其结点不为空,则将root入栈并将root的左孩子置为当前的root,然后对当前结点root再进行相同的处理;
// 2)若其左孩子为空,则取栈顶元素并进行出栈操作,访问该节点,然后将当前的root置为栈顶结点的右孩子
// 3)直到p为null并且栈为空则遍历结束
// iterative
public void inorderTraversal2(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
List<Integer> list = new ArrayList<>();
while(root!=null || !stack.isEmpty()){
while( root!= null){
stack.push( root);
root = root. left;
}
if (! stack.isEmpty()) {
TreeNode topNode = stack.peek();
System. out.println( " "+ topNode. key);
stack.pop();
if ( null != topNode. right) {
root= topNode. right;
}
}
}
}
// 非递归中序遍历----这种不容易记忆和编写
public void inorder_iterative_tree_walk(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
while( root!= null || ! stack.isEmpty()){
if ( root != null) {
stack.push( root);
root = root. left;
} else{
TreeNode top = stack.peek();
System. out.print( top. key+ " ");
stack.pop();
root = top. right; //访问右子树
}
}
}
// 中序遍历---递归
public void inorder_tree_walk( TreeNode x){
if ( x!= null) {
inorder_tree_walk( x. left);
System. out.print( x. key + " ");
inorder_tree_walk( x. right);
}
}
// 前序遍历---递归
public void preorder_tree_walk( TreeNode x){
if ( x!= null) {
System. out.print( x. key + " ");
preorder_tree_walk( x. left);
preorder_tree_walk( x. right);
}
}
// 前序遍历---非递归
public void preorder_iterative_tree_walk(TreeNode x){
Stack<TreeNode > stack = new Stack<>();
stack.push( x);
while(! stack.isEmpty()){
TreeNode top = stack.peek();
System. out.print( top. key+ " ");
stack.pop();
if( top. right!= null){
stack.push( top. right);
}
if ( top. left!= null) {
stack.push( top. left);
}
}
}
// 后序遍历---递归版
public void postorder_tree_walk(TreeNode x){
if ( x!= null) {
postorder_tree_walk( x. left);
postorder_tree_walk( x. right);
System. out.print( x. key+ " ");
}
}
// 后序遍历非递归
// 1.push根结点到第一个栈s中
// 2.从第一个栈s中弹出一个结点,并将其push到第二个output中
// 3.然后push结点的左孩子和右孩子到第一个栈s中
// 4.重复2,3过程直到栈s为空
public void postorder_iterative_tree_walk(TreeNode x){
if ( x== null) return ;
Stack<TreeNode> stack = new Stack<>();
Stack<TreeNode> output = new Stack<>();
stack.push( x);
while(! stack.isEmpty()){
TreeNode top = stack.peek();
output.push( top);
stack.pop();
if ( top. left!= null) {
stack.push( top. left);
}
if ( top. right != null) {
stack.push( top. right);
}
}
while(! output.isEmpty()){
System. out.print( output.peek(). key+ " ");
output.pop();
}
}
// 层次遍历
public void level_order_tree_walk(TreeNode x){
if ( x== null) return ;
//java中队列的调用方法和c++的不一样
LinkedBlockingQueue<TreeNode> queue = new LinkedBlockingQueue();
queue.add( x);
while(! queue.isEmpty()){
TreeNode top = queue.peek();
System. out.print( top. key+ " ");
queue.poll(); //移除元素
if ( top. left != null) {
queue.offer( top. left);
}
if ( top. right != null) {
queue.offer( top. right);
}
}
}
// 对于结点root;
// 1)若其结点不为空,则将root入栈并将root的左孩子置为当前的root,然后对当前结点root再进行相同的处理;
// 2)若其左孩子为空,则取栈顶元素并进行出栈操作,访问该节点,然后将当前的root置为栈顶结点的右孩子
// 3)直到p为null并且栈为空则遍历结束
// iterative
public void inorderTraversal2(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
List<Integer> list = new ArrayList<>();
while(root!=null || !stack.isEmpty()){
while( root!= null){
stack.push( root);
root = root. left;
}
if (! stack.isEmpty()) {
TreeNode topNode = stack.peek();
System. out.println( " "+ topNode. key);
stack.pop();
if ( null != topNode. right) {
root= topNode. right;
}
}
}
}
// 非递归中序遍历----这种不容易记忆和编写
public void inorder_iterative_tree_walk(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
while( root!= null || ! stack.isEmpty()){
if ( root != null) {
stack.push( root);
root = root. left;
} else{
TreeNode top = stack.peek();
System. out.print( top. key+ " ");
stack.pop();
root = top. right; //访问右子树
}
}
}
// 中序遍历---递归
public void inorder_tree_walk( TreeNode x){
if ( x!= null) {
inorder_tree_walk( x. left);
System. out.print( x. key + " ");
inorder_tree_walk( x. right);
}
}
// 前序遍历---递归
public void preorder_tree_walk( TreeNode x){
if ( x!= null) {
System. out.print( x. key + " ");
preorder_tree_walk( x. left);
preorder_tree_walk( x. right);
}
}
// 前序遍历---非递归
public void preorder_iterative_tree_walk(TreeNode x){
Stack<TreeNode > stack = new Stack<>();
stack.push( x);
while(! stack.isEmpty()){
TreeNode top = stack.peek();
System. out.print( top. key+ " ");
stack.pop();
if( top. right!= null){
stack.push( top. right);
}
if ( top. left!= null) {
stack.push( top. left);
}
}
}
// 后序遍历---递归版
public void postorder_tree_walk(TreeNode x){
if ( x!= null) {
postorder_tree_walk( x. left);
postorder_tree_walk( x. right);
System. out.print( x. key+ " ");
}
}
// 后序遍历非递归
// 1.push根结点到第一个栈s中
// 2.从第一个栈s中弹出一个结点,并将其push到第二个output中
// 3.然后push结点的左孩子和右孩子到第一个栈s中
// 4.重复2,3过程直到栈s为空
public void postorder_iterative_tree_walk(TreeNode x){
if ( x== null) return ;
Stack<TreeNode> stack = new Stack<>();
Stack<TreeNode> output = new Stack<>();
stack.push( x);
while(! stack.isEmpty()){
TreeNode top = stack.peek();
output.push( top);
stack.pop();
if ( top. left!= null) {
stack.push( top. left);
}
if ( top. right != null) {
stack.push( top. right);
}
}
while(! output.isEmpty()){
System. out.print( output.peek(). key+ " ");
output.pop();
}
}
// 层次遍历
public void level_order_tree_walk(TreeNode x){
if ( x== null) return ;
//java中队列的调用方法和c++的不一样
LinkedBlockingQueue<TreeNode> queue = new LinkedBlockingQueue();
queue.add( x);
while(! queue.isEmpty()){
TreeNode top = queue.peek();
System. out.print( top. key+ " ");
queue.poll(); //移除元素
if ( top. left != null) {
queue.offer( top. left);
}
if ( top. right != null) {
queue.offer( top. right);
}
}
}
相关文章推荐
- java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历
- 二叉树2:层次遍历方式及先序、中序、后序(递归与非递归)遍历方式
- 二叉树的高度 java 利用递归和层次遍历两种方法
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构 ——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树的先序,中序,层次遍历,递归与非递归实现
- 二叉树的遍历:先序,中序,后序,递归,非递归,层次遍历
- 二叉树的各种遍历(先序、中序、后续、层次)——递归、非递归
- 二叉树的创建,以及递归前中后序以及层次遍历
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树(一) 先序遍历、中序遍历、后续遍历、层次遍历的递归与非递归实现
- 数据结构(一)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树各种遍历的实现(递归、非递归、层次、高度和节点数目)
- java数据结构--二叉树,递归遍历,非递归遍历,层次遍历
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 非递归层次遍历方法实现二叉树中指定节点的层次数查找
- 二叉树相关操作(前序遍历,中序遍历,后序遍历,层次序遍历等)递归和非递归实现
- 二叉树层次遍历与递归释放