1 / 16

アルゴリズムとデータ構造 補足資料 12-2 「 2 分 木」

アルゴリズムとデータ構造 補足資料 12-2 「 2 分 木」. 横浜国立大学 理工 学部 数物・電子情報系学科 富井尚志. struct tree { int key; struct tree *left; struct tree *right; };. key. 21. の「ひな形」(型). NULL. left. right. NULL. 1.メモリに割当てる. p = ( struct tree *) malloc ( sizeof ( struct tree ));. 2 .その量は、 ” struct tree” 型 1 個分.

piper
Download Presentation

アルゴリズムとデータ構造 補足資料 12-2 「 2 分 木」

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. アルゴリズムとデータ構造補足資料12-2「2分木」アルゴリズムとデータ構造補足資料12-2「2分木」 横浜国立大学 理工学部 数物・電子情報系学科 富井尚志

  2. struct tree { int key; struct tree *left; struct tree *right; }; key 21 の「ひな形」(型) NULL left right NULL

  3. 1.メモリに割当てる p = (structtree *)malloc(sizeof(structtree)); 2.その量は、”struct tree”型1個分 3.mallocの戻り値は、割当てたメモリの先頭アドレス 4.そのアドレス(参照先)の中身は “struct tree”型として、「キャスト」(型変換) 5.“struct tree”型へのポインタとして、アドレスを代入 この書き方は、憶えましょう。 結果は ↓これ(1個割当て) p key left right 要するに、 新しく「箱」ができる。 この箱に名前(変数名)はない。 だから、ポインタ変数pで指し示しておく必要がある。

  4. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; }

  5. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; } root new_node

  6. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; } root key left right new_node

  7. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; } root 1 key left right new_node

  8. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; } root 1 key left right new_node key left right

  9. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; } root 1 key left right new_node 2 key NULL left right NULL

  10. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; } root 1 key left right new_node 2 key NULL left right NULL

  11. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; } root 1 key left right new_node 2 key key NULL left right NULL left right

  12. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; } root 1 key left right new_node key 2 key 3 NULL left right NULL NULL left right NULL

  13. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; } root 1 key left right new_node key 2 key 3 NULL left right NULL NULL left right NULL

  14. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; } root root->key 1 key root->right root->left left right new_node root->right->key root->left->key key 2 key 3 left NULL NULL right NULL left right NULL root->left->left root->right->left root->left->right root->right>right 2 <-(left)- 1 –(right)-> 3 root = 40ea0820, root->left = 40ea0830, root->right = 40ea0840

  15. #include<stdio.h> #include<stdlib.h> struct tree { int key; struct tree *left; struct tree *right; }; main() { struct tree *root, *new_node; root=(struct tree *)malloc(sizeof(struct tree)); root->key= 1; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 2; new_node->left = NULL; new_node->right=NULL; root->left = new_node; new_node = (struct tree *)malloc(sizeof(struct tree)); new_node->key = 3; new_node->left = NULL; new_node->right=NULL; root->right = new_node; printf(“%d <-(left)- %d -(right)-> %d\n”, root->left->key, root->key, root->right->key); printf(“root = %x, root->left=%x, root->right=%x\n”, root, root->left, root->right); return 0; } root root->key 1 key root->right root->left left right new_node root->right->key root->left->key key 2 key 3 left NULL NULL right NULL left right NULL root->left->left root->right->left root->left->right root->right>right 2 <-(left)- 1 –(right)-> 3 root = 40ea0810, root->left = 40ea0820, root->right = 40ea0830

  16. root->key 1 key root root->right root->left left right new_node root->right->key root->left->key key key 2 3 key left NULL left right NULL NULL left NULL right right key root->left->left root->right->left left root->left->right root->right>right right key left right

More Related