标签:efault color class type data cpp FN 基本 main
• 二叉树节点类型BTNode:
1 typedef struct node 2 { 3 char data; 4 struct node *lchild, *rchild; 5 } BTNode;
1 void CreateBTree(BTNode *&b, char *str) 2 { 3 BTNode *St[MaxSize], *p = NULL; 4 int top = -1, ch, k, j = 0; 5 b = NULL; 6 ch = str[j]; 7 while (ch != ‘\0‘) 8 { 9 switch (ch) 10 { 11 case ‘(‘: ++top; St[top] = p; k = 1; break; 12 case ‘,‘: k = 2; break; 13 case ‘)‘: --top; break; 14 default: p = (BTNode*)malloc(sizeof(BTNode)); 15 p->data = ch; p->lchild = p->rchild = NULL; 16 if (b == NULL) 17 b = p; 18 else 19 { 20 switch (k) 21 { 22 case 1: St[top]->lchild = p; break; 23 case 2: St[top]->rchild = p; break; 24 } 25 } 26 } 27 ++j; 28 ch = str[j]; 29 } 30 }
1 void DispBTree(BTNode *b) 2 { 3 if (b != NULL) 4 { 5 printf(" %c", b ->data); 6 if (b->lchild != NULL || b->rchild != NULL) 7 { 8 printf("("); 9 DispBTree(b->lchild); 10 if (b->rchild != NULL) 11 printf(","); 12 DispBTree(b->rchild); 13 printf(")"); 14 } 15 } 16 }
1 int HeightBTree(BTNode *b) 2 { 3 if (b != NULL) 4 { 5 int i = HeightBTree(b->lchild); 6 int j = HeightBTree(b->rchild); 7 return (i > j ? (i + 1) : (j + 1)); 8 } 9 return 0; 10 }
1 int Nodes(BTNode *b) 2 { 3 if (b == NULL) 4 return 0; 5 else if (b->lchild == NULL && b->rchild == NULL) 6 return 1; 7 else 8 return (Nodes(b->lchild) + Nodes(b->rchild) + 1); 9 }
1 int LeafNodes(BTNode *b) 2 { 3 if (b == NULL) 4 return 0; 5 else if (b->lchild == NULL && b->rchild == NULL) 6 return 1; 7 else 8 return (LeafNodes(b->lchild) + LeafNodes(b->rchild) ); 9 }
cpp
#include<stdio.h> #include<malloc.h> #define MaxSize 100 typedef struct node { char data; struct node *lchild, *rchild; } BTNode; void CreateBTree(BTNode *&b, char *str) { BTNode *St[MaxSize], *p = NULL; int top = -1, ch, k, j = 0; b = NULL; ch = str[j]; while (ch != ‘\0‘) { switch (ch) { case ‘(‘: ++top; St[top] = p; k = 1; break; case ‘,‘: k = 2; break; case ‘)‘: --top; break; default: p = (BTNode*)malloc(sizeof(BTNode)); p->data = ch; p->lchild = p->rchild = NULL; if (b == NULL) b = p; else { switch (k) { case 1: St[top]->lchild = p; break; case 2: St[top]->rchild = p; break; } } } ++j; ch = str[j]; } } void DispBTree(BTNode *b) { if (b != NULL) { printf(" %c", b ->data); if (b->lchild != NULL || b->rchild != NULL) { printf("("); DispBTree(b->lchild); if (b->rchild != NULL) printf(","); DispBTree(b->rchild); printf(")"); } } } int HeightBTree(BTNode *b) { if (b != NULL) { int i = HeightBTree(b->lchild); int j = HeightBTree(b->rchild); return (i > j ? (i + 1) : (j + 1)); } return 0; } int Nodes(BTNode *b) { if (b == NULL) return 0; else if (b->lchild == NULL && b->rchild == NULL) return 1; else return (Nodes(b->lchild) + Nodes(b->rchild) + 1); } int LeafNodes(BTNode *b) { if (b == NULL) return 0; else if (b->lchild == NULL && b->rchild == NULL) return 1; else return (LeafNodes(b->lchild) + LeafNodes(b->rchild) ); } int level(BTNode *b) { } int main() { BTNode *b; int h; char str[] = "A(B(D(G, H)),C(E(,I), F)"; CreateBTree(b, str); printf("二叉树:"); DispBTree(b); printf("\n"); h = HeightBTree(b); printf("二叉树的高度为:%d\n", h); h = Nodes(b); printf("节点个数: %d\n", h); h = LeafNodes(b); printf("叶子节点个数: %d\n", h); return 0; }
标签:efault color class type data cpp FN 基本 main
原文地址:https://www.cnblogs.com/sunbines/p/9016194.html