tdelete

NAME
tdelete, tfind, tsearch, twalk - manage a binary search tree
SYNOPSIS
[XSI] [Option Start] #include <search.h>

void *tdelete(const void *restrict
key, void **restrict rootp,
       int(*
compar)(const void *, const void *));
void *tfind(const void *
key, void *const *rootp,
       int(*
compar)(const void *, const void *));
void *tsearch(const void *
key, void **rootp,
       int (*
compar)(const void *, const void *));
void twalk(const void *
root,
       void (*
action)(const void *, VISIT, int)); [Option End]

DESCRIPTION

The tdelete(), tfind(), tsearch(), and twalk() functions manipulate binary search trees. Comparisonsare made with a user-supplied routine, the address of which is passed as the compar argument. This routine is called withtwo arguments, which are the pointers to the elements being compared. The application shall ensure that the user-supplied routinereturns an integer less than, equal to, or greater than 0, according to whether the first argument is to be considered less than,equal to, or greater than the second argument. The comparison function need not compare every byte, so arbitrary data may becontained in the elements in addition to the values being compared.

The tsearch() function shall build and access the tree. The key argument is a pointer to an element to be accessedor stored. If there is a node in the tree whose element is equal to the value pointed to by key, a pointer to this foundnode shall be returned. Otherwise, the value pointed to by key shall be inserted (that is, a new node is created and thevalue of key is copied to this node), and a pointer to this node returned. Only pointers are copied, so the applicationshall ensure that the calling routine stores the data. The rootp argument points to a variable that points to the root nodeof the tree. A null pointer value for the variable pointed to by rootp denotes an empty tree; in this case, the variableshall be set to point to the node which shall be at the root of the new tree.

Like tsearch(), tfind() shall search for a node in the tree, returning a pointer to it if found. However, if it isnot found, tfind() shall return a null pointer. The arguments for tfind() are the same as for tsearch().

The tdelete() function shall delete a node from a binary search tree. The arguments are the same as for tsearch().The variable pointed to by rootp shall be changed if the deleted node was the root of the tree. The tdelete()function shall return a pointer to the parent of the deleted node, or an unspecified non-null pointer if the deleted node was theroot node, or a null pointer if the node is not found.

If tsearch() adds an element to a tree, or tdelete() successfully deletes an element from a tree, the concurrentuse of that tree in another thread, or use of pointers produced by a previous call to tfind() or tsearch(), producesundefined results.

The twalk() function shall traverse a binary search tree. The root argument is a pointer to the root node of thetree to be traversed. (Any node in a tree may be used as the root for a walk below that node.) The argument action is thename of a routine to be invoked at each node. This routine is, in turn, called with three arguments. The first argument shall bethe address of the node being visited. The structure pointed to by this argument is unspecified and shall not be modified by theapplication, but it shall be possible to cast a pointer-to-node into a pointer-to-pointer-to-element to access the element storedin the node. The second argument shall be a value from an enumeration data type:

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

(defined in <search.h>), depending on whether this is the first, second, orthird time that the node is visited (during a depth-first, left-to-right traversal of the tree), or whether the node is a leaf. Thethird argument shall be the level of the node in the tree, with the root being level 0.

If the calling function alters the pointer to the root, the result is undefined.

If the functions pointed to by action or compar (for any of these binary search functions) change the tree, theresults are undefined.

These functions are thread-safe only as long as multiple threads do not access the same tree.

RETURN VALUE

If the node is found, both tsearch() and tfind() shall return a pointer to it. If not, tfind() shall returna null pointer, and tsearch() shall return a pointer to the inserted item.

A null pointer shall be returned by tsearch() if there is not enough space available to create a new node.

A null pointer shall be returned by tdelete(), tfind(), and tsearch() if rootp is a null pointer onentry.

The tdelete() function shall return a pointer to the parent of the deleted node, or an unspecified non-null pointer ifthe deleted node was the root node, or a null pointer if the node is not found.

The twalk() function shall not return a value.

ERRORS

No errors are defined.


The following sections are informative.
EXAMPLES

The following code reads in strings and stores structures containing a pointer to each string and a count of its length. It thenwalks the tree, printing out the stored strings and their lengths in alphabetical order.

#include <search.h>
#include <string.h>
#include <stdio.h>


#define STRSZ    10000
#define NODSZ    500


struct node {      /* Pointers to these are stored in the tree. */
    char    *string;
    int     length;
};


char   string_space[STRSZ];  /* Space to store strings. */
struct node nodes[NODSZ];    /* Nodes to store. */
void  *root = NULL;          /* This points to the root. */


int main(int argc, char *argv[])
{
    char   *strptr = string_space;
    struct node    *nodeptr = nodes;
    void   print_node(const void *, VISIT, int);
    int    i = 0, node_compare(const void *, const void *);


    while (gets(strptr) != NULL && i++ < NODSZ)  {
        /* Set node. */
        nodeptr->string = strptr;
        nodeptr->length = strlen(strptr);
        /* Put node into the tree. */
        (void) tsearch((void *)nodeptr, (void **)&root,
            node_compare);
        /* Adjust pointers, so we do not overwrite tree. */
        strptr += nodeptr->length + 1;
        nodeptr++;
    }
    twalk(root, print_node);
    return 0;
}


/*
 *  This routine compares two nodes, based on an
 *  alphabetical ordering of the string field.
 */
int
node_compare(const void *node1, const void *node2)
{
    return strcmp(((const struct node *) node1)->string,
        ((const struct node *) node2)->string);
}


/*
 *  This routine prints out a node, the second time
 *  twalk encounters it or if it is a leaf.
 */
void
print_node(const void *ptr, VISIT order, int level)
{
    const struct node *p = *(const struct node **) ptr;


    if (order == postorder || order == leaf)  {
        (void) printf("string = %s,  length = %d\n",
            p->string, p->length);
    }
}

APPLICATION USAGE

The root argument to twalk() is one level of indirection less than the rootp arguments to tdelete()and tsearch().

There are two nomenclatures used to refer to the order in which tree nodes are visited. The tsearch() function usespreorder, postorder, and endorder to refer respectively to visiting a node before any of its children, afterits left child and before its right, and after both its children. The alternative nomenclature uses preorder,inorder, and postorder to refer to the same visits, which could result in some confusion over the meaning ofpostorder.

Since the return value of tdelete() is an unspecified non-null pointer in the case that the root of the tree has beendeleted, applications should only use the return value of tdelete() as indication of success or failure and should notassume it can be dereferenced. Some implementations in this case will return a pointer to the new root of the tree (or to an emptytree if the deleted root node was the only node in the tree); other implementations return arbitrary non-null pointers.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误是由于尝试删除一个不存在的Tcl命令而导致的。可能是因为在尝试删除命令之前,它已经被删除了或者从未创建过。您可以检查一下代码中是否存在这样的情况,并确保在删除命令之前,它已经被正确地创建和使用。 ### 回答2: Tkinter是Python的标准GUI库,它基于Tcl/Tk开发。Tcl是一种脚本语言,它能够创建GUI并与用户交互,而Tk是一个Tcl库,封装了可视化组件和相关的操作,例如响应鼠标键盘等。 _tkinter.tclerror: can't delete tcl command是Tkinter中的一个错误,它通常发生在尝试删除一个已经存在的Tcl命令时。这个错误表示在Tcl中,有一个命令正在被使用,并且不能被删除。 在Tkinter中,可以使用deletecommand()方法删除一个命令,这个方法需要传入要删除的命令名称作为参数。但是,如果正在运行的程序依赖于这个命令,那么它就不能被删除。这样的情况可能会发生在以下几种情况下: 1、命令正在被其他程序使用,例如在Tkinter窗口中使用的按钮等控件。 2、命令正在被使用的回调函数中运行。 3、命令定义的参数错误或不正确,导致Tcl无法删除它。 如果遇到_tkinter.tclerror: can't delete tcl command的错误,可以尝试以下几种方法: 1、检查是否有其他程序正在使用这个命令,并且停止使用它。 2、检查命令定义是否正确,如果不正确,应该修复它。 3、避免在运行回调函数时使用deletecommand()方法删除命令,应该在回调函数完成后再删除命令。 4、在删除命令之前,先使用exists()方法检查它是否存在,如果不存在,则不需要删除。 总之,_tkinter.tclerror: can't delete tcl command是Tkinter中的一个常见错误,通常发生在使用deletecommand()方法删除命令时。如果遇到这个错误,应该仔细检查程序中使用的命令是否存在问题,并且逐个排除,直到找到问题的原因并修复它。 ### 回答3: 这个错误是由tkinter中的一个tcl命令未被成功删除引起的。tcl是用于解释执行Tcl脚本的编程语言,能够让Python中的GUI框架tkinter进行跨平台的窗口操作。 在Python中,当我们创建一个tcl命令,例如创建一个Button控件时,tkinter框架会在tcl解释器中添加一个与该控件相关联的tcl命令,并且这个命令会在该控件使用完毕后自动删除。然而,在某些情况下,这个命令并没有被成功删除,可能是因为我们在控件被销毁前使用了它,或者是出现了其他的错误导致命令没有被成功删除。 当我们尝试删除这个未成功删除的tcl命令时,就会出现_tkinter.tclerror: can't delete tcl command的错误提示,这时我们需要找到未被成功删除的这个命令,并手动将它删除,或者重新启动Python解释器来清空所有未被成功删除的tcl命令。 为了避免出现这种问题,我们应该在使用完控件后及时销毁它,或者使用更高级的Python GUI框架,如PyQt和wxPython,它们对于控件的销毁和内存管理更加的完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值