diff --git "a/2224020102/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" "b/2224020102/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..221251e3465a1d0c3155e24767d80d726eb4a933 --- /dev/null +++ "b/2224020102/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" @@ -0,0 +1,203 @@ +#include +#include +#include +#define MaxSons 3 +typedef char ElemType; +typedef struct node +{ + ElemType data[15]; + struct node *sons[MaxSons]; +}TSonNode; +typedef struct +{ + char N[15]; + char n[15]; +}array; + +void ReadFile(array R[],FILE *fp,int &n) +{ + while((fscanf(fp,"%s",R[n].N))!=EOF&&(fscanf(fp,"%s",R[n].n))!=EOF) + n++; +} + +TSonNode *CreateTree(char str[],array R[],int n) +{ + TSonNode *t; + int k,i=0,j=0; + t=(TSonNode *)malloc(sizeof(TSonNode)); + strcpy(t->data,str); + for(k=0;ksons[k]=NULL; + while(isons[j]=CreateTree(R[i].n,R,n); + j++; + } + i++; + } + return t; +} + +void DispTree(TSonNode *t) +{ + int i=0; + if(t==NULL) + printf("此树为空树!\n"); + else + { + printf("%s",t->data); + if(t->sons[i]!=NULL) + { + printf("("); + for(i=0;isons[i]); + if(t->sons[i+1]!=NULL) + printf(","); + else + break; + } + printf(")"); + } + } +} + +void DestroyTree(TSonNode *t) +{ + if(t==NULL) + printf("此树为空树!\n"); + else + { + for(int i=0;isons[i]!=NULL) + DestroyTree(t->sons[i]); + else + break; + } + free(t); + } +} + +TSonNode *FindNode(TSonNode *t,char str[]) +{ + TSonNode *p; + if(t==NULL) + return NULL; + else + { + if(strcmp(t->data,str)==0) + return t; + else + { + for(int i=0;isons[i]!=NULL) + { + p=FindNode(t->sons[i],str); + if(p!=NULL) + return p; + } + } + return NULL; + } + } +} + +int ChildCount(TSonNode *p) +{ + int count=0; + for(int i=0;isons[i]!=NULL) + count++; + else + break; + } + return count; +} + +int LeafCount(TSonNode *p) +{ + int count=0; + if(p==NULL) + return 0; + else + { + if(p->sons[0]==NULL) + count++; + else + { + for(int i=0;isons[i]!=NULL) + count=count+LeafCount(p->sons[i]); + else + break; + } + } + } + return count; +} + +int LeafSumOfvalue(TSonNode *p) +{ + int sum=0; + if(p==NULL) + return 0; + else + { + if(p->sons[0]==NULL) + return atoi(p->data); + else + { + for(int i=0;isons[i]!=NULL) + sum+=LeafSumOfvalue(p->sons[i]); + else + break; + } + } + } + return sum; +} + +#include +#include +#include"tree.h" +#define MaxSize 66 +int main() +{ + int n=0; + TSonNode *t; + array R[MaxSize]; + FILE *fp; + if((fp=fopen("table.txt","r"))==NULL) + { + printf("error!cannot open the file!"); + exit(1); + } + printf("读取文件内容存入数组R中\n"); + ReadFile(R,fp,n); + printf("输出数组R:\n"); + for(int i=0;i +#include +using namespace std; +#define MaxSize 50 + +typedef struct node +{ + char data; + struct node *lchild; + struct node *rchild; +}BTNode; +void CreateBTree(BTNode *&b,const char *str) +{ + BTNode *St[MaxSize],*p; + int top=-1,k,j=0; + char ch; + b=NULL; + ch=str[j]; + while(ch!='\0') + { + switch(ch) + { + case '(':top++;St[top]=p;k=1;break; + case ')':top--;break; + case ',':k=2;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(")"); + } + } + } + void DestroyBTree(BTNode *&b) + { + if(b!=NULL) + { + DestroyBTree(b->lchild); + DestroyBTree(b->rchild); + free(b); + } + } +int SSonNodes(BTNode *b) +{ + int num1,num2,n; + if(b==NULL) + return 0; + else if((b->lchild==NULL&&b->rchild!=NULL)||(b->lchild!=NULL&&b->rchild==NULL)) + n=1; + else + n=0;/ + num1=SSonNodes(b->lchild); + num2=SSonNodes(b->rchild); + return (num1+num2+n); + } + int main() + { + BTNode *b; + CreateBTree(b,"A(B(D(,G)),C(E,F))"); + cout<<"二叉树为:"<{ + + String s; + int weight; + private HTreeNode left; + private HTreeNode right; + + + public HTreeNode(int weight){ + this.weight = weight; + } + + public HTreeNode(int weight, HTreeNode left, HTreeNode right){ + this.weight = weight; + this.left = left; + this.right = right; + } + + + public int compareTo(HTreeNode o){ + return new Integer(this.weight).compareTo(new Integer(o.weight)); + } + + + } + + + public void createHFMTree(int[] weights){ + + Queue nodeQueue = new PriorityQueue(); + nodes = new HTreeNode[weights.length]; + + for(int i=0; i 1){ + + HTreeNode left = nodeQueue.poll(); + HTreeNode right = nodeQueue.poll(); + + HTreeNode parent = new HTreeNode(left.weight + right.weight, left, right); + nodeQueue.add(parent); + + } + root = nodeQueue.poll(); + + } + public void output(HTreeNode head){ + if(head != null){ + System.out.println(head.weight); + output(head.left); + output(head.right); + } + } + public void encode(HTreeNode node, String s){ + if(node != null){ + node.s = s; + encode(node.left, node.s+"0"); + encode(node.right, node.s+"1"); + } + } + public void printTreeCode(String[] sa, int[] weights){ + for(int i=0; i> pathList; + private LinkedList path; + + public void test(TreeNode root) { + pathList = new ArrayList<>(); + path = new LinkedList<>(); + recurision(root); + } + + public void recurision(TreeNode node) { + path.add(node.val); + if (node.left == null && node.right == null) { + pathList.add(new ArrayList<>(path)); + return; + } + if (node.left != null) { + recurision(node.left); + path.removeLast(); + } + + if (node.right != null) { + recurision(node.right); + path.removeLast(); + } + } +} \ No newline at end of file