DESCRIPTION
The tdelete(), tfind(), tsearch(), and twalk() functions manipulate binary search trees. Comparisons are made with a user-supplied routine, the address of which is passed as the compar argument. This routine is called with two arguments, which are the pointers to the elements being compared. The application shall ensure that the user-supplied routine returns 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 be contained 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 accessed or stored. If there is a node in the tree whose element is equal to the value pointed to by key, a pointer to this found node shall be returned. Otherwise, the value pointed to by key shall be inserted (that is, a new node is created and the value of key is copied to this node), and a pointer to this node returned. Only pointers are copied, so the application shall ensure that the calling routine stores the data. The rootp argument points to a variable that points to the root node of the tree. A null pointer value for the variable pointed to by rootp denotes an empty tree; in this case, the variable shall 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 is not 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 a null pointer if the node is not found.
The twalk() function shall traverse a binary search tree. The root argument is a pointer to the root node of the tree to be traversed. (Any node in a tree may be used as the root for a walk below that node.) The argument action is the name of a routine to be invoked at each node. This routine is, in turn, called with three arguments. The first argument shall be the address of the node being visited. The structure pointed to by this argument is unspecified and shall not be modified by the application, but it shall be possible to cast a pointer-to-node into a pointer-to-pointer-to-element to access the element stored in the node. The second argument shall be a value from an enumeration data type:
typedef enum { preorder, postorder, endorder, leaf } VISIT;
(defined in ), depending on whether this is the first, second, or third time that the node is visited (during a depth-first, left-to-right traversal of the tree), or whether the node is a leaf. The third 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.
RETURN VALUE
If the node is found, both tsearch() and tfind() shall return a pointer to it. If not, tfind() shall return a 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 on entry.
The tdelete() function shall return a pointer to the parent of the deleted node, or a null pointer if the node is not found.
The twalk() function shall not return a value.
ERRORS
No errors are defined.
อธิบาย
ต้นไม้ค้นหาแบบทวิภาคใช้ฟังก์ชัน tdelete(), tfind(), tsearch() และ twalk() จะเปรียบเทียบกับการให้ผู้ใช้ประจำ อยู่ที่ถูกส่งผ่านเป็นอาร์กิวเมนต์ compar ขั้นตอนนี้เรียกว่า มีอาร์กิวเมนต์สอง ซึ่งเป็นตัวชี้ไปยังองค์ประกอบเปรียบเทียบ แอพลิเคชันจะให้แน่ใจว่า ขั้นตอนที่ให้ผู้ใช้ส่งกลับค่าจำนวนเต็มน้อยกว่า เท่ากับ หรือ 0 ตามว่าอาร์กิวเมนต์แรกจะพิจารณาน้อยกว่า เท่ากับ มากกว่า หรืออาร์กิวเมนต์ที่สองมากกว่า เปรียบฟังก์ชันเปรียบเทียบต้องไม่เทียบทุกไบต์ เพื่อกำหนดข้อมูลอาจมีอยู่ในองค์ประกอบนอกเหนือจากค่าที่เปรียบเทียบ
ฟังก์ชัน tsearch() จะสร้าง และเข้าถึงแผนภูมิ อาร์กิวเมนต์สำคัญเป็นตัวชี้ถึงองค์ประกอบที่จะเข้าถึง หรือจัดเก็บ ถ้ามีโหนในองค์ประกอบมีค่าเท่ากับค่าที่ชี้ไปตามคีย์ ชี้นี้พบโหนจะถูกส่งกลับ มิฉะนั้น จะแทรกค่าที่ชี้ไปตามคีย์ (นั่นคือ สร้างโหนดใหม่ และค่าของคีย์จะถูกคัดลอกโหนนี้), และส่งกลับค่าตัวชี้ไปโหนดนี้ ตัวชี้เฉพาะจะถูกคัดลอก ดังนั้นแอพลิเคชันจะให้แน่ใจว่า ขั้นตอนการเรียกจัดเก็บข้อมูล อาร์กิวเมนต์ rootp ชี้ไปตัวแปรที่ชี้ไปโหนดรากของทรี ค่าตัวชี้เป็น null สำหรับตัวแปรบ่งชี้ rootp แสดงแผนภูมิเป็นว่างเปล่า ในกรณีนี้ ตัวแปรจะถูกตั้งค่าให้ชี้ไปที่โหนดซึ่งจะเป็นรากของใหม่ทรี
เช่น tsearch() tfind() จะค้นหาโหนดในทรี ความตัวชี้นั้นถ้าพบ อย่างไรก็ตาม ถ้าไม่พบ tfind() จะกลับตัวชี้เป็น null อาร์กิวเมนต์สำหรับ tfind() จะเหมือนกับ() tsearch
ฟังก์ชัน tdelete() จะลบโหนตัวจากต้นไม้ค้นหาแบบทวิภาค อาร์กิวเมนต์ไม่เหมือนกับ tsearch() จะสามารถเปลี่ยนตัวแปรบ่งชี้ rootp ถ้าโหนดที่ถูกลบ รากของต้นไม้ ฟังก์ชัน tdelete() จะกลับตัวชี้ไปที่หลักของโหนดที่ถูกลบ หรือตัวชี้เป็น null ถ้าไม่พบโหน
ฟังก์ชัน twalk() จะข้ามต้นไม้ค้นหาแบบทวิภาค อาร์กิวเมนต์รากเป็นตัวชี้ไปโหนดรากของจะได้ไม่เหมือนกัน (โหนดใด ๆ ในต้นไม้อาจจะใช้เป็นรากเดินเล่นด้านล่างโหน) อาร์กิวเมนต์แอคชันเป็นชื่อของชุดคำสั่งจะเรียกในแต่ละโหนด ขั้นตอนนี้ จะ เรียกว่าอาร์กิวเมนต์ที่สามที่ อาร์กิวเมนต์แรกจะเป็นอยู่ของโหนที่การเยี่ยมชม โครงสร้างที่ชี้ไปตามนี้จะไม่สามารถระบุได้ และจะไม่สามารถปรับเปลี่ยนแอพลิเคชัน แต่ก็จะไปโยนชี้ไปโหนในองค์การชี้การชี้การประกอบถึงองค์ประกอบที่จัดเก็บในโหน อาร์กิวเมนต์ที่สองจะเป็นค่าจากการแจงนับข้อมูลชนิด:
เยี่ยมชม typedef enum {รับ postorder, endorder ลีฟ}
(defined in ) ขึ้นอยู่ กับว่านี้เป็นที่แรก สอง หรือสาม เวลาที่โหนเข้าชม (ในระหว่างที่ความลึกการ ซ้ายไปขวาข้ามต้น), หรือว่าโหนดที่เป็นโหนดปลายสุด อาร์กิวเมนต์ที่สามเป็นระดับของโหนดในทรี มีรากอยู่ระดับ 0
ถ้าฟังก์ชันเรียกเปลี่ยนแปลงตัวชี้ไปยังราก ผลคือไม่
กลับค่า
ถ้าพบโหน tsearch() และ tfind() จะตอบตัวชี้ ถ้า ไม่ tfind() จะกลับตัวชี้เป็น null tsearch() จะกลับตัวชี้ไปที่แทรกสินค้า
ตัวชี้เป็น null จะถูกส่งกลับ โดย tsearch() ถ้าไม่มีเนื้อที่ว่างเพียงพอเพื่อดำเนินการใหม่โหน
ตัวชี้เป็น null จะกลับ ทาง tdelete(), tfind(), tsearch() ถ้า rootp เป็นตัวชี้เป็น null บนรายการ
ฟังก์ชัน tdelete() จะกลับตัวชี้หลักของโหนดที่ถูกลบ, หรือตัวชี้เป็น null ถ้าไม่พบโหน
twalk() ฟังก์ชันจะส่งกลับค่า
พลาด
กำหนดข้อผิดพลาดไม่
การแปล กรุณารอสักครู่..
![](//thimg.ilovetranslation.com/pic/loading_3.gif?v=b9814dd30c1d7c59_8619)