Created
October 15, 2013 06:45
-
-
Save guohai/6987500 to your computer and use it in GitHub Desktop.
binary tree operations from http://zhedahht.blog.163.com/blog/static/25411174200732975328975/ and more.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
struct SBinaryTreeNode // a node of the binary tree | |
{ | |
int m_nValue; // value of node | |
struct SBinaryTreeNode *m_pLeft; // left child of node | |
struct SBinaryTreeNode *m_pRight; // right child of node | |
}; | |
/////////////////////////////////////////////////////////////////////// | |
// Get depth of a binary tree | |
// Input: pTreeNode - the head of a binary tree | |
// Output: the depth of a binary tree | |
/////////////////////////////////////////////////////////////////////// | |
int TreeDepth(struct SBinaryTreeNode *pTreeNode) | |
{ | |
// the depth of a empty tree is 0 | |
if (!pTreeNode) | |
return 0; | |
// the depth of left sub-tree | |
int nLeft = TreeDepth(pTreeNode->m_pLeft); | |
// the depth of right sub-tree | |
int nRight = TreeDepth(pTreeNode->m_pRight); | |
// depth is the binary tree | |
return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1); | |
} | |
/////////////////////////////////////////////////////////////////////// | |
// Get number of the nodes | |
// Input: pTreeNode - the head of a binary tree | |
// Output: the number of the nodes | |
/////////////////////////////////////////////////////////////////////// | |
int NodesNumber(struct SBinaryTreeNode *pTreeNode) | |
{ | |
// the number of the nodes is 0 | |
if (!pTreeNode) | |
return 0; | |
return NodesNumber(pTreeNode->m_pLeft) + \ | |
NodesNumber(pTreeNode->m_pRight) + 1; | |
} | |
void Visit(struct SBinaryTreeNode *pTreeNode) | |
{ | |
if (!pTreeNode) | |
return; | |
printf(" %d", pTreeNode->m_nValue); | |
} | |
void PreOrderTraverse(struct SBinaryTreeNode *pTreeNode) | |
{ | |
if (pTreeNode == NULL) | |
return; | |
Visit(pTreeNode); // root | |
PreOrderTraverse(pTreeNode->m_pLeft); // left sub-tree | |
PreOrderTraverse(pTreeNode->m_pRight); // right sub-tree | |
} | |
void InOrderTraverse(struct SBinaryTreeNode *pTreeNode) | |
{ | |
if (pTreeNode == NULL) | |
return; | |
InOrderTraverse(pTreeNode->m_pLeft); // left sub-tree | |
Visit(pTreeNode); // root | |
InOrderTraverse(pTreeNode->m_pRight); // right sub-tree | |
} | |
void PostOrderTraverse(struct SBinaryTreeNode *pTreeNode) | |
{ | |
if (pTreeNode == NULL) | |
return; | |
PostOrderTraverse(pTreeNode->m_pLeft); // left sub-tree | |
PostOrderTraverse(pTreeNode->m_pRight); // right sub-tree | |
Visit(pTreeNode); // root | |
} | |
int main() | |
{ | |
struct SBinaryTreeNode *ROOT = malloc(sizeof(struct SBinaryTreeNode)); | |
struct SBinaryTreeNode *N1 = malloc(sizeof(struct SBinaryTreeNode)); | |
struct SBinaryTreeNode *N2 = malloc(sizeof(struct SBinaryTreeNode)); | |
struct SBinaryTreeNode *N3 = malloc(sizeof(struct SBinaryTreeNode)); | |
struct SBinaryTreeNode *N4 = malloc(sizeof(struct SBinaryTreeNode)); | |
struct SBinaryTreeNode *N5 = malloc(sizeof(struct SBinaryTreeNode)); | |
ROOT->m_pLeft = N1; | |
ROOT->m_pRight = N2; | |
ROOT->m_nValue = 10; | |
N1->m_pLeft = N3; | |
N1->m_pRight = NULL; | |
N1->m_nValue = 6; | |
N2->m_pLeft = N4; | |
N2->m_pRight = N5; | |
N2->m_nValue = 14; | |
N3->m_pLeft = NULL; | |
N3->m_pRight = NULL; | |
N3->m_nValue = 4; | |
N4->m_pLeft = NULL; | |
N4->m_pRight = NULL; | |
N4->m_nValue = 12; | |
N5->m_pLeft = NULL; | |
N5->m_pRight = NULL; | |
N5->m_nValue = 16; | |
/* | |
* 10 | |
* / \ | |
* 6 14 | |
* / / \ | |
* 4 12 16 | |
* | |
*/ | |
printf("binary tree depth: %d\n", TreeDepth(ROOT)); | |
printf("number of nodes: %d\n", NodesNumber(ROOT)); | |
PreOrderTraverse(ROOT); | |
printf("\n"); | |
InOrderTraverse(ROOT); | |
printf("\n"); | |
PostOrderTraverse(ROOT); | |
printf("\n"); | |
free(ROOT); | |
free(N1); | |
free(N2); | |
free(N3); | |
free(N4); | |
free(N5); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment