Java数据结构——二叉数的遍历

二叉树的遍历

1. 几种遍历方法

1.1 深度遍历:

  1. 先序遍历二叉树的操作定义为:
    若二叉树为空,则空操作;否则
    (1)访问根结点;
    (2)先序遍历左子树;
    (3)先序遍历右子树。
  2. 中序遍历二叉树的操作定义为:
    若三叉树为空,则空操作;否则
    (1)中序遍历左子树;
    (2)访问根结点;
    (3)中序遍历右子树。
  3. 后序遍历二叉树的操作定义为
    若二叉树为空,则空操作;否则
    (1)后序遍历左子树;
    (2)后序遍历右子树;
    (3)访问根结点。

1.2 广度遍历

  1. 层次遍历
    (1)严格的从左至右
    (2)从上到下

1.3 例子


在这里插入图片描述


  • 先序遍历:1 2 4 3 5
  • 中序遍历:4 2 1 3 5
  • 后序遍历:4 2 5 3 1
  • 层次遍历:1 2 3 4 5

2. 二叉树的遍历C语言实现

2.1 先序遍历

// 先序遍历
void PreorderTraversal(BiTree T) {
	if(T) {
		printf("%d ",T->data);
		PreorderTraversal(T->lchild);
	    PreorderTraversal(T->rchild);
	}
}

2.2 中序遍历

// 中序遍历
void InorderTraversal(BiTree T) {
	if(T) {
		InorderTraversal(T->lchild);
		printf("%d ",T->data);
	    InorderTraversal(T->rchild);
	}	
}

2.3 后序遍历

// 后序遍历
void PostorderTraversal(BiTree T) {
	if(T) {
		PostorderTraversal(T->lchild);
	    PostorderTraversal(T->rchild);
	    printf("%d ",T->data);
	}
}

2.4 层次遍历

  • 法一:
// 层次遍历
void LevelorderTraversal(BiTree T) {
	if(T) {
		// 不用队列,只能取数组了
		BiTree B[100];
		// 根节点第一个
		B[0] = T;
		// 巧妙之处 
		int first = 0;
		int after = 1;
		// 循环 
		while(first < after) {
			printf("%d ",B[first]->data);
			if(B[first]->lchild) {
				B[after] = B[first]->lchild;
				after++;
			} // of if for lchild
			if(B[first]->rchild) {
				B[after] = B[first]->rchild;
				after++;
			} // of if for rchild
			first++;			
		} // end while
	} // end if
} // end LevelorderTraversal
  • 法二:
//层次遍历
void LevelorderTraversal( BiTree T )
{
	BiTree a[100];
	int i = 0 ,j = 0;
	a[0] = T;
	while(T)
	{
		if(T->lchild!=NULL) a[++i] = T->lchild;
		if(T->rchild!=NULL) a[++i] = T->rchild;
		BiTree t = a[j];
		printf(" %d",t->data);
		if(i==j) break;
		j++;
		T = a[j];
	}
}

2.5 测试

2.5.1 源码
#include<stdio.h>
#include<malloc.h>

typedef int TElemType;

