二叉树
概念
-
二叉树:
是n(n>=0)个节点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根节点和两棵互不相交的、分别称为根节点的左子树和右子树的二叉树组成。
-
满二叉树:
如果一个二叉树中的任何结点要么度数为0,要么度数为2,则此二叉树为满二叉树
-
完全二叉树:
如果一棵二叉树中,只有最下面两层的节点的度数小于2,其余各层的度数都等于2,并且最下面一层的节点都集中在最左边的若干位置上,则此二叉树称为完全二叉树。注意:完全二叉树不一定是满二叉树。
-
二叉树的性质1:
在二叉树的第
i
层上至多有2的i-1
次方个节点。(i
>=1;且根节点定义为第1层) -
二叉树的性质2:
深度为k的二叉树最多有2的k次方-1个节点(K>=1;即根节点所处深度为1)
-
二叉树的性质3:
对任何一个二叉树T,如果其终端节点(度数为0的节点)数为
n0
,度为2的节点数为n2
,则n0=n2+1
. -
二叉树性质4:
具有n个节点的完全二叉树的深度为不大于logn的最大整数+1.
实战
-
计算叶子数
#include <iostream> using namespace std; typedef char DataType; //二叉树数据结构 struct node { DataType info ; //存放结点数据 struct node *lchild , *rchild ; //指向左右孩子的指针 }; typedef struct node *BiTree ; /*创建二叉树 函数名:createBiTree 参数:无 返回值:二叉树根结点指针 */ BiTree createBiTree(void) { char ch ; BiTree root ; cin>>ch ; if(ch == '#') root = NULL; else{ root = new struct node ; root->info = ch ; root->lchild = createBiTree() ; root->rchild = createBiTree(); } return root ; } void visit(BiTree T) { cout<<T->info ; } int countLeaf(BiTree root) { if(root == NULL) return 0; if((root->lchild == NULL)&&(root->rchild == NULL)) return 1; else{ int right ,left; left = 0 + countLeaf(root->lchild); right = 0 + countLeaf(root -> rchild); return left+right; } } int main(void) { Bi Tree root = createBiTree(); cout<<countLeaf(root); }
-
交换左右节点
#include <iostream> using namespace std; typedef char DataType; //二叉树数据结构 struct node { DataType info ; //存放结点数据 struct node *lchild , *rchild ; //指向左右孩子的指针 }; typedef struct node *BiTree ; /*创建二叉树 函数名:createBiTree 参数:无 返回值:二叉树根结点指针 */ BiTree createBiTree(void) { char ch ; BiTree root ; cin>>ch ; if(ch == '#') root = NULL; else{ root = new struct node ; root->info = ch ; root->lchild = createBiTree() ; root->rchild = createBiTree(); } return root ; } void changeLR(BiTree root) { if(root == NULL) return ; BiTree Temp; Temp =root->lchild ; root->lchild = root->rchild ; root->rchild = Temp ; if(root->lchild != NULL){ changeLR(root->lchild) ; } if(root->rchild != NULL){ changeLR(root->rchild) ; } } void visit(BiTree T) //输出结点T的数据 { cout<<T->info ; } void inOrder(BiTree root) { if(root == NULL) return ; inOrder(root->lchild); visit(root); inOrder(root->rchild); } int main(void) { BiTree root = createBiTree(); changeLR(root); inOrder(root); }
-
计算有两个孩子的结点个数
#include <iostream> using namespace std; typedef char DataType; //二叉树数据结构 struct node { DataType info ; //存放结点数据 struct node *lchild , *rchild ; //指向左右孩子的指针 }; typedef struct node *BiTree ; /*创建二叉树 函数名:createBiTree 参数:无 返回值:二叉树根结点指针 */ BiTree createBiTree(void) { char ch ; BiTree root ; cin>>ch ; if(ch == '#') root = NULL; else{ root = new struct node ; root->info = ch ; root->lchild = createBiTree() ; root->rchild = createBiTree(); } return root ; } void visit(BiTree T) { cout<<T->info ; } int countFullNode(BiTree root) { if((root == NULL)|| (root->rchild == NULL)||(root->lchild == NULL)) return 0; else{ return 1+countFullNode(root->rchild)+countFullNode(root->lchild); } } int main(void) { BiTree root = createBiTree(); cout<<countFullNode(root) ; }