#include <limits.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>
typedef int ElementType; typedef struct TreeNode { ElementType Data; struct TreeNode *Left; struct TreeNode *Right; } * BinTree;
#define ElementQueueType BinTree typedef struct QueueNode { ElementQueueType data; struct QueueNode *next; } * QNode;
typedef struct QueueHeader { QNode front; QNode rear; } * Queue;
Queue CreateQueue(void) { Queue Q = (Queue)malloc(sizeof(struct QueueHeader)); Q->front = NULL; Q->rear = NULL; return Q; }
void AddQ(Queue Q, ElementQueueType item) { if (!Q) { return; } if (!item) { return; } QNode temp = (QNode)malloc(sizeof(struct QueueNode)); temp->data = item; temp->next = NULL; if (!Q->front && !Q->rear) { Q->front = temp; Q->rear = temp; } else { Q->rear->next = temp; Q->rear = temp; } }
int IsEmptyQ(Queue Q) { return !Q->front; }
ElementQueueType DeleteQ(Queue Q) { if (!Q) { return NULL; } if (IsEmptyQ(Q)) { return NULL; } QNode temp = Q->front; ElementQueueType tempdata; if (Q->front == Q->rear) { Q->front = NULL; Q->rear = NULL; } else { Q->front = Q->front->next; } tempdata = temp->data; free(temp); return tempdata; }
void PrintQueue(Queue Q) {
if (IsEmptyQ(Q)) { return; } printf("打印队列数据元素:\n"); QNode temp = Q->front;
for (; temp; temp = temp->next) { printf("0x%3lX ", (unsigned long int)temp->data & 0xFFF); } printf("\n"); }
#define ElementStackType BinTree typedef struct StackNode // node defination { ElementStackType data; struct StackNode *next; } * Stack;
int IsEmpty(Stack S) { return !S->next; }
Stack CreateStack(void) { Stack S = (Stack)malloc(sizeof(struct StackNode)); S->next = NULL; return S; }
ElementStackType Pop(Stack S) { ElementStackType temp; Stack tmepNode = S->next; if (tmepNode != NULL) { temp = tmepNode->data; S->next = tmepNode->next; free(tmepNode); return temp; } else { return NULL; } }
void MakeEmpty(Stack S) { while (S->next != NULL) { Pop(S); } } void DisposeStack(Stack S) { MakeEmpty(S); free(S); }
void Push(Stack S, ElementStackType X) { Stack tempNode = S->next; Stack newNode = (Stack)malloc(sizeof(struct StackNode)); newNode->data = X; if (tempNode != NULL) { newNode->next = tempNode; } else { newNode->next = NULL; } S->next = newNode; }
ElementStackType Top(Stack S) { if (S->next != NULL) { return S->next->data; } else { return NULL; } }
void PrintStack(Stack S) { Stack tep = S->next; while (tep != NULL) { printf("addr=0x%3lX dataadd=0x%3lX nextaddr=0x%3lX\n", (unsigned long int)tep & 0xFFF, (unsigned long int)tep->data & 0xFFF, (unsigned long int)tep->next & 0xFFF); tep = tep->next; } }
void PreOrderTraversal(BinTree BT) { if (BT) { printf("%d ", BT->Data); PreOrderTraversal(BT->Left); PreOrderTraversal(BT->Right); } }
void PreOrderPrintLeaves(BinTree BT) { if (BT) {
if (!BT->Left && !BT->Left) { printf("%d ", BT->Data); } PreOrderPrintLeaves(BT->Left); PreOrderPrintLeaves(BT->Right); } }
int FindTreeLength(BinTree BT, int tag) { if (BT) {
if (tag) { return 1 + FindTreeLength(BT->Left, tag); } else { return 1 + FindTreeLength(BT->Right, tag); } } else { return 0; } }
int FindTreeHeight(BinTree BT) { int rightlen, leftlen, maxlen; if (BT) { rightlen = FindTreeHeight(BT->Right); leftlen = FindTreeHeight(BT->Left); maxlen = leftlen > rightlen ? leftlen : rightlen; return maxlen + 1; } else { return 0; } }
void InOrderTraversal(BinTree BT) { if (BT) { PreOrderTraversal(BT->Left); printf("%d ", BT->Data); PreOrderTraversal(BT->Right); } }
void PostOrderTraversal(BinTree BT) { if (BT) { PreOrderTraversal(BT->Left); PreOrderTraversal(BT->Right); printf("%d ", BT->Data); } }
void PreStackTraversal(BinTree BT) { BinTree T = BT; Stack S = (Stack)malloc(sizeof(struct StackNode)); while (T || !IsEmpty(S)) { while (T) { Push(S, T); printf("%d ", T->Data); T = T->Left; } if (!IsEmpty(S)) { T = Pop(S); T = T->Right; } } DisposeStack(S); }
void InStackTraversal(BinTree BT) { BinTree T = BT; Stack S = (Stack)malloc(sizeof(struct StackNode)); while (T || !IsEmpty(S)) { while (T) { Push(S, T); T = T->Left; } if (!IsEmpty(S)) { T = Pop(S); printf("%d ", T->Data); T = T->Right; } } DisposeStack(S); }
void PostStackTraversal(BinTree BT) { BinTree T = BT; Stack S = (Stack)malloc(sizeof(struct StackNode)); while (T || !IsEmpty(S)) { while (T) { Push(S, T); T = T->Left; } if (!IsEmpty(S)) { T = Pop(S); if (T != BT) { printf("%d ", T->Data); } T = T->Right; } } printf("%d ", BT->Data); DisposeStack(S); }
void LevelOrderTraversal(BinTree BT) { Queue myqueue = CreateQueue(); BinTree T = BT; BinTree temp = NULL; AddQ(myqueue, T); while (!IsEmptyQ(myqueue)) { temp = DeleteQ(myqueue); printf("%d ", temp->Data); AddQ(myqueue, temp->Left); AddQ(myqueue, temp->Right); } }
BinTree CreateTreeNode(int dat) { BinTree new = (BinTree)malloc(sizeof(struct TreeNode)); new->Data = dat; new->Left = NULL; new->Right = NULL; return new; } void ConnectNode(BinTree root, BinTree left, BinTree right) {
if (!root) { printf("error in null\n"); return; } root->Left = left; root->Right = right; }
BinTree CreateBinTree(void) { printf("Create Binary Tree like this:\n \ 1 A\n \ / \\ / \\ \n \ 2 3 B C \n \ / \\ / \\ / \\ / \\ \n \ 4 5 6 7 D E F G \n \ / \\ / \\ / \\ / \\ \n \ 8 9 10 11 H I J K \n \ "); BinTree A = CreateTreeNode(1); BinTree B = CreateTreeNode(2); BinTree C = CreateTreeNode(3); BinTree D = CreateTreeNode(4); BinTree E = CreateTreeNode(5); BinTree F = CreateTreeNode(6); BinTree G = CreateTreeNode(7); BinTree H = CreateTreeNode(8); BinTree I = CreateTreeNode(9); BinTree J = CreateTreeNode(10); BinTree K = CreateTreeNode(11); ConnectNode(A, B, C); ConnectNode(B, D, E); ConnectNode(D, H, NULL); ConnectNode(E, NULL, I); ConnectNode(C, F, G); ConnectNode(F, J, K); return A; }
BinTree BinaryTree = NULL; int main(int argc, char const *argv[]) { printf("\n \ Binary Tree Options\n \ (1).make new linked Binary Tree;\n \ (2).递归先序遍历;\n \ (3).递归中序遍历;\n \ (4).递归后序遍历;\n \ (5).循环先序遍历;\n \ (6).循环中序遍历;\n \ (7).循环后序遍历;\n \ (8).打印叶节点;\n \ (9).输出树高度;\n \ (a).层序遍历;\n \ (q).quit;\n \ "); while (1) { switch (getchar()) { case '1': BinaryTree = CreateBinTree(); printf("create success!\n"); break; case '2': printf("递归先序遍历:"); PreOrderTraversal(BinaryTree); printf("\n"); break; case '3': printf("递归中序遍历:"); InOrderTraversal(BinaryTree); printf("\n"); break; case '4': printf("递归后序遍历:"); PostOrderTraversal(BinaryTree); printf("\n"); break; case '5': printf("循环先序遍历:"); PreStackTraversal(BinaryTree); printf("\n\n"); break; case '6': printf("循环中序遍历:"); InStackTraversal(BinaryTree); printf("\n\n"); break; case '7': printf("循环后序遍历:"); PostStackTraversal(BinaryTree); printf("\n\n"); break; case '8': printf("打印叶节点:"); PreOrderPrintLeaves(BinaryTree); printf("\n\n"); break; case '9': printf("打印height:%d", FindTreeHeight(BinaryTree)); printf("\n\n"); break; case 'a': printf("层序遍历:"); LevelOrderTraversal(BinaryTree); printf("\n"); break; case 'q': exit(0); break; default: break; } } return 0; }
|