编程算法 - 数组构造二叉树并打印

数组构造二叉树并打印

本文地址: http://blog.csdn.net/caroline_wendy

数组:

构造二叉树, 需要使用两个队列(queue), 保存子节点和父节点, 并进行交换;

打印二叉树, 需要使用两个队列(queue), 依次打印父节点和子节点, 并进行交换;

二叉树的数据结构:

struct BinaryTreeNode {
	int m_nValue;
	BinaryTreeNode* m_pParent;
	BinaryTreeNode* m_pLeft;
	BinaryTreeNode* m_pRight;
};

代码:

/*
 * main.cpp
 *
 *  Created on: 2014.6.12
 *      Author: Spike
 */

/*eclipse cdt, gcc 4.8.1*/

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

struct BinaryTreeNode {
	int m_nValue;
	BinaryTreeNode* m_pParent;
	BinaryTreeNode* m_pLeft;
	BinaryTreeNode* m_pRight;
};

void printTree (BinaryTreeNode* tree)
{
	BinaryTreeNode* node = tree;
	std::queue<BinaryTreeNode*> temp1;
	std::queue<BinaryTreeNode*> temp2;

	temp1.push(node);

	while (!temp1.empty())
	{
		node = temp1.front();
		if (node->m_pLeft != NULL) {
			temp2.push(node->m_pLeft);
		}

		if (node->m_pRight != NULL) {
			temp2.push(node->m_pRight);
		}

		temp1.pop();

		std::cout << node->m_nValue  << " ";

		if (temp1.empty())
		{
			std::cout << std::endl;
			temp1 = temp2;
			std::queue<BinaryTreeNode*> empty;
			std::swap(temp2, empty);
		}
	}
}

BinaryTreeNode* buildTree (const std::vector<int>& L)
{
	if (L.empty()) return nullptr;

	std::queue<BinaryTreeNode*> parentQueue;
	std::queue<BinaryTreeNode*> childQueue;

	BinaryTreeNode* root = new BinaryTreeNode();
	root->m_nValue = L[0];

	parentQueue.push(root);

	std::size_t times = 1;
	while (times < L.size())
	{
		BinaryTreeNode* parent = parentQueue.front();
		parentQueue.pop();

		BinaryTreeNode* lchild = new BinaryTreeNode();
		lchild->m_nValue = L[times];
		lchild->m_pLeft = nullptr;
		lchild->m_pRight = nullptr;
		++times;

		parent->m_pLeft = lchild;
		lchild->m_pParent = parent;
		childQueue.push(lchild);

		if (times == L.size()) break;

		BinaryTreeNode* rchild = new BinaryTreeNode();
		rchild->m_nValue = L[times];
		rchild->m_pLeft = nullptr;
		rchild->m_pRight = nullptr;
		++times;

		parent->m_pRight = rchild;
		rchild->m_pParent = parent;
		childQueue.push(rchild);

		if (parentQueue.empty()) {
			parentQueue = childQueue;
			std::queue<BinaryTreeNode*> empty;
			std::swap(childQueue, empty);
		}
	}

	return root;
}

int main (void)
{
	std::vector<int> L = {49, 38, 65, 97, 76, 13, 27, 49};
	BinaryTreeNode* tree = buildTree(L);
	printTree(tree);
	return 0;
}

输出:

49
38 65
97 76 13 27
49

编程算法 - 数组构造二叉树并打印

时间: 2024-12-14 06:48:37

编程算法 - 数组构造二叉树并打印的相关文章

编程算法 - 数组中的逆序对 代码(C)

数组中的逆序对 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 在数组中的两个数字如果前面一个数字大于后面的数字, 则这两个数字组成一个逆序对. 输入一个数组, 求出这个数组中的逆序对的总数. 使用归并排序的方法, 辅助空间一个排序的数组, 依次比较前面较大的数字, 算出整体的逆序对数, 不用逐个比较. 时间复杂度: O(nlogn) 代码: /* * main.cpp * * Created on: 2014.6.12 * Author:

编程算法 - 数组中只出现一次的数字 代码(C)

数组中只出现一次的数字 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 一个整型数组里除了两个数字以外, 其他的数字都出现了两次. 请写程序找出这两个只出现一次的数字. 如果从头到尾依次异或数组中的每一个数字, 那么最终的结果刚好是那个只出现一次的数字. 根据结果数组二进制某一位为1, 以此分组, 为1的一组, 为0的一组, 再重新进行异或. 最后得出两个结果. 时间复杂度O(n). 代码: /* * main.cpp * * Create

编程算法 - 数组中出现次数超过一半的数字 代码(C)

数组中出现次数超过一半的数字 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 数组中有一个数字出现的次数超过数组长度的一半, 请找出这个数字. 1. 使用高速排序(QuickSort)的方法, 把中值(middle)和索引(index)匹配, 输出中值, 并检測是否符合要求. 2. 使用计数方法依次比較. 代码:  方法1: /* * main.cpp * * Created on: 2014.6.12 * Author: Spike */

构造二叉树

二叉树是一种特殊的树形结构,每个节点最多有两个子节点,每个两个节点有左右之分,次序不能颠倒.一般使用递归来定义二叉树,因此与二叉树相关的问题都可以通过递归来解决.接下来介绍根据已知的二叉树结构,构造二叉树的方法.首先给出两个二叉树的结构,如下图所示: 1)根据已有的二叉树结构,生成节点数组,依据节点数组构造二叉树时.约定输入的正数表示其节点编号,负数表示节点不存在.从根节点开始,构造其左子树,如果此树上还有左子树,继续操作,直至没有左子树,然后构造右子树.以A为例说明生成节点数组的过程,设节点数

编程算法 - 把数组排成最小的数 代码(C)

把数组排成最小的数 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一个正整数数组, 把数组里所有数字拼接起来排成一个数, 打印能拼接出的所有数字中最小的一个. 大数转换为字符串, 重载快速排序的比较方法, 进行排序, 最后拼接. 代码: /* * main.cpp * * Created on: 2014.6.12 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <stdi

编程算法 - 数字在排序数组中出现的次数 代码(C)

数字在排序数组中出现的次数 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 统计一个数字在排序数组中出现的次数. 通过折半查找, 找到首次出现的位置, 再找到末次出现的位置, 相减即可. 时间复杂度O(logn). 代码: /* * main.cpp * * Created on: 2014.6.12 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <stdio.h> #inc

编程算法 - 二叉树的深度 代码(C)

二叉树的深度 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一棵二叉树的根节点, 求该树的深度. 依次选择最深的左右子树, 然后递归加1. 代码: /* * main.cpp * * Created on: 2014.6.12 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <stdio.h> #include <stdlib.h> #include <

编程算法 - 判断二叉树是不是平衡树 代码(C)

判断二叉树是不平衡树 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一颗二叉树的根结点, 判断该树是不是平衡二叉树. 二叉平衡树: 任意结点的左右子树的深度相差不超过1. 使用后序遍历的方式, 并且保存左右子树的深度, 进行比较. 代码: /* * main.cpp * * Created on: 2014.6.12 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <std

【LeetCode-面试算法经典-Java实现】【105-Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)】

[105-Construct Binary Tree from Preorder and Inorder Traversal(通过前序和中序遍历构造二叉树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the