二叉樹是一種重要的資料結構,關於二叉樹的相關操作尤為重要,其中主要操作大概分為:建樹,遍歷,返回二叉樹的相關引數。
#pragma once
#include "queue.h"
#include "stack.h"
#include #include #include #include #define max(x,y) (x)>(y)?(x):(y)
typedef char datatype;
typedef struct treenodetreenode;
//初始化
treenode * createroot(datatype data)
//建一顆二叉樹
treenode * createtree(datatype preorder, int size, int *pindex)
if (preorder[*pindex] == '#')
treenode *proot = createroot(preorder[*pindex]);
*pindex+=1;
proot->pleft = createtree(preorder, size, pindex);
proot->pright = createtree(preorder, size, pindex);
return proot;
}// 前序,遞迴
void preorder(treenode *proot)
printf("%c ",proot->data);
preorder(proot->pleft);
preorder(proot->pright);
}// 中序,遞迴
void inorder(treenode *proot)
inorder(proot->pleft);
printf("%c ", proot->data);
inorder(proot->pright);
}// 後序,遞迴
void postorder(treenode *proot)
postorder( proot->pleft);
postorder( proot->pright);
printf("%c ", proot->data);
}//樹的結點
int getsize(treenode *proot)
return getsize(proot->pleft) + getsize(proot->pright) + 1;
}//葉子的結點
int getleafsize(treenode *proot)
if (proot->pleft == null && proot->pright == null)
return getleafsize(proot->pleft) + getleafsize(proot->pright);
}//k層 葉子結點
int getklevelsize(treenode *proot, int k)
if (k==1)
return getklevelsize(proot->pleft, k - 1) + getklevelsize(proot->pright, k - 1);
}// 找到,返回結點位址,否則返回 null
treenode *find(treenode *proot, datatype data)
if (proot->data == data)
treenode *pfind=find(proot->pleft, data);
if (pfind != null)
return find(proot->pright, data);
}// 求樹的高度/深度
int getheight(treenode *proot)
if (proot->pleft == null&&proot->pright == null)
int left = getheight(proot->pleft);
int right = getheight(proot->pright);
return max((left), (right)) + 1;
}//非遞層序遍歷
void levelorder(treenode *proot)
if (front->pright != null) }}
//判斷是否為完全二叉樹,是返回1,否返回0.
int iscomlelement(treenode *proot)
queue queue;
treenode *front;
queueinit(&queue);
queuepush(&queue, proot);
while (!queueisempty(&queue))
queuepush(&queue, front->pleft);
queuepush(&queue, front->pright);
} while (!queueisempty(&queue))
return 0;
} return;
}//非遞迴前序
void preorder1(treenode *ptree)
ptop = getfirst(&stack);
stackpop(&stack);
pcur = ptop->pright; }}
//非遞迴中序
void inorder1(treenode *ptree)
top = getfirst(&stack);
stackpop(&stack);
printf("%c ", top->data);
pcur = top->pright; }}
//非遞迴後序
void posorder1(treenode *ptree)
top = getfirst(&stack);
if (top->pright == null || top->pright == last)
else
}}void test()
#pragma once
#include #include #include typedef void * qdatatype;
typedef struct qnodeqnode;
typedef struct queue queue;
void queueinit(queue *pq)
//入佇列
void queuepush(queue *pq, qdatatype data)
pq->prear->pnext = pnewnode;
pq->prear = pnewnode;
}//出佇列
void queuepop(queue *pq)
}qdatatype queuefront(queue *pq)
int queueisempty(queue *pq)
int queuesize(queue *pq)
#pragma once
#include #include #include typedef void * qdatatype;
typedef struct qnodeqnode;
typedef struct queue queue;
void queueinit(queue *pq)
//入佇列
void queuepush(queue *pq, qdatatype data)
pq->prear->pnext = pnewnode;
pq->prear = pnewnode;
}//出佇列
void queuepop(queue *pq)
}qdatatype queuefront(queue *pq)
int queueisempty(queue *pq)
int queuesize(queue *pq)
主函式直接使用test()用來測試:
#include "tree.h"
#include "queue.h"
int main()
關於資料結構中二叉樹
include include include typedef char element typedef struct bitnode bitnode,bitree struct queuenode typedef struct queuenode queuenode typedef queueno...
資料結構 二叉樹 反轉二叉樹
include using namespace std define maxsize 1000 struct binary tree node class queue queue queue void queue push binary tree node btn binary tree node ...
C 資料結構 二叉樹操作
基本知識點不講了,直接說遍歷問題 前序遍歷 根節點 左子樹 右子樹 中序遍歷 左子樹 根節點 右子樹 後序遍歷 左子樹 右子樹 根節點 具體看 未完待續,困,明天繼續 binarytreetest.cpp 定義控制台應用程式的入口點。include stdafx.h include include ...