Binary Search tree implementation of sequential clues in the C Language

Source: Internet
Author: User

/* Threaded_tree.c -- the implementation file of the clue tree */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include "threaded_tree.h" </P> <p>/* local function declaration */<br/> static node * make_node (const item * const pitem); <br/> static int left_is_greater_than_right (const item left, const item right); <br/> static int left_is_lesser_than_right (const item left, const item right ); <br/> static int left_is_assist_to_right (const item le FT, const item right); <br/> static node * find_predecessor (const tree * const ptree, node * const parent, const node * const pnode ); <br/> static node * find_successor (const tree * const ptree, node * const parent, const node * const pnode ); <br/> static node * the_parent_of (const tree * const ptree, const node * const pnode); <br/> static void threading (TREE * const ptree, node ** const pre Vious); </P> <p>/* interface function definition */</P> <p> void initializetree (TREE * const ptree) <br/>{< br/> * ptree = NULL; <br/>}</P> <p> int treeisempty (const tree * const ptree) <br/>{< br/> return null = * ptree; <br/>}</P> <p> int insert (TREE * const ptree, const item * const pitem) <br/>{< br/> node * new_node; <br/> node * parent, * scan = * ptree; </P> <p> If (null = (new_node = make_node (pitem) <br/> return 0; <br /> If (treeisempty (ptree) <br/>{< br/> * ptree = new_node; <br/> (* ptree)-> left = NULL; <br/> (* ptree)-> right = NULL; <br/> return 1; <br/>}< br/> while (scan! = NULL) <br/>{< br/> parent = scan; <br/> If (left_is_greater_than_right (* pitem, scan-> item )) <br/>{< br/> If (thread = scan-> right_tag) <br/>{< br/> parent = scan; <br/> break; <br/>}< br/> else <br/> scan = scan-> right; <br/>}< br/> else if (left_is_lesser_than_right (* pitem, scan-> item) <br/>{< br/> If (thread = scan-> left_tag) <br/>{< br/> parent = scan; <br/> break; <br/>}< br/> else <B R/> scan = scan-> left; <br/>}< br/>/* duplicate data */<br/> else <br/> {<br/> free (new_node ); <br/> return 0; <br/>}< br/> If (left_is_greater_than_right (* pitem, parent-> item )) <br/>{< br/> parent-> right_tag = link; <br/> parent-> right = new_node; <br/>/* This is used to automatically complete the central order clue when adding the knot. currently, the clue function cannot make a clue to the already clue tree */<br/>/* parent-> right-> left = find_predecessor (ptree, parent, parent-> right); <br/> P Arent-> right = find_successor (ptree, parent, parent-> right ); */<br/>}< br/> else <br/>{< br/> parent-> left_tag = link; <br/> parent-> left = new_node; <br/>/* parent-> left = find_predecessor (ptree, parent, parent-> left ); <br/> parent-> left-> right = find_successor (ptree, parent, parent-> left ); */<br/>}</P> <p> return 1; <br/>}</P> <p> node * findmin (const tree * C Onst ptree) <br/>{< br/> node * parent = * ptree, * scan = * ptree; </P> <p> while (scan! = NULL) <br/>{< br/> parent = scan; <br/> If (link = scan-> left_tag) <br/> scan = scan-> left; <br/> else <br/> break; <br/>}</P> <p> return parent; <br/>}</P> <p> node * findmax (const tree * const ptree) <br/>{< br/> node * parent = NULL, * scan = * ptree; </P> <p> while (scan! = NULL) <br/>{< br/> parent = scan; <br/> If (link = scan-> right_tag) <br/> scan = scan-> right; <br/> else <br/> break; <br/>}</P> <p> return parent; <br/>}</P> <p>/* This function cannot maintain tree clue after deleting a node */<br/> tree Delete (TREE tree, const item) <br/>{< br/> node * temp; </P> <p> If (null = tree) <br/> return NULL; <br/> If (left_is_greater_than_right (item, tree-> item) <br/> tree-> right = Delete (tree-> ri Ght, item); <br/> else if (left_is_lesser_than_right (item, tree-> item) <br/> tree-> left = Delete (tree-> left, ITEM ); <br/> else <br/> {<br/> If (link = tree-> left_tag & link = tree-> right_tag & tree-> left! = NULL & tree-> right! = NULL) <br/>{< br/> temp = findmin (& tree-> right); <br/> tree-> item = temp-> item; <br/> tree-> right = Delete (tree-> right, temp-> item ); <br/>}< br/> else <br/> {<br/> temp = tree; <br/> If (link = tree-> left_tag & null = tree-> left) <br/> tree = tree-> right; <br/> else if (link = tree-> right_tag & null = tree-> right) <br/> tree = tree-> left; <br/> else <br/> tree = NULL; <Br/> free (temp); <br/>}</P> <p> return tree; <br/>}</P> <p> void threadedtraversal (const tree, void (* pfun) (const item )) <br/>{< br/> node * scan = tree; </P> <p> while (scan! = NULL) <br/>{< br/> while (link = scan-> left_tag) <br/> scan = scan-> left; <br/> (* pfun) (scan-> item); <br/> while (thread = scan-> right_tag & scan-> right! = NULL) <br/>{< br/> scan = scan-> right; <br/> (* pfun) (scan-> item ); <br/>}< br/> scan = scan-> right; <br/>}</P> <p> void inordertraversal (const tree, void (* pfun) (const item) <br/>{< br/> If (tree! = NULL) <br/>{< br/> If (link = tree-> left_tag) <br/> inordertraversal (tree-> left, pfun ); <br/> (* pfun) (tree-> item); <br/> If (link = tree-> right_tag) <br/> inordertraversal (tree-> right, pfun); <br/>}</P> <p> void inorderthreading (TREE * const ptree) <br/>{< br/> node * Previous = NULL; </P> <p> threading (ptree, & previous ); <br/>}</P> <p>/* local function definition */</P> <p> static node * make_node (C Onst item * const pitem) <br/>{< br/> node * new_node; </P> <p> new_node = (node *) malloc (sizeof (node )); <br/> If (null = new_node) <br/> return NULL; <br/> else <br/>{< br/> new_node-> item = * pitem; <br/> new_node-> left = NULL; <br/> new_node-> right = NULL; <br/> new_node-> left_tag = thread; <br/> new_node-> right_tag = thread; <br/> return new_node; <br/>}</P> <p> static int left_ I S_greater_than_right (const item left, const item right) <br/>{< br/> return left> right; <br/>}</P> <p> static int left_is_lesser_than_right (const item left, const item right) <br/>{< br/> return left <right; <br/>}</P> <p> static int left_is_assist_to_right (const item left, const item right) <br/>{< br/> return left = right; <br/>}</P> <p> static node * find_predecessor (const tree * const ptree, Node * const parent, const node * const pnode) <br/>{< br/> node * predecessor; </P> <p> If (pnode = parent-> left) <br/>{< br/> If (pnode = findmin (ptree )) <br/> predecessor = NULL; <br/> else <br/> predecessor = the_parent_of (ptree, parent ); <br/>}< br/> else if (pnode = parent-> right) <br/> predecessor = parent; </P> <p> return predecessor; <br/>}</P> <p> static node * find_successor (const tree * Const ptree, node * const parent, const node * const pnode) <br/>{< br/> node * successor; </P> <p> If (pnode = parent-> left) <br/> successor = parent; <br/> else if (pnode = parent-> right) <br/>{< br/> If (pnode = findmax (ptree) <br/> successor = NULL; <br/> else <br/> successor = the_parent_of (ptree, parent); <br/>}</P> <p> return successor; <br/>}</P> <p> static node * the_parent_of (const t REE * const ptree, const node * const pnode) <br/> {<br/> node * parent = NULL, * scan = * ptree; </P> <p> while (scan! = NULL) <br/>{< br/> parent = scan; <br/> If (left_is_greater_than_right (pnode-> item, scan-> item )) <br/>{< br/> If (scan-> right! = NULL) <br/> If (left_is_assist_to_right (pnode-> item, scan-> right-> item) <br/> break; <br/> scan = scan-> right; <br/>}< br/> else if (left_is_lesser_than_right (pnode-> item, scan-> item )) <br/>{< br/> If (scan-> left! = NULL) <br/> If (left_is_assist_to_right (pnode-> item, scan-> left-> item) <br/> break; <br/> scan = scan-> left; <br/>}< br/> else <br/> {<br/> If (* ptree = scan) <br/> parent = NULL; <br/> break; <br/>}</P> <p> return parent; <br/>}</P> <p> static void threading (TREE * const ptree, node ** const previous) <br/>{< br/> If (* ptree! = NULL) <br/>{< br/> threading (& (* ptree)-> left, previous); <br/> If (null = (* ptree) -> left) <br/>{< br/> (* ptree)-> left_tag = thread; <br/> (* ptree)-> left = * Previous; <br/>}< br/> If (* Previous! = NULL) <br/> If (null = (* Previous)-> right) <br/> {<br/> (* Previous)-> right_tag = thread; <br/> (* Previous)-> right = * ptree; <br/>}< br/> * Previous = * ptree; <br/> threading (& (* ptree)-> right, previous); <br/>}< br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.