一、二叉树的性质
1、第K层的二叉树节点个数最多为2^(k - 1)个
2、k层的二叉树节点个数最多有2^(k) - 1个
3、度数为2的节点个数n2等于叶子节点个数n0减,即n2 + 1 = n0
4、第i个节点的左孩子为2i,右孩子为(2i + 1),根节点从1开始
5、具有N个节点的二叉树的深度为log2(N)的下界 + 1
二、树的表示方法
1、二叉链表表示法
typedef struct TreeNode{
int date;
struct TreeNode *lNode, *rNode;
}TreeNode;
2、三叉链表表示法
typedef struct TreeNode{
int date;
struct TreeNode *lNode, *rNode, *parent;
}TreeNode;
3、双亲链表表示法
typedef struct BTNode{
int date;
int parentPosition; //根节点的下标
int LRtag; //左右孩子的标志
}BTNode;
typedef struct BTree{
BTNode node[100];
int nodenum;
int root;
}BTree;
int main(){
BTree tree;
tree.root = 0;
tree.node[0].date = 0;
tree.node[1].date = 1;
tree.node[2].date = 2;
tree.node[3].date = 3;
tree.node[4].date = 4;
tree.node[1].parentPostion = 0;
tree.node[1].LRtag = 1;
tree.node[2].parentPostion = 0;
tree.node[2].LRtag = 2;
tree.node[3].parentPostion = 1;
tree.node[3].LRtag = 1;
tree.node[4].parentPostion = 2;
tree.node[4].LRtag = 2;
}
三、二叉树的遍历
1、二叉树递归遍历的本质
(1)先序遍历
typedef struct TreeNode{
int date;
struct TreeNode *lNode, *rNode;
}TreeNode;
void preoder(TreeNode* t){
if(t == NULL)
return;
printf("%d\n", t->date);
preoder(t->lNode);
preoder(t->rNode);
}
(2)中序遍历
typedef struct TreeNode{
int date;
struct TreeNode *lNode, *rNode;
}TreeNode;
void inoder(TreeNode* t){
if(t == NULL)
return;
inoder(t->lNode);
printf("%d\n", t->date);
inoder(t->rNode);
}
(1)后序遍历
typedef struct TreeNode{
int date;
struct TreeNode *lNode, *rNode;
}TreeNode;
void postoder(TreeNode* t){
if(t == NULL)
return;
postoder(t->lNode);
postoder(t->rNode);
printf("%d\n", t->date);
}
三种递归遍历的本质是一样的,如果把printf的输出语句去掉,代码都是一样的,在二叉树的遍历过程中,所有的节点都会经过三次,如果每个节点经过第一次就访问,即为先序遍历,如果每个节点经过第二次再访问,即为中序遍历,如果每个节点经过第三次再访问,即为后序遍历;所以说递归遍历二叉树的本质就是遍历都是先左孩子再右孩子,只不过访问节点的时机不同罢了。
下面的二叉树均是以这样的二叉树结构体实现的:
typedef struct _tag_BTree {
char val;
struct _tag_BTree* left, *right;
}BTree;
2、 非递归遍历
(1)先序遍历
void unrecpreoder(BTree* TreeNodeA) {
BTree* p = TreeNodeA;
stack<BTree*> st;
while (p || !st.empty()) {
while (p) {
st.push(p);
cout << p->val;
p = p->left;
}
p = st.top();
st.pop();
p = p->right;
}
return;
}
2、中序遍历
void unrecinoder(BTree* TreeNodeA) {
BTree* p = TreeNodeA;
stack
void unrecpostoder(BTree* TreeNodeA) {
BTree* p = TreeNodeA;
BTree* q = NULL;
stack<BTree*> st;
while (p || !st.empty()) {
while (p) {
st.push(p);
p = p->left;
}
p = st.top();
if (p->right == NULL || p->right == q) {
q = st.top();
cout << q->val;
st.pop();
p = NULL;
}
else
p = p->right;
}
return;
}
四、求所有节点个数
int getleafnum(BTree* TreeNode, int * num) {
int leafnum = 0;
if (TreeNode == NULL)
return 0;
leafnum++;
int leftnum = getleafnum(TreeNode->left, num);
int rightnum = getleafnum(TreeNode->right, num);
leafnum += leftnum + rightnum;
*num = leafnum;
return leafnum;
}
五、求二叉树的深度
int getdepth(BTree* TreeNode, int *depth) {
int treedepth = 0;
if (TreeNode == NULL)
return 0;
int leftdepth = getdepth(TreeNode->left, depth);
int rightdepth = getdepth(TreeNode->right, depth);
treedepth = max(leftdepth, rightdepth) + 1;
*depth = treedepth;
return treedepth;
}
六、二叉树的拷贝
BTree* TreeCopy(BTree* TreeNode) {
if (TreeNode == NULL)
return NULL;
BTree* newnode = (BTree*)malloc(sizeof(BTree));
newnode->val = TreeNode->val;
newnode->left = TreeCopy(TreeNode->left);
newnode->right = TreeCopy(TreeNode->right);
return newnode;
}
未完待续……