typedef struct BTNode{
char val;
struct BTNode *lChild, *rChild;
}BTNode, *BTree;
BTree CreatBTreePre(){
char ch;
scanf("%c", &ch);
BTree root;
if (ch == '#')
root = NULL;
else{
root = (BTree)malloc(sizeof(BTree));
root->val = ch;
root->lChild = CreatBTreePre();
root->rChild = CreatBTreePre();
}
return root;
}
void PreOrderRe(BTree root){
if (!root)
return;
printf("%c ", root->val);
PreOrderRe(root->lChild);
PreOrderRe(root->rChild);
}
void InOrderRe(BTree root){
if (!root)
return;
InOrderRe(root->lChild);
printf("%c ", root->val);
InOrderRe(root->rChild);
}
void PostOrderRe(BTree root){
if (!root)
return;
PostOrderRe(root->lChild);
PostOrderRe(root->rChild);
printf("%c ", root->val);
}
void PreOrderNotRe(BTree root){
Stack *s = (Stack *)malloc(sizeof(Stack));
InitStack(s);
BTree ptr = root;
while (ptr || !isEmpty(s)){
if (ptr){
printf("%c ", ptr->val);
Push(s, ptr);
ptr = ptr->lChild;
} else{
Pop(s, &ptr);
ptr = ptr->rChild;
}
}
}
void InOrderNotRe(BTree root){
Stack *s = (Stack *)malloc(sizeof(Stack));
InitStack(s);
BTree ptr = root;
while (ptr || !isEmpty(s)){
if (ptr){
Push(s, ptr);
ptr = ptr->lChild;
} else{
Pop(s, &ptr);
printf("%c ", ptr->val);
ptr = ptr->rChild;
}
}
}
void PostOrderNotRe(BTree root){
Stack *s = (Stack *)malloc(sizeof(Stack));
InitStack(s);
BTree ptr = root;
BTree pre = NULL;
while (ptr || !isEmpty(s)){
if (ptr){
Push(s, ptr);
ptr = ptr->lChild;
} else{
getTop(s, &ptr);
if (ptr->rChild && ptr->rChild != pre)
ptr = ptr->rChild;
else{
Pop(s, &ptr);
printf("%c ", ptr->val);
pre = ptr;
ptr = NULL;
}
}
}
}
void LevelOrderBTree(BTree root){
Queue *q = (Queue *)malloc(sizeof(Queue));
BTree ptr;
InitQueue(q);
JoinQueue(q, root);
while (!IsEmpty(q)){
LeaveQueue(q, &ptr);
printf("%c ", ptr->val);
if (ptr->lChild)
JoinQueue(q, ptr->lChild);
if (ptr->rChild)
JoinQueue(q, ptr->rChild);
}
}
BTree PreAndInCreatBTree(char *preOrder, int preStart, int preEnd,
char *inOrder, int inStart, int inEnd){
if (preStart > preEnd || inStart > inEnd)
return NULL;
BTree root = (BTree)malloc(sizeof(BTree));
root->val = preOrder[preStart];
root->lChild = NULL;
root->rChild = NULL;
int index = inStart;
for (; index <= inEnd; index++)
if (inOrder[index] == preOrder[preStart])
break;
root->lChild = PreAndInCreatBTree(preOrder, preStart+1, preStart + index - inStart, inOrder, inStart, index - 1);
root->rChild = PreAndInCreatBTree(preOrder, preStart + index - inStart + 1, preEnd, inOrder,index + 1, inEnd);
return root;
}
BTree InAndPostCreatBTree(char *inOrder, int inStart, int inEnd,
char *postOrder, int postStart, int postEnd){
if (inStart > inEnd || postStart > postEnd)
return NULL;
BTree root = (BTree)malloc(sizeof(BTree));
root->val = postOrder[postEnd];
root->lChild = NULL;
root->rChild = NULL;
int index = inStart;
for (; index <= inEnd; index++)
if (inOrder[index] == postOrder[postEnd])
break;
root->lChild = InAndPostCreatBTree(inOrder, inStart, index - 1, postOrder, postStart, postStart + index - inStart - 1);
root->rChild = InAndPostCreatBTree(inOrder, index + 1, inEnd, postOrder, postStart + index - inStart, postEnd - 1);
return root;
}
void ReverseLever(BTree root){
Queue *q = (Queue*)malloc(sizeof(Queue));
Stack *s = (Stack*)malloc(sizeof(Stack));
BTree ptr;
InitStack(s);
InitQueue(q);
JoinQueue(q, root);
while (!IsEmpty(q)){
LeaveQueue(q, &ptr);
Push(s, ptr);
if (ptr->lChild)
JoinQueue(q, ptr->lChild);
if (ptr->rChild)
JoinQueue(q, ptr->rChild);
}
while (!isEmpty(s)){
BTree *dis = (BTree *)malloc(sizeof(BTree));
getTop(s, dis);
printf("%c ", (*dis)->val);
Pop(s, &root);
free(dis);
}
}
int GetHight(BTree root){
if (!root) return 0;
if (!root->lChild && !root->rChild) return 1;
int hight = 0;
Queue *q = (Queue*)malloc(sizeof(Queue));
InitQueue(q);
JoinQueue(q, root);
while (!IsEmpty(q)){
hight++;
int index = q->rear - q->front;
BTree node = NULL;
for (int i = 0; i < index; ++i) {
LeaveQueue(q, &node);
if (node->lChild || node->rChild){
if (node->lChild)
JoinQueue(q, node->lChild);
if (node->rChild)
JoinQueue(q, node->rChild);
}
}
}
return hight;
}
_Bool IsCompleteBinaryTree(BTree root){
if (!root) return 1;
_Bool leaf = 0;
Queue *q = (Queue *)malloc(sizeof(Queue));
InitQueue(q);
JoinQueue(q, root);
while (!IsEmpty(q)){
BTree node = NULL;
LeaveQueue(q, &node);
if ((leaf && (node->lChild || node->rChild)) || (!node->lChild && node->rChild))
return 0;
if (node->lChild)
JoinQueue(q, node->lChild);
if (node->rChild)
JoinQueue(q, node->rChild);
if ( (node->lChild && !node->rChild) || (!node->rChild && !node->lChild))
leaf = 1;
}
return 1;
}
int DoubleBranchNodeNums(BTree root){
if (!root) return 0;
if (!root->rChild && !root->lChild) return 0;
int count = 0;
Queue *q = (Queue *)malloc(sizeof(Queue));
BTree cur = NULL;
InitQueue(q);
JoinQueue(q, root);
while (!IsEmpty(q)){
LeaveQueue(q, &cur);
if (cur->lChild && cur->rChild)
count++;
if (cur->lChild)
JoinQueue(q, cur->lChild);
if (cur->rChild)
JoinQueue(q, cur->rChild);
}
return count;
}
void SwapLeftAndRight(BTree root){
if (root){
SwapLeftAndRight(root->lChild);
SwapLeftAndRight(root->rChild);
BTree temp = root->lChild;
root->lChild = root->rChild;
root->rChild = temp;
temp = NULL;
} else
return;
}
char GetKthInPreOrder(BTree root, int k){
Stack *s = (Stack *)malloc(sizeof(Stack));
InitStack(s);
BTree ptr = root;
int count = 0;
while (ptr || !isEmpty(s)){
if (ptr){
Push(s, ptr);
count++;
if (count == k)
return ptr->val;
ptr = ptr->lChild;
} else{
Pop(s, &ptr);
ptr = ptr->rChild;
}
}
}
_Bool DeleteSubtree(BTree root, char x, _Bool flag){
if (!root)
return 0;
else{
if (root->val == x)
flag = 1;
int leftRes = DeleteSubtree(root->lChild, x, flag);
int rightRes = DeleteSubtree(root->rChild, x, flag);
if (flag == 1){
if (root->val == x)
return 1;
root = NULL;
} else{
if (leftRes == 1)
root->lChild = NULL;
if (rightRes == 1)
root->rChild = NULL;
}
}
return 0;
}
int XNodeAncestor(BTree root, char x){
BTree ptr = root;
if (ptr){
if (ptr->val == x)
return 1;
int left = XNodeAncestor(root->lChild, x);
int right = XNodeAncestor(root->rChild, x);
if (left || right){
printf("%c ", ptr->val);
return 1;
} else
return 0;
}
return 0;
}
BTree CommonAncestor(BTree root, char p, char q){
if (!root || root->val == p || root->val == q)
return root;
BTree left = CommonAncestor(root->lChild, p, q);
BTree right = CommonAncestor(root->rChild, p, q);
if (left && right)
return root;
return left ? left : right;
}
void GetPostOrder(char *pre, int preS, int preE, char *post, int postS, int postE){
if (preS > preE)
return;
int half;
post[postE] = pre[preS];
half = (preE - preS) / 2;
GetPostOrder(pre, preS + 1, preS + half, post, postS, postS + half - 1);
GetPostOrder(pre, preS + half + 1, preE, post, postS + half, postE - 1);
}
int GetLeafNodeNums(BTree root){
int count;
if (!root)
count = 0;
else if (!root->lChild && !root->rChild)
count = 1;
else
count = GetLeafNodeNums(root->lChild) + GetLeafNodeNums(root->rChild);
return count;
}
int GetNodeNums(BTree root){
int count = 0;
if (!root)
return 0;
else
count = GetNodeNums(root->lChild) + GetNodeNums(root->rChild) + 1;
return count;
}
int GetWidth(BTree root){
Queue *q = (Queue *)malloc(sizeof(Queue));
InitQueue(q);
BTree ptr = NULL;
int length = 0;
JoinQueue(q, root);
while (!IsEmpty(q)){
if (q->rear - q->front > length)
length = q->rear - q->front;
LeaveQueue(q, &ptr);
if (ptr->lChild)
JoinQueue(q, ptr->lChild);
if (ptr->rChild)
JoinQueue(q, ptr->rChild);
}
return length;
}
#define max(a, b) ((a)>= (b)) ? (a) : (b)
int GetHeight(BTree root){
return !root ? 0 : max(GetHeight(root->lChild), GetHeight(root->rChild)) + 1;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具