Server for Information Technologies ������ ��������������
������� �������������� ����������
(095) 932-9212, 932-9213, 939-0783
E-mail: [email protected]
������ �������� ����(!) ������������� ���������� CIT Forum CD-ROM

TSEARCH(3C)

��������
tsearch, tfind, tdelete, twalk - ���������� ��������� ��������� ������

���������

	#include <search.h>
	
	char *tsearch ((char *) key, (char **) rootp, compar)
	int (*compar) ( );
	
	char *tfind ((char *) key, (char **) rootp, compar)
	int (*compar) ( );
	
	char *tdelete ((char *) key, (char **) rootp, compar)
	int (*compar) ( );
	
	char *twalk ((char *) root, action)
	void (*action) ( );

��������
������� tsearch, tfind, tdelete � twalk ������������� ��� ���������� �������� ��� ��������� ��������� ������. ������� ����������� �� ������ ����������, ��������� � ����� �. �����: ��������� ���������������� ��� ���. �. 3. ����������, �����. - �.: ���, 1978. ������ 6.2.2, ��������� T � D. �������� ��������� ����������� � ������� �������, ��������������� �������������. ������� ��������� ���������� � ����� ����������� - ����������� �� ������������ ��������. � ������������ � ���, ����� ����� ����� ��� ����������: ������� ����, ������ ���� ��� ������� ����, ������ �������� ��������� �������, ������ ��� ������� �� ��������� �� �������. � ��������� �� ����������� ������ ����������� ������ ����, ������� ��������, � ���������� � ������������ ���������, ����� ��������� ������������ ������.

������� tsearch ������������ ��� ���������� ������ � ������� � ����. �������� key �������� ���������� �� ������� ������ (����). ���� � ������ ���� ���� � �������, ������� �������, �� ����������� ������� ������ ��������� �� ���� ����, ������ ����� �������� �������� ��������� �� ������. � ��������� ������ � ������ ����������� ���� �� ������� �� ������� ������ � ������������ ��������� �� ����. �������, ��� ���������� ������ ���������, ������� ���������� ��������� ���� ������ ������� ������. �������� rootp ��������� �� ����������, ������� �������� ���������� �� ������ ������. �������� NULL ����������, �� ������� ��������� rootp, �������� ������ ������; � ���� ������ ���������� ��������������� ������ ��������� �� ������, ������� �������� � ����� ������ ������.

������� ������� tsearch, ������� tfind ������������ ����� ������ � ������, ��������� � ������ ������ ��������� �� ����. ������ � ������ ���������� ������ ������� tfind ���������� ������ ��������� NULL. ��������� ��� ������� tfind ����� ��, ��� � ��� ������� tsearch.

������� tdelete ������� ���� �� ��������� ������ ������. ��������� ����� ��, ��� � ��� ������� tsearch. ����������, �� ������� ��������� rootp, ����������, ���� ��������� ���� �������� ������ ������. ������� tdelete ���������� ��������� �� ������ ���������� ���� ��� ������ ��������� NULL, ���� ���� �� ������.

������� twalk ������������ ����� ��������� ������ ������. �������� root ��������� �� ������ ��������������� ������. ����� ���� ����� ���� ����������� � �������� ����� ��� ������ ���������������� ���������. �������� action - ��� �������, ������� ���������� ��� ������� ����. ��� � ���� ������� ����� ��� ���������. ������ ���������� ������ ����� �������� ����. ������ �������� - ��� �������� �������������� ���� ������, ������������� �� ���������� ����� <search.h> ���

	typedef enum {preorder, postorder, endorder, leaf} VISIT

�������� ����������, ������� ��� (������, ������ ��� ������) �������������� ������ � ���� (�� ����� ������ ������ � ������� � ����� �������) ��� ����������, ��� ���� �������� ������. ������ �������� - ��� ������� ���� � ������ (� �������������, ��� ������ ����� ������� 0).

��������� �� ���� � ������ ������ ������ ����� ��� "��������� �� �������" � ����������������� � ���� "��������� �� ������". ����������, ������������ �������� ������� ��������������� � ���� "��������� �� �������", ���� ��� � ����������� ����� "��������� �� ������".

������
��������� ��������� ��������� ������� �������� � ���������� � ������ ���������, ���������� ��������� �� ������ ������� � �� �����. ����� �������������� ����� ������ � ��������������� � ���������� ������� ����������� ������� �������� � �� �������.

	#include <search.h>
	#include <stdio.h>
	
	struct node {    /* � ������ ����� ������������ ���������
	                  �� ��� ��������� */
	char *string;
	int  length;
	};
	
	char string_space [10000]; /* ������������ ��� ��������
	                            ������� �������� */
	struct node nodes [500];   /* ������������ ��� ��������
	                            �������� */
	struct node *root=NULL;    /* ��������� �� ������ */
	
	main()
	{
	char *strptr = string_space;
	struct node *nodeptr = nodes;
	void print_node (), twalk ();
	int i = 0, node_compare ();
	
	while (gets (strptr) != NULL && i++ < 500) {
	
	  /* ������������� ��������� */
	  nodeptr -> string = strptr;
	  nodeptr -> length = strlen(strptr);
	
	  /* ��������� ��������� � ������ */
	  (void) tsearch ((char *) nodeptr, (char **)&root,
	                  node_compare);
	
	  /* ��������������� ��������� */
	  strptr += nodeptr->length + 1;
	    nodeptr++;
	}
	twalk ((char *) root, print_node);
	}
	
	/* ������� ���������� ��� ��������� � ������������
	 � ���������� ���������������� ������� �������� */
	int node_compare (node1, node2)
	char *node1, *node2;
	{
	return strcmp (((struct node *) node1)->string,
	               ((struct node *) node2)->string);
	}
	
	/* ������� ������������� ���� ��� ������ ������ � ���� */
	void print_node (node, order, level)
	char **node;
	VISIT order;
	int level;
	{
	if (order == preorder || order == leaf) {
	  (void) printf ("string = %20s, length = %d\n",
	            (*((struct node **)node)) -> string,
	            (*((struct node **)node)) -> length);
	}
	}

��. �����
bsearch(3C), hsearch(3C), lsearch(3C).

�����������
������� tsearch ���������� ������ ��������� NULL, ���� �� ������� ���������� ������������ ��� �������� ������ ����.

������� tfind � tdelete ���������� ������ ��������� NULL, ���� �������� rootp ����� NULL.

���� ������ �������, �� ������� tsearch � tfind ���������� ��������� �� ���. � ������ ���������� ������ ������� tfind ���������� ������ ��������� NULL, � ������� tsearch ���������� ��������� �� ����������� ������.

���������������
�������� root ������� twalk ����� �� ���� ������� ��������� ��������� ������, ��� �������� rootp ������� tsearch � tdelete.

������� preorder, postorder � endorder ����� ����������� ������. ������� tsearch ���������� ������� preorder, postorder � endorder ��� �����������, ��������������, ��������� �������: ������ � ���� ����� �������� � ������-���� ��� �������, ������ � ���� ����� ������� � ��� ������ ������� � ����� �������� � ������� �������, ������ � ���� ����� ������� � ����� ��������. ����� ��������� ������� ������������ ��� �������� ������� ������ ������, ��� ����� �������� � ��������.

�����������
���� ���������� ������� �������� ��������� �� ������ ������, �� ����������� ����� ��������������.
Comments: [email protected]
Designed by Andrey Novikov
Copyright © CIT