c++二叉树按层序遍历顺序输入(由上到下),先序遍历顺序输出,求完整代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++二叉树按层序遍历顺序输入(由上到下),先序遍历顺序输出,求完整代码相关的知识,希望对你有一定的参考价值。
参考技术A #include <stdlib.h>#include <vector>
using namespace std;
struct BTreeNode
int value;
BTreeNode* left;
BTreeNode* right;
BTreeNode(int& value)
left = right = NULL; this->value = value;
;
struct BTree
BTree()
head = NULL;
typedef void (*VFunc)(BTreeNode*, void*);
void visitFront(VFunc func, void* data)
if (head != NULL) VisitFront(head, func, data);
static void VisitFront(BTreeNode* node, VFunc func, void* data)
func(node, data);
if (node->left!= NULL) VisitFront(node->left, func, data);
if (node->right!= NULL) VisitFront(node->right, func, data);
void visitMiddle(VFunc func, void* data)
if (head != NULL) VisitMiddle(head, func, data);
static void VisitMiddle(BTreeNode* node, VFunc func, void* data)
if (node->left!= NULL) VisitMiddle(node->left, func, data);
func(node, data);
if (node->right!= NULL) VisitMiddle(node->right, func, data);
void visitBack(VFunc func, void* data)
if (head != NULL) VisitBack(head, func, data);
static void VisitBack(BTreeNode* node, VFunc func, void* data)
if (node->left!= NULL) VisitBack(node->left, func, data);
if (node->right!= NULL) VisitBack(node->right, func, data);
func(node, data);
static void VisitSum(BTreeNode*, void* data)
*(int*)data += 1;
int count()
int nodeCount = 0;
visitFront(VisitSum, &nodeCount);
return nodeCount;
void visitLayer(VFunc func, void* data)
if (head == NULL) return;
vector<BTreeNode*> nodes;
nodes.resize(count());
int i, last = 1;
nodes[0] = head;
for (i = 0; i < int(nodes.size()); i++)
func(nodes[i], data);
if (nodes[i]->left)
nodes[last++] = nodes[i]->left;
if (nodes[i]->right)
nodes[last++] = nodes[i]->right;
static void VisitClear(BTreeNode* node, void*)
delete node;
void clear()
visitBack(VisitClear, NULL);
head = NULL;
static int FindAt(int* arr, int b, int e, int& v)
for (int i = b; i < e; i++)
if (arr[i] == v) return i;
return -1;
bool buildFM(int* f, int *m, int n)
clear();
if (n == 0) return true;
int im = FindAt(m, 0, n, f[0]);
if (im == -1) return false;
head = new BTreeNode(f[0]);
int ifront = 1;
BuildFM(head, f, ifront, n, im, m, 0, n);
return (ifront == n);
static void BuildFM(BTreeNode* node,
int* f, int& ifront, int nfront,
int im, int* m, int bm, int em)
if (ifront == nfront) return;
int il = FindAt(m, bm, im, f[ifront]);
if (il != -1)
node->left = new BTreeNode(f[ifront]);
ifront += 1;
BuildFM(node->left, f, ifront, nfront, il, m, bm, im);
int ir = FindAt(m, im+1, em, f[ifront]);
if (ir != -1)
node->right = new BTreeNode(f[ifront]);
ifront += 1;
BuildFM(node->right, f, ifront, nfront, ir, m, im+1, em);
bool buildFB(int* f, int *b, int n)
// can't build because the left-right child can't be resolved
return false;
bool buildFL(int* f, int *l, int n)
// can't build because the left-right child can't be resolved
return false;
bool buildMB(int* m, int *b, int n)
clear();
if (n == 0) return true;
int iback = n-1;
int im = FindAt(m, 0, n, b[iback]);
if (im == -1) return false;
head = new BTreeNode(b[iback]);
iback -= 1;
BuildMB(head, b, iback, im, m, 0, n);
return (iback == -1);
static void BuildMB(BTreeNode* node,
int* b, int& iback,
int im, int* m, int bm, int em)
if (iback == -1) return;
int ir = FindAt(m, im+1, em, b[iback]);
if (ir != -1)
node->right = new BTreeNode(b[iback]);
iback -= 1;
BuildMB(node->right, b, iback, ir, m, im+1, em);
int il = FindAt(m, bm, im, b[iback]);
if (il != -1)
node->left = new BTreeNode(b[iback]);
iback -= 1;
BuildMB(node->left, b, iback, il, m, bm, im);
bool buildML(int* m, int *l, int n)
clear();
if (n == 0) return true;
int im = FindAt(m, 0, n, l[0]);
if (im == -1) return false;
struct NodeInfo
int im;
int b, e;
BTreeNode* node;
;
vector<NodeInfo> ni;
ni.resize(n);
head = new BTreeNode(l[0]);
ni[0].b = 0;
ni[0].e = n;
ni[0].im = im;
ni[0].node = head;
int last = 1;
for (int i = 0; i < last; i++)
int next = last;
if (next < n)
int il = FindAt(m, ni[i].b, ni[i].im, l[next]);
if (il != -1)
ni[last].node = new BTreeNode(l[next]);
ni[i].node->left = ni[last].node;
ni[last].b = ni[i].b;
ni[last].im = il;
ni[last].e = ni[i].im;
last += 1;
next += 1;
if (next < n)
int ir = FindAt(m, ni[i].im+1, ni[i].e, l[next]);
if (ir != -1)
ni[last].node = new BTreeNode(l[next]);
ni[i].node->right = ni[last].node;
ni[last].b = ni[i].im+1;
ni[last].im = ir;
ni[last].e = ni[i].e;
last += 1;
return (last == n);
bool buildBL(int* b, int *l, int n)
// can't build because the left-right child can't be resolved
return false;
BTreeNode* head;
;
#include <stdio.h>
#include <stdlib.h>
void IterOutput(BTreeNode* node, void*)
printf("%c ", node->value);
void VisitTree(BTree& bt)
bt.visitFront(IterOutput, NULL);
printf("\n");
bt.visitMiddle(IterOutput, NULL);
printf("\n");
bt.visitBack(IterOutput, NULL);
printf("\n");
bt.visitLayer(IterOutput, NULL);
printf("\n");
int main(int argc, char* args[])
BTree bt;
int af[] = 'a','b','e','f','c','g';
int am[] = 'e','b','f','a','g','c';
int ab[] = 'e','f','b','g','c','a';
int al[] = 'a','b','c','e','f','g';
int length = 6;
bt.buildFM(af, am, length);
VisitTree(bt);
printf("\n");
bt.buildMB(am, ab, length);
VisitTree(bt);
printf("\n");
bt.buildML(am, al, length);
VisitTree(bt);
printf("\n");
::system("pause");
return 0;
追问
代码没这么长啊
层序输入是哪个代码
参考技术B .. 确实, 召唤大神8698追问什么啊……
急需代码啊
力扣 - 102二叉树的层序遍历(剑指Offer - 面试题32:从上到下打印二叉树)
题目
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
示例:
二叉树:[3,9,20,null,null,15,7],
3
/ \\
9 20
/ \\
15 7
输出层序遍历的结果
3 9 20 15 7
分析
迭代法
用一个队列来存储当前层数的节点地址,每次从队列头部取出一个节点,然后判断是否为NULL,若不为空则输出当前节点,并把左右子节点存入队列尾部。直到队列中没有元素为止。因为这里拿c语言实现,所以用数组代替队列,
C
#include<stdio.h>
#include<assert.h>
#include<stdbool.h>
#include<stdlib.h>
#define MAXSIZE 100 /* 存储空间初始分配量 */
typedef int TElemType; /* 树结点的数据类型,目前暂定为整型 */
typedef struct node
{
TElemType data;
struct node* lchild, * rchild;//左右孩纸指针
}BTree;
void CreatTree(BTree** T)//构造二叉树
{
TElemType n = 0;
scanf("%d", &n);
if (999 != n)
{
(*T) = (BTree*)malloc(sizeof(BTree));
assert(NULL != *T);
(*T)->data = n;
CreatTree(&((*T)->lchild));
CreatTree(&((*T)->rchild));
}
else
{
*T = NULL;
}
}
//层序遍历 迭代法
void sequenceTraversal(BTree* T)
{
assert(NULL != T);
BTree* queue[MAXSIZE] = { 0 };
queue[0] = T;
int end = 1;
int start = 0;
while (start < end)
{
T = queue[start];
start++;
if (T != NULL)
{
printf("%d ", T->data);
queue[end++] = T->lchild;
queue[end++] = T->rchild;
}
}
}
int main()
{
BTree* root = NULL;
printf("请按照先序的规则输入数据:\\n");
CreatTree(&root);
printf("层序遍历输出:\\n");
sequenceTraversal(root);
printf("\\n");
return 0;
}
本章完!
以上是关于c++二叉树按层序遍历顺序输入(由上到下),先序遍历顺序输出,求完整代码的主要内容,如果未能解决你的问题,请参考以下文章