Created
May 30, 2017 19:21
-
-
Save skhozinova/aad4b90c5b289f87f39470b10409e1bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <conio.h> | |
//Постройте дерево бинарного поиска и определите есть ли в нем узел с заданным значением a. | |
struct Ttree | |
{ | |
int inf; | |
Ttree *left; | |
Ttree *right; | |
}; | |
//формирование дерева | |
void add(int x, Ttree *&tr) | |
{ | |
if (!tr) | |
{ | |
tr = (Ttree*)malloc(sizeof(Ttree)); | |
tr->inf = x; | |
tr->left = tr->right = NULL; | |
} | |
else if (x < tr->inf) add(x, tr->left); | |
else if (x > tr->inf) add(x, tr->right); | |
} | |
//прямой обход дерева(корень-левое-правое) | |
void preorder(Ttree *tr) | |
{ | |
if (tr) | |
{ | |
printf("%3d ", tr->inf); | |
preorder(tr->left); | |
preorder(tr->right); | |
} | |
} | |
//поиск бинарного дерева | |
void search( int x, Ttree *tr) | |
{ | |
if(!tr) printf("error!");//значение не найдено | |
else if(x<tr->inf) search(x,tr->left);//если искомое значение меньше значения в текущем узле, то ищем x рекурсивно слева | |
else if(x>tr->inf) search(x,tr->right);//то ищем x рекурсивно справа | |
else printf("%d",x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment