Following is the study and understanding of the binary tree by Ayu. I would like to share with you, hoping to help some of you. If you have more expenses, please generously point out.
A carp will be explained in the following order (c language implementation)
BTNode *_CreateBinTree(char *array, int size, int* index, BTDataType invalid);//Establishment of Binary Tree BTNode *CreateBinTree(char *array, int size, BTDataType invaild);//Establishment of Binary Tree void PreOrder(BTNode *pRoot);//Preorder traversal: the left subtree of the root - "the right subtree of the root" void InOrder(BTNode *pRoot);//Mid-order traversal: the left subtree of the root - "the right subtree of the root -" void PostOrder(BTNode *pRoot);//Postordinal traversal: the left subtree of the root - "the right subtree of the root -"the root" void LeveIOrder(BTNode *pRoot);//level traversal void DestroyBinTree(BTNode **pRoot);//Destruction of Binary Trees BTNode *CopyBinTree(BTNode *pRoot);//Copies of Binary Trees int GetBinTreeSize(BTNode *pRoot);//Calculate how many nodes there are in a binary tree int GetLeafCount(BTNode *pRoot);//Calculate how many leaf nodes are in a binary tree int GetBinTreeHeight(BTNode *pRoot);//Computing the depth of a tree int GetKLeveNodeCount1(BTNode *pRoot,int k);//Get the number of nodes in the k-th layer of the binary tree 1 int GetKLeveNodeCount2(BTNode *pRoot, int k);//Get the number of nodes in the k-th layer of the binary tree 2 BTNode *BinaryTreeFind(BTNode *root, BTDataType x);//Return to the location of the x element void Mirror(BTNode *pRoot);//Binary Tree Mirror Sequence void MirrorNor(BTNode *pRoot);//Is the mirror image successful? void Swap(BTNode **left, BTNode **right);//Binary Tree Mirror Image
First, attach the structure definition file (which contains the queue structure, which is created for sequence traversal)
#ifndef _BINTREE_C_ #define _BINTREE_C_ #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<string.h> typedef char BTDataType; typedef struct BTNode//Binary tree node { struct BTNode *_pLeft;//Child Representation struct BTNode *_pRight; BTDataType data; }BTNode; typedef BTNode* QDataType; typedef struct QNode//Queue node { struct QNode* _pNext; QDataType _data; }QNode; typedef struct Queue//Define two pointers to the node at the head and end of the queue { QNode * _front;//Team leader QNode * _back;//Team tail }Queue; void QueueInit(Queue *q);// Initialization void QueuePush(Queue *q, QDataType x);//Queue entry void QueuePop(Queue *q);//Outgoing queue QDataType QueueFrount(Queue *q);//Get head QDataType QueueBack(Queue *q);//Get tail int QueueSize(Queue *q);//Queue size int QueueEmpty(Queue *q);//Sentence blank void QueueDestroy(Queue *q);//Destruction void QueueShow(Queue *q);//Printing BTNode *_CreateBinTree(char *array, int size, int* index, BTDataType invalid);//Establishment of Binary Tree BTNode *CreateBinTree(char *array, int size, BTDataType invaild);//Establishment of Binary Tree void PreOrder(BTNode *pRoot);//Preorder traversal: the left subtree of the root - "the right subtree of the root" void InOrder(BTNode *pRoot);//Mid-order traversal: the left subtree of the root - "the right subtree of the root -" void PostOrder(BTNode *pRoot);//Postordinal traversal: the left subtree of the root - "the right subtree of the root -"the root" void LeveIOrder(BTNode *pRoot);//level traversal void DestroyBinTree(BTNode **pRoot);//Destruction of Binary Trees BTNode *CopyBinTree(BTNode *pRoot);//Copies of Binary Trees int GetBinTreeSize(BTNode *pRoot);//Calculate how many nodes there are in a binary tree int GetLeafCount(BTNode *pRoot);//Calculate how many leaf nodes are in a binary tree int GetBinTreeHeight(BTNode *pRoot);//Computing the depth of a tree int GetKLeveNodeCount1(BTNode *pRoot,int k);//Get the number of nodes in the k-th layer of the binary tree 1 int GetKLeveNodeCount2(BTNode *pRoot, int k);//Get the number of nodes in the k-th layer of the binary tree 2 BTNode *BinaryTreeFind(BTNode *root, BTDataType x);//Return to the location of the x element void Mirror(BTNode *pRoot);//Binary Tree Mirror Sequence void MirrorNor(BTNode *pRoot);//Is the mirror image successful? void Swap(BTNode **left, BTNode **right);//Binary Tree Mirror Image #endif // !_BINTREE_C_
Test function file
#include"BinTree.h" void TestBinTree() { char *str = "ABD###CE##F##"; BTNode *pRoot = CreateBinTree(str, strlen(str),'#'); printf("Preorder ergodic results: "); PreOrder(pRoot); printf("\n"); printf("Intermediate order traversal results: "); InOrder(pRoot); printf("\n"); printf("Postorder ergodic results: "); PostOrder(pRoot); printf("\n"); LeveIOrder(pRoot);//Sequence traversal results printf("Two fork tree%d Leaf nodes\n", GetLeafCount(pRoot));//Get the number of leaf nodes printf("Two fork tree%d Node\n", GetBinTreeSize(pRoot));//Finding the Number of Nodes in Binary Tree printf("The depth of a binary tree is%d\n", GetBinTreeHeight(pRoot));//Get the depth of the tree printf("Number of nodes in the third layer of binary tree%d\n", GetKLeveNodeCount1(pRoot, 3)); printf("Number of nodes in the third layer of binary tree%d\n", GetKLeveNodeCount2(pRoot, 3)); BTNode * Find = BinaryTreeFind(pRoot,'B');//Find the node for B printf("The range found is:%c\n", Find->data); Mirror(pRoot);//Mirror 1 Sequence Traversal LeveIOrder(pRoot);//Sequence traversal results MirrorNor(pRoot);//Mirror 2 Preorder Traversal LeveIOrder(pRoot);//Sequence traversal results BTNode*p = CopyBinTree(pRoot);//Copy LeveIOrder(p);//Sequence traversal results DestroyBinTree(pRoot);//Destruction } int main() { TestBinTree(); system("pause"); return; }
One: Binary Tree Creation (Child Representation)
The creation of a binary tree requires the following two functions
BTNode *_CreateBinTree(char *array, int size, int* index, BTDataType invalid);//Establishment of Binary Tree BTNode *CreateBinTree(char *array, int size, BTDataType invaild);//Establishment of Binary Tree
Implementation code:
BTNode *BuyBinTeeNode(BTDataType data)//Create a new node { BTNode *pNewNode = (BTNode*)malloc(sizeof(BTNode)); if (NULL == pNewNode) { assert(0); return NULL; } pNewNode->data = data; pNewNode->_pLeft = NULL; pNewNode->_pRight = NULL; return pNewNode; } BTNode *_CreateBinTree(char *array, int size, int* index, BTDataType invalid)//Creation of Binary Tree { //Root node BTNode *pRoot = NULL; int count = 0; if (*index < size && array[*index] != invalid) { count++; pRoot = BuyBinTeeNode(array[*index]); ++*index; //Left subtree of root pRoot->_pLeft = _CreateBinTree(array, size, index, invalid); ++*index; //Right subtree of root pRoot->_pRight = _CreateBinTree(array, size, index, invalid); } return pRoot; } BTNode *CreateBinTree(char *array, int size, BTDataType invaild)//Creation of Binary Tree { //Root node int index = 0; return _CreateBinTree(array, size, &index, invaild); }
analysis
Following the above method of recursion, we can create the binary tree we want.
2. Pre-order traversal, mid-order traversal and post-order traversal
These three traversals are very similar, so let's just introduce the preface traversal.
Code:
//Preorder traversal: the left subtree of the root - "the right subtree recurrence of the root" void PreOrder(BTNode *pRoot) { if (pRoot) { printf("%-2c", pRoot->data); PreOrder(pRoot->_pLeft); PreOrder(pRoot->_pRight); } } //Mid-order traversal: the left subtree of the root -"the right subtree recurrence of the root -" void InOrder(BTNode *pRoot) { if (pRoot) { InOrder(pRoot->_pLeft); printf("%-2c", pRoot->data); InOrder(pRoot->_pRight); } } //Postordinal traversal: the left subtree of the root - "the right subtree of the root -" root recursion void PostOrder(BTNode *pRoot) { if (pRoot) { PostOrder(pRoot->_pLeft); PostOrder(pRoot->_pRight); printf("%-2c", pRoot->data);; } }
Parsing: Pre-order traversal is convenient according to the left subtree of root - > root - > right subtree of root; our binary tree is created by the method of pre-order traversal, so the steps are the same as above: so there is no illustration here.
3. Sequence traversal
Code:
void LeveIOrder(BTNode *pRoot)//level traversal { if (NULL == pRoot) { return; } printf("Sequence traversal results:"); Queue q; QueueInit(&q); QueuePush(&q,pRoot); while (!QueueEmpty(&q)) { BTNode *pCur = QueueFrount(&q); printf("%-2c", pCur->data); if (pCur->_pLeft) { QueuePush(&q, pCur->_pLeft); } if (pCur->_pRight) { QueuePush(&q, pCur->_pRight); } QueuePop(&q); } printf("\n"); QueueDestroy(&q);//Destruction }
Analysis: Sequence traversal, as its name implies, is based on the binary tree layer traversal; for the binary tree we created, the result of sequence traversal is
A BC DEF; We use queues in sequence traversal. For an introduction to queues, Click Introduction to queues For detailed analysis, see the following figure.
Fourth: Getting the Number of Leaf Nodes
Code:
int GetLeafCount(BTNode *pRoot)//Calculate how many leaf nodes are in a binary tree { if (NULL == pRoot) { return 0; } if (NULL == pRoot->_pLeft) { return 1; } return GetLeafCount(pRoot->_pLeft) + GetLeafCount(pRoot->_pRight); }
Analysis: To calculate the number of leaf nodes, it is only necessary to recursively traverse them, and return 1 when both left and right nodes are NULL, and add them up.
Others are not very difficult, Ayu does not specifically describe, the following is the whole code.
BinTree.h
#ifndef _BINTREE_C_ #define _BINTREE_C_ #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<string.h> typedef char BTDataType; typedef struct BTNode//Binary tree node { struct BTNode *_pLeft;//Child Representation struct BTNode *_pRight; BTDataType data; }BTNode; typedef BTNode* QDataType; typedef struct QNode//Queue node { struct QNode* _pNext; QDataType _data; }QNode; typedef struct Queue//Define two pointers to the node at the head and end of the queue { QNode * _front;//Team leader QNode * _back;//Team tail }Queue; void QueueInit(Queue *q);// Initialization void QueuePush(Queue *q, QDataType x);//Queue entry void QueuePop(Queue *q);//Outgoing queue QDataType QueueFrount(Queue *q);//Get head QDataType QueueBack(Queue *q);//Get tail int QueueSize(Queue *q);//Queue size int QueueEmpty(Queue *q);//Sentence blank void QueueDestroy(Queue *q);//Destruction void QueueShow(Queue *q);//Printing BTNode *_CreateBinTree(char *array, int size, int* index, BTDataType invalid);//Establishment of Binary Tree BTNode *CreateBinTree(char *array, int size, BTDataType invaild);//Establishment of Binary Tree void PreOrder(BTNode *pRoot);//Preorder traversal: the left subtree of the root - "the right subtree of the root" void InOrder(BTNode *pRoot);//Mid-order traversal: the left subtree of the root - "the right subtree of the root -" void PostOrder(BTNode *pRoot);//Postordinal traversal: the left subtree of the root - "the right subtree of the root -"the root" void LeveIOrder(BTNode *pRoot);//level traversal void DestroyBinTree(BTNode **pRoot);//Destruction of Binary Trees BTNode *CopyBinTree(BTNode *pRoot);//Copies of Binary Trees int GetBinTreeSize(BTNode *pRoot);//Calculate how many nodes there are in a binary tree int GetLeafCount(BTNode *pRoot);//Calculate how many leaf nodes are in a binary tree int GetBinTreeHeight(BTNode *pRoot);//Computing the depth of a tree int GetKLeveNodeCount1(BTNode *pRoot,int k);//Get the number of nodes in the k-th layer of the binary tree 1 int GetKLeveNodeCount2(BTNode *pRoot, int k);//Get the number of nodes in the k-th layer of the binary tree 2 BTNode *BinaryTreeFind(BTNode *root, BTDataType x);//Return to the location of the x element void Mirror(BTNode *pRoot);//Binary Tree Mirror Sequence void MirrorNor(BTNode *pRoot);//Is the mirror image successful? void Swap(BTNode **left, BTNode **right);//Binary Tree Mirror Image #endif // !_BINTREE_C_
BinTree.c
#include"BinTree.h" BTNode *BuyBinTeeNode(BTDataType data) { BTNode *pNewNode = (BTNode*)malloc(sizeof(BTNode)); if (NULL == pNewNode) { assert(0); return NULL; } pNewNode->data = data; pNewNode->_pLeft = NULL; pNewNode->_pRight = NULL; return pNewNode; } BTNode *_CreateBinTree(char *array, int size, int* index, BTDataType invalid)//Creation of Binary Tree { //Root node BTNode *pRoot = NULL; int count = 0; if (*index < size && array[*index] != invalid) { count++; pRoot = BuyBinTeeNode(array[*index]); ++*index; //Left subtree of root pRoot->_pLeft = _CreateBinTree(array, size, index, invalid); ++*index; //Right subtree of root pRoot->_pRight = _CreateBinTree(array, size, index, invalid); } return pRoot; } BTNode *CreateBinTree(char *array, int size, BTDataType invaild)//Creation of Binary Tree { //Root node int index = 0; return _CreateBinTree(array, size, &index, invaild); } //Preorder traversal: the left subtree of the root - "the right subtree recurrence of the root" void PreOrder(BTNode *pRoot) { if (pRoot) { printf("%-2c", pRoot->data); PreOrder(pRoot->_pLeft); PreOrder(pRoot->_pRight); } } //Mid-order traversal: the left subtree of the root -"the right subtree recurrence of the root -" void InOrder(BTNode *pRoot) { if (pRoot) { InOrder(pRoot->_pLeft); printf("%-2c", pRoot->data); InOrder(pRoot->_pRight); } } //Postordinal traversal: the left subtree of the root - "the right subtree of the root -" root recursion void PostOrder(BTNode *pRoot) { if (pRoot) { PostOrder(pRoot->_pLeft); PostOrder(pRoot->_pRight); printf("%-2c", pRoot->data);; } } void DestroyBinTree(BTNode **pRoot)//Postorder of Destruction and Use { assert(pRoot); if (*pRoot) { DestroyBinTree(&(*pRoot)->_pLeft); DestroyBinTree(&(*pRoot)->_pRight); free(*pRoot); *pRoot = NULL; } } int GetLeafCount(BTNode *pRoot)//Calculate how many leaf nodes are in a binary tree { if (NULL == pRoot) { return 0; } if (NULL == pRoot->_pLeft) { return 1; } return GetLeafCount(pRoot->_pLeft) + GetLeafCount(pRoot->_pRight); } int _GetBinTreeSize(BTNode *pRoot, int *k)//Calculate how many nodes there are in a binary tree { if (pRoot) { *k = *k+1; _GetBinTreeSize(pRoot->_pLeft, k); _GetBinTreeSize(pRoot->_pRight, k); } return *k; } int GetBinTreeSize(BTNode *pRoot )//Calculate how many nodes there are in a binary tree { if (NULL == pRoot) { return 0; } int k = 0; return _GetBinTreeSize(pRoot, &k); } int count = 0; int _GetBinTreeHeight(BTNode *pRoot ,int *k)//Computing the depth of a tree { if (pRoot) { *k = *k + 1; _GetBinTreeHeight(pRoot->_pLeft, k); _GetBinTreeHeight(pRoot->_pRight, k); } if (*k > count) { count = *k; } *k = *k - 1; return *k; } int GetBinTreeHeight(BTNode *pRoot)//Computing the depth of a tree { int k = 0; _GetBinTreeHeight(pRoot, &k); return count; } int _GetKLeveNodeCount(BTNode* pRoot, int k, int *m)//Get the number of nodes in the k-th layer of the binary tree 1 { if (pRoot) { *m = *m + 1; if (*m == k) { count++; } _GetKLeveNodeCount(pRoot->_pLeft,k, m); _GetKLeveNodeCount(pRoot->_pRight,k, m); } if (pRoot) { *m = *m-1; } return *m; } int GetKLeveNodeCount1(BTNode *pRoot, int k)//Get the number of nodes in the k-th layer of the binary tree 1 { if (NULL == pRoot || k < 0) { return 0; } count = 0; int m = 0; _GetKLeveNodeCount(pRoot, k, &m); return count; } int GetKLeveNodeCount2(BTNode *pRoot, int k)//Get the number of nodes in the k-th layer of the binary tree 2 { if (NULL == pRoot || k < 0) { return 0; } if (1 == k) { return 1; } return GetKLeveNodeCount2(pRoot->_pLeft, k - 1) + GetKLeveNodeCount2(pRoot->_pRight, k - 1); } BTNode *BinaryTreeFind(BTNode *pRoot, BTDataType x)//Return to the location of the x element { if (NULL == pRoot) { return NULL; } if (x == pRoot->data) { return pRoot; } BTNode *pNode = NULL; if (pNode = BinaryTreeFind(pRoot->_pLeft, x))//Left child tree search { return pNode; } return BinaryTreeFind(pRoot->_pRight, x);//Right subtree search } void LeveIOrder(BTNode *pRoot)//level traversal { if (NULL == pRoot) { return; } printf("Sequence traversal results:"); Queue q; QueueInit(&q); QueuePush(&q,pRoot); while (!QueueEmpty(&q)) { BTNode *pCur = QueueFrount(&q); printf("%-2c", pCur->data); if (pCur->_pLeft) { QueuePush(&q, pCur->_pLeft); } if (pCur->_pRight) { QueuePush(&q, pCur->_pRight); } QueuePop(&q); } printf("\n"); QueueDestroy(&q);//Destruction } void Swap(BTNode **left, BTNode **right)//exchange { BTNode *tmp = *left; *left = *right; *right = tmp; } void Mirror(BTNode *pRoot)//Binary Tree Mirror 1 { if (NULL == pRoot) { return NULL; } Queue q; QueueInit(&q); QueuePush(&q, pRoot); while(!QueueEmpty(&q)) { BTNode *pCur = QueueFrount(&q); Swap(&pCur->_pLeft, &pCur->_pRight); if (pCur->_pLeft) { QueuePush(&q, pCur->_pLeft); } if (pCur->_pRight) { QueuePush(&q, pCur->_pRight); } QueuePop(&q); } } void MirrorNor(BTNode *pRoot)//Binary Tree Mirror 2 { if (pRoot) { Swap(&pRoot->_pLeft, &pRoot->_pRight); MirrorNor(pRoot->_pLeft); MirrorNor(pRoot->_pRight); } } BTNode *CopyBinTree(BTNode *pRoot)//Copies of Binary Trees { BTNode *pNewRoot = NULL; if (pRoot) { pNewRoot = BuyBinTeeNode(pRoot->data); pNewRoot->_pLeft = CopyBinTree(pRoot->_pLeft); pNewRoot->_pRight = CopyBinTree(pRoot->_pRight); } return pNewRoot; } void QueueInit(Queue *q)// Initialization { assert(q); q->_back = NULL; q->_front = NULL; } QNode *BuyQueueNode(QDataType x)//Create a new node { QNode* pNewNode = (QNode*)malloc(sizeof(QNode)); if (NULL == pNewNode) { assert(0); return; } pNewNode->_data = x; pNewNode->_pNext = NULL; return pNewNode; } void QueuePush(Queue *q, QDataType x)//Queue entry { assert(q); QNode *pNewNode = BuyQueueNode(x); if (QueueEmpty(q)) { q->_back = pNewNode; q->_front = pNewNode; } else { q->_back->_pNext = pNewNode; q->_back = pNewNode; } } void QueuePop(Queue *q)//Outgoing queue { assert(q); if (q->_front == NULL) { return; } QNode *pDelete = q->_front; if (NULL == q->_front) { q->_back = NULL; q->_front = NULL; } else { q->_front = q->_front->_pNext; } free(pDelete); } QDataType QueueFrount(Queue *q)//Get head { assert(q); return q->_front->_data; } QDataType QueueBack(Queue *q)//Get tail { assert(q); return q->_back->_data; } int QueueSize(Queue *q)//Queue size { assert(q); int count = 0; QNode *pCur = q->_front; while (pCur) { pCur = pCur->_pNext; count++; } return count; } int QueueEmpty(Queue *q)//Sentence blank { assert(q); return NULL == q->_front; } void QueueDestroy(Queue *q)//Destruction { assert(q); QNode *pCur = q->_front; while (pCur) { q->_front = pCur->_pNext; free(pCur); pCur = q->_front; } q->_back = NULL; q->_front = NULL; } void QueueShow(Queue *q)//Printing { assert(q); QNode *qCur = q->_front; printf("Element is"); while (qCur) { printf("%-3d", qCur->_data); qCur = qCur->_pNext; } printf("\n"); }
test.c
#include"BinTree.h" void TestBinTree() { char *str = "ABD###CE##F##"; BTNode *pRoot = CreateBinTree(str, strlen(str),'#'); printf("Preorder ergodic results: "); PreOrder(pRoot); printf("\n"); printf("Intermediate order traversal results: "); InOrder(pRoot); printf("\n"); printf("Postorder ergodic results: "); PostOrder(pRoot); printf("\n"); LeveIOrder(pRoot);//Sequence traversal results printf("Two fork tree%d Leaf nodes\n", GetLeafCount(pRoot));//Get the number of leaf nodes printf("Two fork tree%d Node\n", GetBinTreeSize(pRoot));//Finding the Number of Nodes in Binary Tree printf("The depth of a binary tree is%d\n", GetBinTreeHeight(pRoot));//Get the depth of the tree printf("Number of nodes in the third layer of binary tree%d\n", GetKLeveNodeCount1(pRoot, 3)); printf("Number of nodes in the third layer of binary tree%d\n", GetKLeveNodeCount2(pRoot, 3)); BTNode * Find = BinaryTreeFind(pRoot,'B');//Find the node for B printf("The range found is:%c\n", Find->data); Mirror(pRoot);//Mirror 1 Sequence Traversal LeveIOrder(pRoot);//Sequence traversal results MirrorNor(pRoot);//Mirror 2 Preorder Traversal LeveIOrder(pRoot);//Sequence traversal results BTNode*p = CopyBinTree(pRoot);//Copy LeveIOrder(p);//Sequence traversal results DestroyBinTree(pRoot);//Destruction } int main() { TestBinTree(); system("pause"); return; }
Output results: