アルゴリズムとデータ構造 補足資料 13-2
「 2 分探索木への節点の追加」
探索木のオペレータ
• 探索木を探索する
• 探索木に節点を追加(挿入)する
• 探索木から節点を削除する
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y root NULL
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 7 root NULL
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 7 root NULL
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 7 root NULL
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
search(7, NULL) : 呼出1
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 7 t NULL
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 7 root NULL
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
search(7, NULL) : 呼出1
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 7 t
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 7 root NULL
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
search(7, NULL) : 呼出1
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 7 t
7 1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 7 root NULL
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
search(7, NULL) : 呼出1
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 7 t
7
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 7 root NULL
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
search(7, NULL) : 呼出1
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 7 t
7
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 7 root NULL
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 7 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
search(2, root) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 2 t
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
search(2, root) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 2 t
2 が入るのは、
7 の左部分木
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
search(2, root) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 2 t
2 が入るのは、
7 の左部分木
search(2, NULL) : 呼出 3
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
x 2 t NULL
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
search(2, root) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 2 t
2 が入るのは、
7 の左部分木
search(2, NULL) : 呼出 3
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 2 t
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
search(2, root) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 2 t
2 が入るのは、
7 の左部分木
search(2, NULL) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
x 2 t
2 1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
search(2, root) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 2 t
2 が入るのは、
7 の左部分木
search(2, NULL) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 2 t
2
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
search(2, root) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 2 t
2 が入るのは、
7 の左部分木
search(2, NULL) : 呼出 3
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
x 2 t
2
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
N U L L
1
search(2, root) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 2 t
2 が入るのは、
7 の左部分木
2
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
search(2, root) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 2 t
2 が入るのは、
7 の左部分木
2
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
search(2, root) : 呼出 2
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 2 t
2 が入るのは、
7 の左部分木
2
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2 が入るのは、
7 の左部分木
2
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2 が入るのは、
7 の左部分木
2
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 2 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2 が入るのは、
7 の左部分木
2
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2
N U L L
N U L L
1
search(9, root) : 呼出 4
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 9 t
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2
N U L L
N U L L
1
search(9, root) : 呼出 4
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 9 t
9 が入るのは、
7 の右部分木
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2
N U L L
N U L L
1
search(9, root) : 呼出 4
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 9 t
9 が入るのは、
7 の右部分木
search(9, NULL) : 呼出 5
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 9 t NULL
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2
N U L L
N U L L
1
search(9, root) : 呼出 4
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 9 t
9 が入るのは、
7 の右部分木
search(9, NULL) : 呼出 5
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
x 9 t
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2
N U L L
N U L L
1
search(9, root) : 呼出 4
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 9 t
9 が入るのは、
7 の右部分木
search(9, NULL) : 呼出 5
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 9 t
9 1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2
N U L L
N U L L
1
search(9, root) : 呼出 4
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 9 t
9 が入るのは、
7 の右部分木
search(9, NULL) : 呼出 5
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
x 9 t
9
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2
N U L L
N U L L
1
search(9, root) : 呼出 4
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 9 t
9 が入るのは、
7 の右部分木
search(9, NULL) : 呼出 5
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 9 t
9
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7
N U L L
1
2
N U L L
N U L L
1
search(9, root) : 呼出 4
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 9 t
9 が入るのは、
7 の右部分木
9
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
search(9, root) : 呼出 4
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 9 t
9 が入るのは、
7 の右部分木
9
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
search(9, root) : 呼出 4
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 9 t
9
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 9 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 1 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 1 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
search(1, root) : 呼出 6
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 1 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
search(1, root) : 呼出 6
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 1 t
1 が入るのは、
7 の左部分木
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 1 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
search(1, root) : 呼出 6
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
1 が入るのは、
7 の左部分木
search(1, t->left) : 呼出 7
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 1 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
search(1, root) : 呼出 6
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 1 t
1 が入るのは、
7 の左部分木
search(1, t->left) : 呼出 7
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
1 が入るのは、
2 の左部分木
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 1 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
search(1, root) : 呼出 6
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
1 が入るのは、
7 の左部分木
search(1, t->left) : 呼出 7
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
1 が入るのは、
2 の左部分木
search(1, NULL) : 呼出 8
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t NULL
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 1 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
search(1, root) : 呼出 6
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
x 1 t
1 が入るのは、
7 の左部分木
search(1, t->left) : 呼出 7
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
1 が入るのは、
2 の左部分木
search(1, NULL) : 呼出 8
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
x 1 t
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 1 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
search(1, root) : 呼出 6
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
1 が入るのは、
7 の左部分木
search(1, t->left) : 呼出 7
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
1 が入るのは、
2 の左部分木 1
1
search(1, NULL) : 呼出 8
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 1 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
search(1, root) : 呼出 6
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
x 1 t
1 が入るのは、
7 の左部分木
search(1, t->left) : 呼出 7
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
1 が入るのは、
2 の左部分木 1
1
search(1, NULL) : 呼出 8
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
x 1 t
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 1 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
search(1, root) : 呼出 6
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
1 が入るのは、
7 の左部分木
search(1, t->left) : 呼出 7
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
1 が入るのは、
2 の左部分木 1
N U L L
N U L L
1
search(1, NULL) : 呼出 8
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
探索木 (search tree)
main()
root = NULL;
while( ( y=get_data() )!= EOD ) root = search( y, root )
…
y 1 root
int a[] = { 7, 2, 9, 1, 6, 9, 8, …}
7 1
2
N U L L
N U L L
1
9
N U L L
N U L L
1
search(1, root) : 呼出 6
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
x 1 t
1 が入るのは、
7 の左部分木
search(1, t->left) : 呼出 7
if ( t == NULL ) {
t = (struct tree *)malloc(sizeof(struct tree));
t->key = x; t->count = 1;
t->left = NULL; t->right = NULL;
}
else if ( x < t->key )
t->left = search( x, t->left );
else if ( x > t->key )
t->right = search( x, t->right );
else
(t->count)++;
return (t);
x 1 t
1 が入るのは、
2 の左部分木