JavaScript之二叉树前中后序遍历(递归与非递归版对比)

构建二叉树数据

binaryTree.js

const bt = {
	val: 1,
	left: {
		val: 2,
		left: {
			val: 3,
			left: null,
			right: null
		},
		right: {
			val: 4,
			left: null,
			right: null
		}
	},
	right: {
		val: 5,
		left: {
			val: 6,
			left: null,
			right: null
		},
		right: {
			val: 7,
			left: null,
			right: null
		}
	}
}

module.exports = bt;

通过node模块语法导出。

递归版

前序遍历

前序遍历的顺序是先遍历根节点->左节点->右节点 ( 根 左 右 )的形式

const bt = require('./binaryTree.js')
const preorder = (root) => {
	if(!root) return;
	console.log(root.val) //1 2 3 4 5 6 7
	preorder(root.left)
	preorder(root.right)
}
preorder(bt);
中序遍历
const bt  = require('./binaryTrees.js')
 const inorder = (root) => {
 	if (!root) {return; }
	inorder(root.left) 
	console.log(root.val) //3 2 4 1 6 5 7
 	inorder(root.right)
}
 inorder(bt)
后序遍历
const bt = require('./binaryTrees.js')
 const postorder = (root) => {
	if (!root) {return; }
 	postorder(root.left)
 	postorder(root.right)
 	console.log(root.val) //3 4 2 6 7 5 1 
 }
 postorder(bt)

非递归版

非递归版需要使用到栈结构,利用栈模拟递归时的堆栈调用顺序

前序遍历
 const preorder = (root) => {
 	if (!root) {return; }
 	const stack = [root];
 	while(stack.length>0){
 		const n = stack.pop()
	    console.log(n.val)
		if (n.right) {stack.push(n.right)}
		if (n.left) {stack.push(n.left)}
 	}
 }
 preorder(bt)
中序遍历

const inorder = (root) => {
	if (!root) {return; }
	const stack = [];
	let p = root;
	while(stack.length || p){
		while(p){
			stack.push(p);
			p = p.left;
		}
		const n = stack.pop()
		console.log(n.val)
		p = n.right
	}
}
inorder(bt)
后续遍历
const postorder = (root) => {
	if (!root) {return; }
	const stack = [root];
	const outputStack = [];
	while (stack.length > 0) {
		const n = stack.pop()
		outputStack.push(n)
		if (n.left) {
			stack.push(n.left)
		}
		if (n.right) {
			stack.push(n.right)
		}
	}
	while (outputStack.length){
		const n = outputStack.pop()
		console.log(n.val)
	}
}
postorder(bt)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于一个完全二叉树,我们可以用数组来表示。假设数组的索引从0开始,对于一个节点i,它的左子节点的索引为2i+1,右子节点的索引为2i+2。 非叶子部分即为除去最后一层的所有节点。若树的深度为d,则最后一层的节点数量为2^(d-1)。所以非叶子部分的节点数量为2^(d-1)-1。 后序遍历的顺序为先遍历左子树,再遍历右子树,最后访问根节点。对于非叶子部分的后序遍历,我们只需要从根节点开始遍历到最后一个非叶子节点即可。 具体步骤为: 1. 根据非叶子节点数量,计算树的深度d。 2. 从根节点开始,遍历到索引为2^(d-1)-1的节点。 3. 对于每个节点,先递归遍历它的左子节点,再递归遍历它的右子节点。 4. 最后访问根节点。 以下是一个JavaScript的示例代码实现: ``` function postOrderTraversal(arr, nodeIndex) { const length = arr.length; if (nodeIndex >= length) { return; } // 左子节点的索引 const leftChild = 2 * nodeIndex + 1; // 右子节点的索引 const rightChild = 2 * nodeIndex + 2; // 先遍历左子节点 postOrderTraversal(arr, leftChild); // 再遍历右子节点 postOrderTraversal(arr, rightChild); // 访问根节点 console.log(arr[nodeIndex]); } const completeBinaryTree = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const lastNonLeafIndex = Math.floor((completeBinaryTree.length - 1) / 2) - 1; postOrderTraversal(completeBinaryTree, 0); ``` 以上代码中,我们定义了一个`postOrderTraversal`函数,传入的参数为数组表示的完全二叉树,以及当前节点的索引。函数先判断节点索引是否超出了数组的长度,如果是,则无需再进行遍历。接下来,我们计算左子节点和右子节点的索引,并先递归遍历它们,最后再输出当前节点的值。最后,我们传入根节点的索引0,即可得到非叶子部分的后序遍历结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值