树型结构的建立与遍历
树型结构的遍历是树型结构算法的基础,本程序演示二叉树的存储结构的建立和遍历过程。
(1) 编写建立二叉树的二叉链表存储结构的程序,并(用广义表的形式)显示并保存二叉树;
(2) 采用二叉树的二叉链表存储结构,编写程序实现二叉树的先序、中序和后序遍历的递归和非递归算法以及层序遍历算法,并显示二叉树和相应的遍历序列;
(3) 在二叉树的二叉链表存储结构基础上,编写程序实现二叉树的先序或中序或后序线索链表存储结构建立的算法,并(用广义表的形式)显示和保存二叉树的相应的线索链表;
#include <stdio.h> #include <string.h> #include <stdlib.h> char s[105]; int tag = 0; int top; typedef struct node { char data; struct node *left,*right; }Node; typedef struct nodeth { char data; int ltag,rtag; struct nodeth* left,*right; }NodeTH; NodeTH *pre = NULL; typedef struct st { int flag; Node *ptr; }STACK; void printBTree(Node *T); void printBTreeTH(NodeTH *T); Node *contree1(); void PreOrder(Node *T); void InOrder(Node *T); void PostOrder(Node *T); void NPreOrder(Node *T); void NInOrder(Node *T); void NPostOrder(Node *T); void LeverOrder(Node *T); void InOrderTH(NodeTH *root); NodeTH *PreNext(NodeTH *p); NodeTH *InNext(NodeTH *p); NodeTH *PostNext(NodeTH *p); NodeTH *Copy(Node *T); NodeTH *Locate(char ch,NodeTH *root); NodeTH *InOrderTHING(NodeTH *sroot); NodeTH *Leftest(NodeTH *T); void PreVisit(NodeTH *root); void InVisit(NodeTH *droot); void PostVisit(NodeTH *droot); int main() //读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树 { Node *head=NULL; Node *chead=NULL; NodeTH *root,*droot; root = (NodeTH *)malloc(sizeof(NodeTH)); int choice; printf("Begin create1: enter preorder root: \n"); scanf("%s",s); head = contree1(); chead = head; printf("Create Finished!\n\n"); root = Copy(chead); droot = InOrderTHING(root); //线索二叉树的根节点 printf("Please input your choice:"); scanf("%d",&choice); while (choice != -1) { switch(choice) { case 1: { printBTree(head); printf("\n"); break; } case 2: { PreOrder(head); printf("\n"); printf("PreOrder Finished!\n"); break; } case 3: { InOrder(head); printf("\n"); printf("InOrder Finished!\n"); break; } case 4: { PostOrder(head); printf("\n"); printf("PostOrder Finished!\n"); break; } case 5: { NPreOrder(chead); printf("\n"); printf("NPreOrder Finished!\n"); break; } case 6: { NInOrder(chead); printf("\n"); printf("NInOrder Finished!\n"); break; } case 7: { NPostOrder(chead); printf("\n"); printf("NPostOrder Finished!\n"); break; } case 8: { LeverOrder(chead); printf("\n"); printf("LeverOrder Finished!\n"); break; } case 9: { printBTreeTH(root); printf("\n"); printf("InOrderTH Finished!\n"); break; } case 10: { char ch; NodeTH *pos,*next; printf("Please input a char you whose innext you want to look:\n"); scanf(" %c",&ch); pos = Locate(ch,droot); next = InNext(pos); if(next != NULL) { printf("%c\n",next->data); } else { printf("none point!\n"); } break; } case 11: { char ch; NodeTH *pos,*next; printf("Please input a char you whose prenext you want look\n"); scanf(" %c",&ch); pos = Locate(ch,droot); next= PreNext(pos); if(next == NULL) { printf("none prenext\n"); } else { printf("%c\n",next->data); } break; } case 12: { char ch; NodeTH *pos,*next; printf("Please input a char you whose postnext you want look\n"); scanf(" %c",&ch); pos = Locate(ch,droot); next= PreNext(pos); if(next == NULL) { printf("none postnext\n"); } else { printf("%c\n",next->data); } break; } case 13: { PreVisit(root); printf("\n"); printf("PreVisit finished!\n"); break; } case 14: { InVisit(droot); printf("\n"); printf("InVisit finished!\n"); break; } case 15: { PostOrder(head); printf("\n"); printf("PostVisit finished!\n"); break; } default: { printf("Input error!\n"); } } printf("Please input your choice:"); scanf("%d",&choice); } return 0; } Node *contree1() { Node *p; char c; c=s[tag++]; if(c=='#'||tag>strlen(s)) { return NULL; } else { p=(Node *)malloc(sizeof(Node)); p->data=c; p->left=contree1(); p->right=contree1(); } return p; } void printBTree(Node *T) { if (T != NULL) { printf("%c",T->data); if(T->left != NULL || T->right != NULL) { printf("("); printBTree(T->left); if(T->right != NULL) { printf(","); } printBTree(T->right); printf(")"); } } return ; } void PreOrder(Node *T) { if(T != NULL) { printf("%c",T->data); PreOrder(T->left); PreOrder(T->right); } } void InOrder(Node *T) { if(T != NULL) { InOrder(T->left); printf("%c",T->data); InOrder(T->right); } } void PostOrder(Node *T) { if(T != NULL) { PostOrder(T->left); PostOrder(T->right); printf("%c",T->data); } } void NPreOrder(Node *T) { Node *stc[100]; top = -1; while(T != NULL || top != -1) { while(T != NULL) { printf("%c",T->data); stc[++top] = T; T = T->left; } if(top != -1) { T = stc[top--]; T = T->right; } } } void NInOrder(Node *T) { Node *stc[100]; top = -1; while(T != NULL || top != -1) { while(T != NULL) { stc[++top] = T; T = T->left; } if(top != -1) { T = stc[top--]; printf("%c",T->data); T = T->right; } } } void NPostOrder(Node *T) { STACK stk[100]; top = -1; while(T != NULL || top != -1) { while(T != NULL) { top++; stk[top].ptr = T; stk[top].flag = 1; T = T->left; } while(top != -1 && stk[top].flag == 2) { T = stk[top--].ptr; printf("%c",T->data); if(T == stk[0].ptr) return; } if (top != -1) { stk[top].flag = 2; T = stk[top].ptr->right; } } } void LeverOrder(Node *T) { Node *Q[100]; Node *q; int front,rear; front = rear = 0; if(T==NULL) return ; Q[++rear] = T; while(front != rear) { q = Q[++front]; printf("%c",q->data); if(q->left != NULL) Q[++rear] = q->left; if(q->right != NULL) Q[++rear] = q->right; } } void InOrderTH(NodeTH *root) { if(root) { InOrderTH(root->left); root->ltag = root->left!=NULL?1:0; root->rtag = root->right!=NULL?1:0; if(pre) { if(root->ltag == 0) root->left = pre; if(pre->rtag == 0) pre->right = root; } pre = root; InOrderTH(root->right); } } NodeTH *InOrderTHING(NodeTH *sroot) { NodeTH *leftest; NodeTH *droot; droot = (NodeTH *)malloc(sizeof(NodeTH)); droot->data = '*'; InOrderTH(sroot); leftest = Leftest(sroot); droot->ltag = 1; droot->rtag = 0; droot->left = droot; droot->right = leftest; leftest->left = droot; return droot; } NodeTH *Leftest(NodeTH *T) { NodeTH *temp = (NodeTH *)malloc(sizeof(NodeTH)); temp = T; while(temp->ltag) { temp = temp->left; } return temp; } void printBTreeTH(NodeTH *T) //输出线索二叉树 { if (T != NULL) { printf("%c",T->data); if(T->ltag != 0 || T->rtag != 0) { printf("("); printBTreeTH(T->left); if(T->rtag != 0) { printf(","); } printBTreeTH(T->right); printf(")"); } } return ; } NodeTH* Copy(Node *T) { if(T == NULL) return NULL; NodeTH* temp; //头结点 temp = (NodeTH *)malloc(sizeof(NodeTH)); temp->data = T->data; temp->left = Copy(T->left); temp->right = Copy(T->right); return temp; } NodeTH *PreNext(NodeTH *p) { NodeTH *Q; if(p->right == NULL) { return NULL; } if(p->ltag == 1) { Q = p->left; } else { Q = p; while(Q->rtag==0) { Q = Q->right; } Q = Q->right; } return Q; } NodeTH *InNext(NodeTH *p) { NodeTH *Q; Q = p->right; if(p->rtag == 1) { while(Q->ltag == 1) { Q = Q->left; } } return Q; } NodeTH *Locate(char ch,NodeTH *root) { NodeTH *temp; temp = root; do{ temp = InNext(temp); if (temp != NULL) { if(temp->data == ch) { return temp; } } }while(temp != NULL); return NULL; } void PreVisit(NodeTH *root) { NodeTH *temp = (NodeTH *)malloc(sizeof(NodeTH)); temp = root; while(temp != NULL) { printf("%c",temp->data); temp = PreNext(temp); } } void InVisit(NodeTH *droot) { NodeTH *temp =(NodeTH *)malloc(sizeof(NodeTH *)); temp = droot; do{ temp = InNext(temp); if(temp!=NULL) { printf("%c",temp->data); } }while(temp != NULL); }