160 likes | 319 Views
アルゴリズムとデータ構造 補足資料 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 個分.
E N D
アルゴリズムとデータ構造補足資料12-2「2分木」アルゴリズムとデータ構造補足資料12-2「2分木」 横浜国立大学 理工学部 数物・電子情報系学科 富井尚志
struct tree { int key; struct tree *left; struct tree *right; }; key 21 の「ひな形」(型) NULL left right NULL
1.メモリに割当てる p = (structtree *)malloc(sizeof(structtree)); 2.その量は、”struct tree”型1個分 3.mallocの戻り値は、割当てたメモリの先頭アドレス 4.そのアドレス(参照先)の中身は “struct tree”型として、「キャスト」(型変換) 5.“struct tree”型へのポインタとして、アドレスを代入 この書き方は、憶えましょう。 結果は ↓これ(1個割当て) p key left right 要するに、 新しく「箱」ができる。 この箱に名前(変数名)はない。 だから、ポインタ変数pで指し示しておく必要がある。
#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; }
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
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