#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef int DataType;
typedef struct BiTNode {
DataType data;
struct BiTNode *LChild;
struct BiTNode *RChild;
}BiTNode, *BiTree;
//**************************************************
//有关栈的部分
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef BiTNode* Elemtype;
typedef struct {
Elemtype data[MAXSIZE];
int top;
}SeqStack;
void InitStack(SeqStack *S)
{
S->top = -1;
}
int StackEmpty(SeqStack S) {
return S.top == -1;
}
int StackFull(SeqStack S)
{
return S.top == MAXSIZE - 1;
}
int PushStack(SeqStack *S, Elemtype e)
{
if (!StackFull(*S)) {
S->data[++S->top] = e;
return OK;
}
else
return ERROR;
}
int PopStack(SeqStack *S, Elemtype* x)
{
if (!StackEmpty(*S)) {
*x = S->data[S->top--];
return OK;
}
else {
printf("栈为空\n");
return ERROR;
}
}
int GetTop(SeqStack *S, Elemtype *x)
{
if (!StackEmpty(*S)) {
*x = S->data[S->top];
return OK;
}
else {
printf("栈为空!\n");
return ERROR;
}
}
//**************************************************************************************
//***************************************************************************************
//队列相关的代码
typedef BiTNode * QueueElemType;
typedef struct{
QueueElemType element[MAXSIZE];
int top;
int rear;
}SeqQueue;
void InitSeqQueue(SeqQueue *Q) {
Q->top = 0;
Q->rear = 0;
}
int IsEmpty(SeqQueue Q)
{
if (Q.rear == Q.top)
return 1;
else
return 0;
}
int IsFull(SeqQueue Q)
{
if ((Q.rear + 1) % MAXSIZE == Q.top)
return 1;
else
return 0;
}
int EnterQueue(SeqQueue *Q, QueueElemType x)
{
if ((Q->rear + 1) % MAXSIZE == Q->top)
return 0;
Q->element[Q->rear] = x;
Q->rear = (Q->rear + 1) % MAXSIZE;
return 1;
}
int DeleteQueue(SeqQueue *Q, QueueElemType *x)
{
if (Q->rear == Q->top)
return 0;
*x = Q->element[Q->top];
Q->top = (Q->top + 1) % MAXSIZE;
return 1;
}
//**************************************************************************************
//拓展先序创建二叉树
void CreateBiTree(BiTree *root)
{
int num;
scanf("%d", &num);
if (num == 9999) *root = NULL;
else {
*root = (BiTNode *)malloc(sizeof(BiTNode));
(*root)->data = num;
CreateBiTree(&((*root)->LChild));
CreateBiTree(&((*root)->RChild));
}
}
void visit(int data)
{
printf("%5d", data);
}
//先序遍历递归和非递归
void preOrder(BiTree root)
{
if (root != NULL) {
visit(root->data);
preOrder(root->LChild);
preOrder(root->RChild);
}
}
//利用栈来实现前序
void preOrderNR(BiTree root)
{
SeqStack s;
InitStack(&s);
BiTNode * p = root;
if (!p) return;
//只要左孩子不为空,就一直入栈,否则退栈访问右孩子
while (!StackEmpty(s) || p) {
if (p != NULL) {
visit( p->data);
PushStack(&s, p);
p = p->LChild;
}
else {
PopStack(&s, &p);
p = p->RChild;
}
}
}
//中序遍历递归和非递归
void InOrder(BiTree root)
{
if (root != NULL) {
InOrder(root->LChild);
visit(root->data);
InOrder(root->RChild);
}
}
void InOrderNR(BiTree root)
{
SeqStack s;
InitStack(&s);
BiTNode *p = root;
while (p!=NULL || !StackEmpty(s)) {
if (p != NULL) {
PushStack(&s, p);
p = p->LChild;
}
else {
PopStack(&s, &p);
visit(p->data);
p = p->RChild;
}
}
}
//后续遍历递归和非递归
void postOrder(BiTree root)
{
if (root != NULL) {
postOrder(root->LChild);
postOrder(root->RChild);
visit(root->data);
}
}
void postOrderNR(BiTree root)
{
SeqStack s;
InitStack(&s);
BiTNode * p = root, *pre = NULL;
while (!StackEmpty(s) || p) {
if (p != NULL) {
PushStack(&s, p);
p = p->LChild;
}else{
GetTop(&s, &p);
if (p->RChild == NULL || p->RChild == pre) {
PopStack(&s, &p);
visit(p->data);
pre = p;
p = NULL;
}
else {
p = p->RChild;
}
}
}
}
//求叶子结点个数
int CountLeaf(BiTree root)
{
if (root == NULL) return 0;
if (root->LChild == NULL && root->RChild == NULL)
return 1;
else
return CountLeaf(root->LChild) + CountLeaf(root->RChild);
}
int totalLeaf = 0;
void CountLeaf2(BiTree root)
{
if (root != NULL) {
if (!root->LChild && !root->RChild) {
totalLeaf++;
}
CountLeaf2(root->LChild);
CountLeaf2(root->RChild);
}
}
//求树的高度
int treeHeight(BiTree root)
{
int hl, hr, maxh;
if (root != NULL) {
hl = treeHeight(root->LChild);
hr = treeHeight(root->RChild);
maxh = (hl > hr ? hl: hr) + 1;
return maxh;
}
else
return 0;
}
int height = 0;
void treeHeight2(BiTree root, int h)
{
if (root != NULL) {
if (h > height) height = h;
treeHeight2(root->LChild, h + 1);
treeHeight2(root->RChild, h + 1);
}
}
//横向打印二叉树
void HorizontalPrint(BiTree root ,int layer)
{
if (root != NULL) {
HorizontalPrint(root->RChild, layer + 1);
for (int i = 0; i < layer; i++)
printf(" ");
printf("%d\n", root->data);
HorizontalPrint(root->LChild, layer + 1);
}
}
//层序遍历
int LayerOrder(BiTree root)
{
SeqQueue Q;
InitSeqQueue(&Q);
BiTNode * p = root;
if (p == NULL) return 0;
EnterQueue(&Q, p);
while (!IsEmpty(Q)) {
DeleteQueue(&Q, &p);
visit(p->data);
if (p->LChild)
EnterQueue(&Q, p->LChild);
if (p->RChild)
EnterQueue(&Q, p->RChild);
}
return 1;
}
//获取一个整数的数字个数
int CountDigit(int data)
{
string dataStr = to_string(data);
return dataStr.length() ;
}
typedef struct NewNode{
BiTNode * treePart;
int countDigit;
}NewNode;
//中序遍历获取序列
void InOrder2(BiTree root, string &InStr, vector<NewNode> &data) {
if (root != NULL) {
InOrder2(root->LChild, InStr,data);
InStr += to_string(root->data);
NewNode nn;
nn.treePart = root;
nn.countDigit = CountDigit(root->data);
data.push_back(nn);
InOrder2(root->RChild, InStr, data);
}
}
//竖形打印
void PrintVertically(BiTree root)
{
queue<BiTNode*> q;
if (root == NULL)
{
printf("\nThe tree is empty.\n");
return;
}
string InStr;
vector<NewNode> data;
InOrder2(root, InStr,data);
q.push(root);
while (!q.empty()) {
vector<BiTNode*> cache;
while (!q.empty()) {
cache.push_back(q.front());
q.pop();
}
string line = "";
for (int i = 0; i < InStr.length(); i++)
line += " ";
for (auto p : cache) {
if (p) {
int totalDigits = 0;
for (int i = 0; i < data.size(); i++) {
if (data[i].treePart == p)
break;
totalDigits += data[i].countDigit;
}
for (int i = totalDigits; i < totalDigits + CountDigit(p->data); i++)
line[i] = InStr[i];
if (p->LChild) q.push(p->LChild);
if (p->RChild) q.push(p->RChild);
}
}
cout << line << endl;
}
}
//查找根结点到目标结点的路径,并将其存储在stack中
void Path(BiTree root, BiTNode * r , stack<BiTNode *> &s)
{
if (root == NULL) return;
BiTNode *p = root, *q = NULL;
while (p != NULL || !s.empty()) {
if (p) {
s.push(p);
p = p->LChild;
}
else {
p = s.top();
if (p->RChild == NULL || p->RChild == q) {
if (p == r)
return;
q = p;
s.pop();
p = NULL;
}
else
p = p->RChild;
}
}
}
//将栈的元素反过来装入
stack<BiTNode*> ReverseStack(stack<BiTNode*> s)
{
stack<BiTNode*> s1;
while (!s.empty()) {
s1.push(s.top());
s.pop();
}
return s1;
}
//查找二叉树两结点的共同祖先
BiTNode * FindCommonAncestor(BiTree root, BiTNode *p, BiTNode *q) {
stack<BiTNode *> s1, s2;
Path(root, p, s1);
if (s1.empty()) {
printf("p指针不是二叉树中的有效指针\n");
return NULL;
}
s1 = ReverseStack(s1);
Path(root, q, s2);
if (s2.empty()) {
printf("q指针不是二叉树中的有效指针\n");
return NULL;
}
s2 = ReverseStack(s2);
BiTNode *pre = NULL;
while (!s1.empty() && !s2.empty()) {
if (s1.top() == s2.top()) {
pre = s1.top();
s1.pop();
s2.pop();
}
else
break;
}
return pre;
}
//计算非叶子结点的个数
int CountNoLeaf(BiTree root)
{
static int num = 0;
if (root != NULL) {
if (root->LChild != NULL || root->RChild != NULL)
num++;
CountNoLeaf(root->LChild);
CountNoLeaf(root->RChild);
}
return num;
}
void DelSubTree(BiTree &root)
{
if (root == NULL) return;
DelSubTree(root->LChild);
DelSubTree(root->RChild);
free(root);
}
//删去二叉树每一个以X为根结点的子树,并释放它们的空间
void DelXSubTree(BiTree &root, DataType x)
{
if (root != NULL) {
if (root->data == x) {
DelSubTree(root);
root = NULL;//删除后将被删除的引用改为NULL
}
else {
/*if (root->LChild && root->LChild->data == x)
root->LChild = NULL;
if (root->RChild && root->RChild->data == x)
root->RChild = NULL;*/
DelXSubTree(root->LChild, x);
DelXSubTree(root->RChild, x);
}
}
}
void DelXSubTree2(BiTree &root, DataType x)
{
if (root != NULL) {
if (root->LChild && root->LChild->data == x) {
DelSubTree(root->LChild);
root->LChild = NULL;
}
if (root->RChild && root->RChild->data == x) {
DelSubTree(root->RChild);
root->RChild = NULL;
}
if (root->data == x) {
DelSubTree(root);
root = NULL;
}
}
}
//判断一棵二叉树是否是正则二叉树
//正则二叉树是指在二叉树中不存在子树个数为1的结点
bool IsRegular = true;
void IsRegularTree(BiTree root)
{
if (root != NULL) {
bool flag1 = (root->LChild == NULL);
bool flag2 = (root->RChild == NULL);
if ((flag1 && !flag2) || (!flag1 && flag2))
IsRegular = false;
IsRegularTree(root->LChild);
IsRegularTree(root->RChild);
}
}
bool IsRegularTree2(BiTree root)
{
if (root == NULL)
return true;
else if (root->LChild == NULL && root->RChild == NULL)
return true;
else if (root->LChild == NULL || root->RChild == NULL)
return false;
else{
bool isRegularLeft = IsRegularTree2(root->LChild);
bool isRegularRight = IsRegularTree2(root->RChild);
return isRegularLeft && isRegularRight;
}
}
int CountOneNode(BiTree root)
{
static int num = 0;
if (root != NULL) {
if (root->LChild && !root->RChild || root->RChild && !root->LChild)
num++;
CountOneNode(root->LChild);
CountOneNode(root->RChild);
}
return num;
}
bool IsRegularTree3(BiTree root)
{
int numberOneNode = CountOneNode(root);
if (numberOneNode != 0)
return false;
else
return true;
}
//判断一棵树是不是完全二叉树
//判断方法:利用层序遍历,不过即使是空的孩子结点也要入队列;
//当从队列中取出一个空结点,其后续取出的所有结点必然也是空节点
int IsCompleteTree(BiTree root)
{
if (root == nullptr)
return 1;
queue<BiTNode*> q;
q.push(root);
BiTNode *p = root;
while (!q.empty() || p) {
p = q.front();
q.pop();
if (p) {
q.push(p->LChild);
q.push(p->RChild);
}
else {
while (!q.empty()) {
p = q.front();
q.pop();
if (p)
return 0;
}
}
}
return 1;
}
typedef struct {
BiTNode * data[MAXSIZE];
int level[MAXSIZE];
int rear, front;
}Qu;
//求一颗二叉树的最大宽度
//思路:利用队列层序遍历二叉树,并将结点指针和对应层号保存,
// 然后依次从队列中取出元素并计算每个层次的个数
// 取其最大值即可得到最大宽度
int MaxWidth(BiTree root)
{
if (root == NULL) {
printf("空树\n");
return 0;
}
Qu qu;
qu.rear = qu.front = -1;
//加入第一个结点
BiTNode * p = root;
qu.data[++qu.rear] = p;
qu.level[qu.rear] = 1;//对应于同一个下标,这里的qu.rear不用再自增1了
int curLevel;
//取出当前结点和其层次号,加入子结点并且层次号加1
while (qu.front < qu.rear) {
p = qu.data[++qu.front];
//这里的++front是逻辑上的出队列,但是结点指针和层次数据却已经保存下来了
curLevel = qu.level[qu.front];
if (p->LChild) {
qu.data[++qu.rear] = p->LChild;
qu.level[qu.rear] = curLevel + 1;
}
if (p->RChild) {
qu.data[++qu.rear] = p->RChild;
qu.level[qu.rear] = curLevel + 1;
}
}
//遍历队列的level数组,依次统计各个层次的个数,找到其中的最大值以及层次总数
int layer = 1, maxWidth = 0, count = 0;
int curLayerNum;
while (count <= qu.rear) {
curLayerNum = 0;
while (count <= qu.rear && qu.level[count] == layer) {
count++;
curLayerNum++;
}
layer = qu.level[count];
if (curLayerNum > maxWidth)
maxWidth = curLayerNum;
}
return maxWidth;
}
//采用递归算法,交换一棵树所有结点的所有子树
//前中后序均可以
void SwapChildren(BiTree root)
{
if (root != NULL) {
SwapChildren(root->LChild);
SwapChildren(root->RChild);
BiTNode * temp = root->LChild;
root->LChild = root->RChild;
root->RChild = temp;
}
}
//计算一个给定二叉树双分支结点的个数
int Count2BranchNode(BiTree root)
{
if (root == NULL)
return 0;
else if (root->LChild && root->RChild)
return Count2BranchNode(root->LChild) + Count2BranchNode(root->RChild) + 1;
else
return Count2BranchNode(root->LChild) + Count2BranchNode(root->RChild);
}
//使用非递归算法查找后续遍历的第一个结点
BiTNode * LDRFirstNode(BiTree root)
{
if (root == NULL) {
printf("空树,不存在第一个结点\n");
return NULL;
}
BiTNode * p = root;
while (p != NULL && (p->LChild != NULL || p->RChild != NULL)) {
if (p->LChild != NULL)
p = p->LChild;
else
p = p->RChild;
}
return p;
}
//将二叉树的叶子结点串联成一个单链表,表头指针为head
BiTNode *pre = NULL;
BiTNode * head = NULL;
void LeafLink(BiTree root)
{
if (root != NULL) {
if (root->LChild == NULL && root->RChild == NULL) {
if (pre == NULL)
head = root;
else
pre->RChild = root;
pre = root;
}
LeafLink(root->LChild);
LeafLink(root->RChild);
}
}
//设有一个满二叉树(所有结点的值均不相同),已知其先序序列为pre,求其后序序列
void GetPostSeqFromPre(int preSeq[], int low, int high, vector<int> &postSeq)//默认数组从0到n-1存取
{
int rootIndex = low, halfLen = (high - low) / 2;
if (low < high) {
GetPostSeqFromPre(preSeq, low + 1, low + halfLen, postSeq);
GetPostSeqFromPre(preSeq, low + halfLen + 1, high, postSeq);
}
postSeq.push_back(preSeq[rootIndex]);
}
//根据先序序列和中序序列创建二叉树
void CreateTreePreMid(BiTree *root , DataType pre[] , int l1, int h1, DataType mid[], int l2, int h2)
{
if (l1 <= h1) {
*root = (BiTNode *)malloc(sizeof(BiTNode));
(*root)->data = pre[l1];
int midIndex = 0;
for (int i = l2; i <= h2; i++)
if (mid[i] == pre[l1])
midIndex = i;
int leftLen = midIndex - l2, rightLen = h2 - midIndex;
CreateTreePreMid(&((*root)->LChild), pre, l1 + 1, l1 + leftLen, mid, l2, l2 + leftLen - 1);
CreateTreePreMid(&((*root)->RChild), pre, h1 - rightLen + 1, h1, mid, h2 - rightLen + 1, h2);
}
else {
*root = NULL;
}
}
void CreateTreePreMid2(BiTree &root, DataType pre[], int l1, int h1, DataType mid[], int l2, int h2)
{
if (l1 <= h1) {
root = (BiTNode *)malloc(sizeof(BiTNode));
root->data = pre[l1];
int midIndex = 0;
for (int i = l2; i <= h2; i++)
if (mid[i] == pre[l1])
midIndex = i;
int leftLen = midIndex - l2, rightLen = h2 - midIndex;
CreateTreePreMid2(root->LChild, pre, l1 + 1, l1 + leftLen, mid, l2, l2 + leftLen - 1);
CreateTreePreMid2(root->RChild, pre, h1 - rightLen + 1, h1, mid, h2 - rightLen + 1, h2);
}
else {
root = NULL;
}
}
//根据中序序列和后序序列创建二叉树
void CreateTreeMidPost(BiTree &root, DataType mid[], int l1, int h1, DataType post[], int l2, int h2)
{
if (l1 <= h1) {
root = (BiTNode *)malloc(sizeof(BiTNode));
root->data = post[h2];
int midIndex = 0;
for (int i = l1; i <= h1; i++)
if (mid[i] == post[h2])
midIndex = i;
int leftLen = midIndex - l1;
int rightLen = h1 - midIndex;
CreateTreeMidPost(root->LChild, mid, l1, midIndex - 1, post, l2, l2 + leftLen - 1);//注意这部分前面不要写错l和h
CreateTreeMidPost(root->RChild, mid, midIndex + 1, h1, post, l2 +leftLen, h2-1);
}
else {
root = NULL;//一定记的要置为NULL
}
}
//根据层序和中序序列建立二叉树
//层序号可以确认第一个是根结点,然后根据中序序列可以得到左子树和右子树的结点分别是什么,
//然后再层序遍历中依次判断其在中序对应的左子树还是右子树中,将其按照左右子树分开,
//按照顺序依次连接就可以得到左子树和右子树的层序遍历序列
//创建新结点的函数
BiTree NewBiTNode(DataType val)
{
BiTNode * newBiTNode = new BiTNode;
newBiTNode->data = val;
newBiTNode->LChild = NULL;
newBiTNode->RChild = NULL;
return newBiTNode;
}
void CreateFromLevelInOrder(BiTree &root, vector<DataType> layer, DataType mid[], int midL, int midH)
{
if (layer.size() == 0) {
root = NULL; //表示当子树没有左孩子或者右孩子时,leftLayer或者rightLayer为空,将对应的指针域设置为NULL
}
else {
root = NewBiTNode(layer[0]);
int midIndex = 0;
for (int i = midL; i <= midH; i++) {
if (mid[i] == layer[0]) {
midIndex = i;
break;
}
}
//分别求出左右子树的层序遍历序列
vector<DataType> leftLayer;
vector<DataType> rightLayer;
for (int i = 1; i < layer.size(); i++) {
bool isLeft = false;
for (int j = midL; j < midIndex; j++) {
if (mid[j] == layer[i]) {
isLeft = true;
break;
}
}
if (isLeft)
leftLayer.push_back(layer[i]);
else
rightLayer.push_back(layer[i]);
}
CreateFromLevelInOrder(root->LChild, leftLayer, mid, midL, midIndex - 1);
CreateFromLevelInOrder(root->RChild, rightLayer, mid, midIndex + 1, midH);
}
}
//给出二叉树自下到上、从右至左的层次遍历算法
void ReverseLayerOrder(BiTree root, stack<BiTNode*> & reverseLayer)
{
if (root == NULL) return;
const int maxn = 30;
BiTNode * qu[maxn];
int rear = -1, front = -1;
qu[++rear] = root;
BiTNode * p = root;
while (front < rear ) {//终止条件是队列为空,而不包括p不为NULL, p || 栈不为空 是后序非递归遍历的条件,不要记错了
p = qu[++front];
reverseLayer.push(p);
if (p->LChild)
qu[++rear] = p->LChild;
if (p->RChild)
qu[++rear] = p->RChild;
}
}
//设计一个非递归算法求二叉树的高度
// 利用层次遍历来计算
typedef struct {
Elemtype data[MAXSIZE];
int rear, front;
}MyQueue;
int TreeHeightNR(BiTree root)
{
if (!root)
return 0;
//设置一个变量来保存当前高度,每次结点出队列放入其子孩子结点其高度要加一
MyQueue q;
q.front = q.rear = -1;
q.data[++q.rear] = root;
BiTNode * p = NULL;
int countLayerNodes = 1, layerCount = 0, count ;
while (q.front < q.rear) {
int i = 0;
count = 0;
while (i < countLayerNodes) {
p = q.data[++q.front];
if (p->LChild) {
q.data[++q.rear] = p->LChild;
count++;
}
if (p->RChild) {
q.data[++q.rear] = p->RChild;
count++;
}
i++;
}
layerCount++;
countLayerNodes = count;
}
return layerCount;
}
//方法2::层序遍历的同时设置其layer,则最后一个结点的layer为树的高度
typedef struct {
Elemtype data[MAXSIZE];
int level[MAXSIZE];
int rear, front;
}LevelQu;
int TreeHeightNR2(BiTree root)
{
if (!root) return 0;
LevelQu qu;
qu.rear = qu.front = -1;
BiTNode *p = root;
int height = 0;
qu.data[++qu.rear] = root;
qu.level[qu.rear] = 1;
while (qu.front < qu.rear) {
p = qu.data[++qu.front];
height = qu.level[qu.front];
if (p->LChild) {
qu.data[++qu.rear] = p->LChild;
qu.level[qu.rear] = height + 1;
}
if (p->RChild) {
qu.data[++qu.rear] = p->RChild;
qu.level[qu.rear] = height + 1;
}
}
return height;
}
//二叉树的路径长度指的是所有结点的路径长度之和
//结点的路径长度等于结点层次减一
int pathLen = 0;
void PathLen(BiTree root, int layer)
{
if (root != NULL) {
pathLen += (layer - 1);
PathLen(root->LChild, layer + 1);
PathLen(root->RChild, layer + 1);
}
}
int main()
{
/*printf("%d\n", CountDigit(123));
printf("%d\n", CountDigit(12334));*/
//BiTree root;
//CreateBiTree(&root);//355 65 9999 7 9999 9999 12666 355 9999 18 9999 9999 1688888888 9999 9999
//
//printf("\nPreOrder :\n");
//preOrder(root);
//printf("\n");
//preOrderNR(root);
//printf("\nInOrder :\n");
//InOrder(root);
//printf("\n");
//InOrderNR(root);
//printf("\nPostOrder: \n");
//postOrder(root);
//printf("\n");
//postOrderNR(root);
//printf("\nLayer Order :\n");
//LayerOrder(root);
//printf("\nThe 横向打印:\n");
//HorizontalPrint(root,1);
//treeHeight2(root, 1);
//printf("\n The height : %d, %d\n", treeHeight(root), height);
//CountLeaf2(root);
//printf("\n The leaf's count is: %d, %d\n", CountLeaf(root), totalLeaf);
//PrintVertically(root);
//BiTNode *nearestAncestor = FindCommonAncestor(root, NULL, root->RChild);
//printf("Ancestor is NULL? %d\n", nearestAncestor == NULL);
//nearestAncestor = FindCommonAncestor(root, root->RChild->RChild, root->RChild->LChild->RChild);
//printf("Ancestor is :%d\n", nearestAncestor->data);
//printf("\nThe number of non leaf is %d\n", CountNoLeaf(root));
//IsRegularTree(root);
//printf("1.Tree is reguar tree?%d\n", IsRegular);
//printf("2.Tree is reguar tree?%d\n", IsRegularTree2(root));
//printf("3.Tree is reguar tree?%d\n", IsRegularTree3(root));
//DelXSubTree(root, 355);
//printf("\n Deleting SubTree of %d :\n", 355);
//PrintVertically(root);
//BiTree root2;
//printf("Tree2 creating\n");
//CreateBiTree(&root2);//15 42 9999 9999 36 9999 9999 //90 23 45 9999 9999 9999 32 71 9999 9999 66 78 9999 9999 79 9999 9999
//PrintVertically(root2);
//printf("Tree root2 is a complete binary tree? %d\n", IsCompleteTree(root2));
//printf("The maximum width of root2 is :%d\n", MaxWidth(root2));
//
//SwapChildren(root2);
//PrintVertically(root2);
//printf("The number of 2 branches node:%d\n", Count2BranchNode(root2));
//printf("The first node of the post sequence: %d\n", LDRFirstNode(root2)->data);
//LeafLink(root2);
//BiTNode * p = head;
//while (p) {
// printf("%d, \n", p->data);
// p = p->RChild;
//}
//vector<int> postSeq;
//int seqNum = 0;
//printf("请输入已知的完全二叉树的先序序列的结点个数:\n");
//scanf("%d", &seqNum);
//int * preSeq = new int[seqNum];
//printf("请输入已知的完全二叉树的先序序列:\n");
//for (int i = 0; i < seqNum; i++) {
// cin >> preSeq[i];
//}
//GetPostSeqFromPre(preSeq, 0, seqNum-1, postSeq);
//printf("后序序列是:\n");
//for (vector<int>::iterator it = postSeq.begin(); it != postSeq.end(); it++) {
// printf("%5d", (*it));
//}
//DataType pre[] = { 1,32,45,16, 13, 6 }, mid[] = { 32, 45,1, 13, 16, 6 };
//BiTree root3;
//CreateTreePreMid2(root3, pre, 0, 5, mid, 0,5);
//PrintVertically(root3);
//DataType post[] = { 45, 32, 13, 6, 16, 1 };
//BiTree root4;
//CreateTreeMidPost(root4, mid, 0, 5, post, 0, 5);
//PrintVertically(root4);
const int maxn = 40;
DataType mid[maxn];
vector<DataType> layer;
int n;
scanf("%d", &n);//5
int temp;
for (int i = 0; i < n; i++) {
scanf("%d", &temp);
layer.push_back(temp); //12 13 14 18 15
}
for (int i = 0; i < n; i++) {
scanf("%d", &temp);
mid[i] = temp;// 13 18 12 15 14
}
BiTree root4;
CreateFromLevelInOrder(root4, layer, mid, 0, n-1);
PrintVertically(root4);
printf("树高为(使用非递归算法1):%d\n", TreeHeightNR(root4));
printf("树高为(使用非递归算法2):%d\n", TreeHeightNR2(root4));
stack<BiTNode*> reverseLayer;
BiTNode * p = NULL;
ReverseLayerOrder(root4, reverseLayer);
while (!reverseLayer.empty()) {
p = reverseLayer.top();
printf("%5d", p->data);
reverseLayer.pop();
}
PathLen(root4, 1);
printf("树的路径长度是:%d\n", pathLen);
}
二叉链表存储树
最新推荐文章于 2025-03-24 11:51:57 发布