Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > กระทู้สำหรับภาษาต่าง ๆ > สอบถามการเขียนโปรแกรมภาษาซี โครงสร้างแบบต้นไม้ค่ะ



สอบถามการเขียนโปรแกรมภาษาซี โครงสร้างแบบต้นไม้ค่ะ

 
Topic : 126048



โพสกระทู้ ( 33 )
บทความ ( 0 )



สถานะออฟไลน์



โจทย์บอกว่า
1.ให้สามารถกดย้อนกลับ ให้ดำเนินการคำสั่งตรงข้ามกับประวัติได้ทันทีโดยไม่จำเป็นต้องรักษา
โครงสร้างของต้นไม่เดิม
2.สามารถแสดงประวัติการทำงานโดนแสดงครั้งล่าสุดก่อน

ลองเขียนแล้วโปรแกรม ERROR ตลอดเลยค่ะ โดยตอนนี้สามารถ Insert กับ Delete ได้ แต่ยังไม่สามารถ แสดงประวัติ และกดย้อนกลับได้ค่ะ
ช่วยแนะนำหน่อยนะคะ ขอบคุณค่ะ

Code
#include <stdio.h> // #include<conio.h> #include<stdlib.h> #include<string.h> typedef struct Tree Tree; struct Tree { Tree * left, * right; int element; }; Tree *make_empty(Tree *t) { if (t != NULL) { make_empty(t->left); make_empty(t->right); free(t); } return NULL; } Tree *find_min(Tree *t) { if (t == NULL) { return NULL; } else if (t->left == NULL) { return t; } else { return find_min(t->left); } } Tree *find_max(Tree *t) { if (t == NULL) { return NULL; } else if (t->right == NULL) { return t; } else { return find_max(t->right); } } Tree *find(int elem, Tree *t) { if (t == NULL) { return NULL; } if (elem < t->element) { return find(elem, t->left); } else if (elem > t->element) { return find(elem, t->right); } else { return t; } } Tree * insert(int value, Tree * t) { Tree * new_node; if (t == NULL) // insert the first node { new_node = (Tree *) malloc (sizeof (Tree)); if (new_node == NULL) { return t; } new_node->element = value; new_node->left = new_node->right = NULL; return new_node; } if (value < t->element) // less than { t->left = insert(value, t->left); } else if (value > t->element) // greater than { t->right = insert(value, t->right); } else // equal { //duplicate, ignore it return t; } return t; } Tree * deletenode(int value, Tree * t) { Tree * x; Tree *tmp_cell; if (t==NULL) return NULL; if (value < t->element){ t->left = deletenode(value, t->left); } else if (value > t->element){ t->right = deletenode(value, t->right); } else if (t->left && t->right){ // case 1 (t:LR): t has both left and right children tmp_cell = find_min(t->right); t->element = tmp_cell->element; t->right = deletenode(t->element, t->right); } else{ tmp_cell = t; if (t->left == NULL) t = t->right; else if (t->right == NULL) t = t->left; free(tmp_cell); } return t; } typedef struct asciinode_struct{ struct asciinode_struct * left, * right; int edge_length; int height; int lablen; int parent_dir; char label[11]; } asciinode; #define MAX_HEIGHT 1000 int lprofile[MAX_HEIGHT]; int rprofile[MAX_HEIGHT]; #define INFINITY (1<<20) int gap = 3; int print_next; int MIN (int X, int Y) { return ((X) < (Y)) ? (X) : (Y); } int MAX (int X, int Y) { return ((X) > (Y)) ? (X) : (Y); } asciinode * build_ascii_tree_recursive(Tree * t) { asciinode * node; if (t == NULL) return NULL; node = (asciinode *)malloc(sizeof(asciinode)); node->left = build_ascii_tree_recursive(t->left); node->right = build_ascii_tree_recursive(t->right); if (node->left != NULL) { node->left->parent_dir = -1; } if (node->right != NULL) { node->right->parent_dir = 1; } sprintf(node->label, "%d", t->element); node->lablen = strlen(node->label); return node; } //Copy the tree into the ascii node structre asciinode * build_ascii_tree(Tree * t) { asciinode *node; if (t == NULL) return NULL; node = build_ascii_tree_recursive(t); node->parent_dir = 0; return node; } //Free all the nodes of the given tree void free_ascii_tree(asciinode *node) { if (node == NULL) return; free_ascii_tree(node->left); free_ascii_tree(node->right); free(node); } void compute_lprofile(asciinode *node, int x, int y) { int i, isleft; if (node == NULL) return; isleft = (node->parent_dir == -1); lprofile[y] = MIN(lprofile[y], x-((node->lablen-isleft)/2)); if (node->left != NULL) { for (i=1; i <= node->edge_length && y+i < MAX_HEIGHT; i++) { lprofile[y+i] = MIN(lprofile[y+i], x-i); } } compute_lprofile(node->left, x-node->edge_length-1, y+node->edge_length+1); compute_lprofile(node->right, x+node->edge_length+1, y+node->edge_length+1); } void compute_rprofile(asciinode *node, int x, int y) { int i, notleft; if (node == NULL) return; notleft = (node->parent_dir != -1); rprofile[y] = MAX(rprofile[y], x+((node->lablen-notleft)/2)); if (node->right != NULL) { for (i=1; i <= node->edge_length && y+i < MAX_HEIGHT; i++) { rprofile[y+i] = MAX(rprofile[y+i], x+i); } } compute_rprofile(node->left, x-node->edge_length-1, y+node->edge_length+1); compute_rprofile(node->right, x+node->edge_length+1, y+node->edge_length+1); } void compute_edge_lengths(asciinode *node) { int h, hmin, i, delta; if (node == NULL) return; compute_edge_lengths(node->left); compute_edge_lengths(node->right); /* first fill in the edge_length of node */ if (node->right == NULL && node->left == NULL) { node->edge_length = 0; } else { if (node->left != NULL) { for (i=0; i<node->left->height && i < MAX_HEIGHT; i++) { rprofile[i] = -INFINITY; } compute_rprofile(node->left, 0, 0); hmin = node->left->height; } else { hmin = 0; } if (node->right != NULL) { for (i=0; i<node->right->height && i < MAX_HEIGHT; i++) { lprofile[i] = INFINITY; } compute_lprofile(node->right, 0, 0); hmin = MIN(node->right->height, hmin); } else { hmin = 0; } delta = 4; for (i=0; i<hmin; i++) { delta = MAX(delta, gap + 1 + rprofile[i] - lprofile[i]); } if (((node->left != NULL && node->left->height == 1) || (node->right != NULL && node->right->height == 1))&&delta>4) { delta--; } node->edge_length = ((delta+1)/2) - 1; } h = 1; if (node->left != NULL) { h = MAX(node->left->height + node->edge_length + 1, h); } if (node->right != NULL) { h = MAX(node->right->height + node->edge_length + 1, h); } node->height = h; } void print_level(asciinode *node, int x, int level) { int i, isleft; if (node == NULL) return; isleft = (node->parent_dir == -1); if (level == 0) { for (i=0; i<(x-print_next-((node->lablen-isleft)/2)); i++) { printf(" "); } print_next += i; printf("%s", node->label); print_next += node->lablen; } else if (node->edge_length >= level) { if (node->left != NULL) { for (i=0; i<(x-print_next-(level)); i++) { printf(" "); } print_next += i; printf("/"); print_next++; } if (node->right != NULL) { for (i=0; i<(x-print_next+(level)); i++) { printf(" "); } print_next += i; printf("\\"); print_next++; } } else { print_level(node->left, x-node->edge_length-1, level-node->edge_length-1); print_level(node->right, x+node->edge_length+1, level-node->edge_length-1); } } void print_ascii_tree(Tree * t) { asciinode *proot; int xmin, i; if (t == NULL) return; proot = build_ascii_tree(t); compute_edge_lengths(proot); for (i=0; i<proot->height && i < MAX_HEIGHT; i++) { lprofile[i] = INFINITY; } compute_lprofile(proot, 0, 0); xmin = 0; for (i = 0; i < proot->height && i < MAX_HEIGHT; i++) { xmin = MIN(xmin, lprofile[i]); } for (i = 0; i < proot->height; i++) { print_next = 0; print_level(proot, -xmin, i); printf("\n"); } if (proot->height >= MAX_HEIGHT) { printf("(This tree is taller than %d, and may be drawn incorrectly.)\n", MAX_HEIGHT); } free_ascii_tree(proot); } Tree* initialTree(Tree *root){ int treeData[] = {20, 12, 30, 15, 40, 10, 25}; int value; int sizeOfTree = sizeof(treeData)/sizeof(treeData[0]); for(int i = 0; i < sizeOfTree; i++){ value = treeData[i]; root = insert(value, root); } print_ascii_tree(root); return root; } int main() { Tree * root; int i; int value; int select; root = NULL; // clrscr(); make_empty(root); root = initialTree(root); do{ printf("\n\nMENU: 1: Insert, 2: Delete, 3: Undo, 4. History 5: Exit\n"); printf("Select: "); fflush(stdin); scanf("%d", &select); switch(select){ case 1: printf("Insert value: "); scanf("%d", &value); root = insert(value, root); print_ascii_tree(root); // << AddHistory code >> break; case 2: printf("Delete value: "); scanf("%d", &value); root = deletenode(value, root); print_ascii_tree(root); // << AddHistory code >> break; case 3: printf("Undo: "); // << UNDO code >> print_ascii_tree(root); break; case 4: printf("History: \n"); // << ViewHistory code >> break; default: break; } }while(select != 5); make_empty(root); return 0; }




Tag : C, Objective-C


Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2017-01-15 22:35:41 By : Digi Do View : 3629 Reply : 1
 

 

No. 1



โพสกระทู้ ( 1,458 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook Blogger

c compiler หลายตระกูล มี debugger tools แบบ built-in มาให้
ปัญหาเจาะจงต้องดูที่ข้อความของ error ที่แจ้งมา แล้วแก้ไขตามที่ compiler ฟ้อง
ส่วนหนึ่งมาจากการพิมผิด พิมตก หรือเกิน...อีกส่วนมาจาก procedural error
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2020-07-30 13:15:30 By : PhrayaDev
 


   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : สอบถามการเขียนโปรแกรมภาษาซี โครงสร้างแบบต้นไม้ค่ะ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่