// 二叉树的二叉链表存储表示
typedef struct BiTNode {
	TElemType data;
	struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
void CreateBiTree(BiTree &T) {
	// 
	T = (BiTree)malloc(sizeof(BiTNode));
	// 
	TElemType data;
	scanf("%d",&data);
	if(data == -1) T = NULL;
	if(T) {
		T->data = data;
		printf("输入%d节点的左子节点:\n",data);
		CreateBiTree(T->lchild);
		printf("输入%d节点的右子节点:\n",data);
		CreateBiTree(T->rchild);		 
	} // end if
} // end CreateBiTree

// 先序遍历
void PreorderTraversal(BiTree T) {
	if(T) {
		printf("%d ",T->data);
		PreorderTraversal(T->lchild);
	    PreorderTraversal(T->rchild);
	}
}
// 中序遍历
void InorderTraversal(BiTree T) {
	if(T) {
		InorderTraversal(T->lchild);
		printf("%d ",T->data);
	    InorderTraversal(T->rchild);
	}	
}
// 后序遍历
void PostorderTraversal(BiTree T) {
	if(T) {
		PostorderTraversal(T->lchild);
	    PostorderTraversal(T->rchild);
	    printf("%d ",T->data);
	}
}
// 层次遍历
void LevelorderTraversal(BiTree T) {
	if(T) {
		// 不用队列,只能取数组了
		BiTree B[100];
		// 根节点第一个
		B[0] = T;
		// 巧妙之处 
		int first = 0;
		int after = 1;
		// 循环 
		while(first < after) {
			printf("%d ",B[first]->data);
			if(B[first]->lchild) {
				B[after] = B[first]->lchild;
				after++;
			} // of if for lchild
			if(B[first]->rchild) {
				B[after] = B[first]->rchild;
				after++;
			} // of if for rchild
			first++;			
		} // end while
	} // end if
} // end LevelorderTraversal

int main(void) {
	printf("输入根节点:\n");
	BiTree T;
	CreateBiTree(T);
	printf("先序遍历:");
	PreorderTraversal(T); printf("\n");
	printf("中序遍历:");
	InorderTraversal(T); printf("\n");
	printf("后序遍历:");
	PostorderTraversal(T); printf("\n");
	printf("层次遍历:");
	LevelorderTraversal(T); printf("\n");
	return 0;
} 
2.5.2 输出样例

在这里插入图片描述


3. 二叉树的遍历Java语言实现

3.1 先序遍历

	/**
	 * 先序遍历
	 */
	public void PreorderTraversal() {
		System.out.print(data + " ");
		if(lchild != null) {
			lchild.PreorderTraversal();
		}
		if(rchild != null) {
			rchild.PreorderTraversal();
		}
	}

3.2 中序遍历

	/**
	 * 中序遍历
	 */
	public void InorderTraversal() {
		if(lchild != null) {
			lchild.InorderTraversal();
		}
		System.out.print(data + " ");
		if(rchild != null) {
			rchild.InorderTraversal();
		}
	}

3.3 后序遍历

	/**
	 * 后序遍历
	 */
	public void PostorderTraversal() {
		if(lchild != null) {
			lchild.PostorderTraversal();
		}
		if(rchild != null) {
			rchild.PostorderTraversal();
		}
		System.out.print(data + " ");
	}

3.4 层次遍历

	/**
	 * 层次遍历
	 * @param T
	 */
	public static void LevelorderTraversal(BiTree T) {
		if(T != null) {
			// 不用队列,只能取数组了
			BiTree[] B = new BiTree[100];
			// 根节点第一个
			B[0] = T;
			// 巧妙之处 
			int first = 0;
			int after = 1;
			// 循环 
			while(first < after) {
				System.out.printf("%d ",B[first].data);
				if(B[first].lchild != null) {
					B[after] = B[first].lchild;
					after++;
				} // of lchild
				if(B[first].rchild != null) {
					B[after] = B[first].rchild;
					after++;
				} // of rchild
				first++;			
			} // end while
		} // end if
		
	} // end LevelorderTraversal

3.5 测试

3.5.1 源码
package datastructure.binarytree;

public class BinaryTree {

	/** 二叉树的数据域 */
	int data;

	/** 左孩子 */
	BinaryTree lchild;

	/** 右孩子 */
	BinaryTree rchild;

	/** 无参构造器 */
	BinaryTree() {
	}

	/**
	 * 构造一个根节点
	 * 
	 * @param elem
	 */
	BinaryTree(int elem) {
		data = elem;
		lchild = null;
		rchild = null;
	}

	/**
	 * 二叉树的存储
	 * 
	 * @return 二叉树
	 */
	public BinaryTree createBinaryTree() {

		// 创建根节点
		BinaryTree root = new BinaryTree(1);

		// 创建树枝,值域
		BinaryTree T_l = new BinaryTree(2);
		BinaryTree T_r = new BinaryTree(3);
		BinaryTree T_l_l = new BinaryTree(4);
		BinaryTree T_r_r = new BinaryTree(5);

		// 指针域
		root.lchild = T_l;
		root.rchild = T_r;
		T_l.lchild = T_l_l;
		T_r.lchild = T_r_r;

		return root;
	} // createBinaryTree

	/**
	 * 先序遍历
	 */
	public void preOrderTraverse() {
		System.out.print(data + " ");
		if (lchild != null) {
			lchild.preOrderTraverse();
		}
		if (rchild != null) {
			rchild.preOrderTraverse();
		}
	}

	/**
	 * 中序遍历
	 */
	public void inOrderTraverse() {
		if (lchild != null) {
			lchild.inOrderTraverse();
		}
		System.out.print(data + " ");
		if (rchild != null) {
			rchild.inOrderTraverse();
		}
	}

	/**
	 * 后序遍历
	 */
	public void postOrderTraverse() {
		if (lchild != null) {
			lchild.postOrderTraverse();
		}
		if (rchild != null) {
			rchild.postOrderTraverse();
		}
		System.out.print(data + " ");
	}

	/**
	 * 层次遍历
	 * 
	 * @param T
	 */
	public static void levelOrderTraverse(BinaryTree T) {
		if (T != null) {
			// 不用队列,只能取数组了
			BinaryTree[] B = new BinaryTree[100];
			// 根节点第一个
			B[0] = T;
			// 巧妙之处
			int first = 0;
			int after = 1;
			// 循环
			while (first < after) {
				System.out.printf("%d ", B[first].data);
				if (B[first].lchild != null) {
					B[after] = B[first].lchild;
					after++;
				}
				if (B[first].rchild != null) {
					B[after] = B[first].rchild;
					after++;
				}
				first++;
			}
		} // end if
	}

	public static void main(String[] args) {
		BinaryTree BT = new BinaryTree();
		BinaryTree binarytree = BT.createBinaryTree();

		System.out.print("先序遍历:");
		binarytree.preOrderTraverse();
		System.out.println();

		System.out.print("中序遍历:");
		binarytree.inOrderTraverse();
		System.out.println();

		System.out.print("后序遍历:");
		binarytree.postOrderTraverse();
		System.out.println();

		System.out.print("层次遍历:");
		levelOrderTraverse(binarytree);
		System.out.println();
	}

} // BinaryTree
3.5.2 输出样例

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姜满月

鼓励,鼓励,更加努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值