文章目录
二叉树的基本结构
用指针来指向两个子树节点,依次来把节点来连接起来
二叉树的遍历(前序,中序,后序) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 #include <stdio.h> #include <stdlib.h> typedef char btdata;typedef struct BinaryTreeNode { struct BinaryTreeNode *left ; struct BinaryTreeNode *right ; btdata data; }btnode; btnode* buynode (btdata x) { btnode*node=malloc (sizeof (btnode)); if (node==NULL ) { exit (-1 ); } node->data=x; node->left=NULL ; node->right=NULL ; } btnode*creatbinarytree () { btnode*nodeA=buynode('A' ); btnode*nodeB=buynode('B' ); btnode*nodeC=buynode('C' ); btnode*nodeD=buynode('D' ); btnode*nodeE=buynode('E' ); btnode*nodeF=buynode('F' ); nodeA->left=nodeB; nodeB->left=nodeD; nodeA->right=nodeC; nodeC->left=nodeE; nodeC->right=nodeF; return nodeA; }void preorder (btnode*root) { if (root==NULL ) { printf ("NULL->" ); return ; }printf ("%c->" ,root->data); preorder(root->left); preorder(root->right); }void inorder (btnode*root) { if (root==NULL ) { printf ("NULL->" ); return ; } inorder(root->left);printf ("%c->" ,root->data); inorder(root->right); }void postorder (btnode*root) { if (root==NULL ) { printf ("NULL->" ); return ; } postorder(root->left); postorder(root->right);printf ("%c->" ,root->data); }
计算二叉树的节点的个数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 int binarytreesize (btnode*root) { if (root==NULL ) { return 0 ; } static int count=0 ; count++; binarytreesize(root->left); binarytreesize(root->right); return count; }void binarytreesize2 (btnode*root,int *x) { if (root==NULL ) { return ; } ++(*x); binarytreesize2(root->left,x); binarytreesize2(root->right,x); }int binarytreesize3 (btnode*root) { return root==NULL ?0 :binarytreesize3(root->left)+binarytreesize3(root->right)+1 ; }
1 2 3 4 5 6 7 8 9 10 11 12 int binaryleafsize2 (btnode*root) {if (root==NULL ) {return 0 ; } return (root->left==NULL )&&(root->right==NULL )?1 :binaryleafsize2(root->left)+binaryleafsize2(root->right); }
计算第k层的节点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 int binarytreelevelsize (btnode*root,int k) { if (root==NULL ) { return 0 ; } if (k<1 ) { return 0 ; } else if (k==1 ) { return 1 ; }return binarytreelevelsize(root->left,k-1 )+binarytreelevelsize(root->right,k-1 ); }
二叉树的深度 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 int binarytreedepth (btnode*root) {if (root==NULL ) { return 0 ; }int leftdepth=binarytreedepth(root->left);int rightdepth=binarytreedepth(root->right);return leftdepth>rightdepth?leftdepth+1 :rightdepth+1 ; } ```c
查找一个节点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 btnode* binarytreefind (btnode*root,btdata x) {if (root==NULL ) { return NULL ; }if (root->data==x) { return root; } btnode*leftret=binarytreefind(root->left,x); if (leftret) { return leftret; } btnode*rightret=binarytreefind(root->right,x); if (rightret) { return rightret; }return NULL ; }
层序遍历
用队列实现 1.先入根 2.在当前节点出来后,再把他的左右孩子带进去,不为空才带进去,这样上一层节点出的时候,带入下一层 3.直到队列为空,说明最后一层没有节点,遍历结束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 void binarytreelevelorder (btnode* root) { if (root == NULL ) return ; queue q; queueinit(&q); queuepush(&q, root); while (!queueempty(&q)) { btnode* front = queuefront(&q); queuepop(&q); printf ("%c " , front->data); if (front->left) queuepush(&q, front->left); if (front->right) queuepush(&q, front->right); } printf ("\n" ); }
判断是否为完全二叉树
判断满二叉树,可以计算树的高度,再计算树的节点树,是否等于2^h-1,十分容易
1.判断是否是完全二叉树(非空节点是连续的) 用层序遍历,队列的时候,空也入,null也当作一个元素,遇到空之后,队列里面全是空 非完全二叉树,遇到空后,队列里面并非全空
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 bool iscompletebinarytree (btnode*root) { queue q; queueinit(&q); queuepush(&q, root); while (!queueempty(&q)) { btnode* front = queuefront(&q); queuepop(&q); if (front == NULL ) { break ; } else { queuepush(&q, front->left); queuepush(&q, front->right); } } while (queuesize(&q)) { btnode* front = queuefront(&q); queuepop(&q); if (front) { queuedestroy(&q); return false ; } } queuedestroy(&q); return true ; }
test.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 int main () { btnode*root=creatbinarytree(); postorder(root); int n1=0 ; binarytreesize2(root, &n1); printf ("\n" ); printf ("n1=%d\n" ,n1); int n2=0 ; printf ("n2=%d\n" ,binarytreesize3(root)); int leafn=0 ;; printf ("leafn=%d\n" ,leafn); printf ("leaf2=%d\n" ,binaryleafsize2(root)); int n3=0 ; printf ("levelk=%d\n" ,binarytreelevelsize(root,3 ));printf ("depth=%d\n" ,binarytreedepth(root)); return 0 ; }