The fourth day of the learning tree, the first class stretched the tree. I wrote it for 8 hours. It was so sleepy. It felt so good. After writing it, I read it again and wrote it down.
I hope someone else will be willing to communicate with me.
/* Splay_tree.c -- Extension tree implementation file */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include "splay_tree.h" </P> <p>/* local data type definition */<br/> typedef position stack_item; <br/> typedef struct stack_node <br/> {<br/> stack_item item; <br/> struct stack_node * Next; <br/>} stack_node; <br/> typedef stack_node * stack; </P> <p>/* local function declaration */<br/> position make_node (const item ); <br/> int left_is_greater_than_ri Ght (const item left, const item right); <br/> int left_is_lesser_than_right (const item left, const item right); <br/> void copy_item_to_node (const item * const pitem, position * const pnew_node); <br/> position discover_item (const splaytree, const item); <br/> position the_parent_of (const splaytree, const position target ); <br/> position single_rotate_with_left (position K2); <Br/> position single_rotate_with_right (position K2); <br/> position zig_zig_rotate_with_left (position K3); <br/> position zig_zig_rotate_with_right (position K3 ); <br/> position zig_zag_rotate_with_left (position K3); <br/> position zig_zag_rotate_with_right (position K3); <br/> void initialize_stack (stack * pstack ); <br/> int stack_is_empty (const stack * const pstack); <br/> int stack_is_full (Void); <br/> int push (stack * pstack, const stack_item item); <br/> stack_item top (const stack * const pstack, const int number ); <br/> int stack_item_count (const stack * const pstack); <br/> int POP (stack * pstack, const int number ); <br/> void empty_the_stack (stack * pstack); <br/> stack discover_item_by_use_stack (const splaytree, const item ); </P> <p>/* interface function definition */</P> <p> void iinti Alizesplaytree (splaytree * ptree) <br/>{< br/> (* ptree) = NULL; <br/>}</P> <p> int splaytreeisempty (const splaytree) <br/>{< br/> return null = tree; <br/>}</P> <p> int splaytreeisfull (void) <br/>{< br/> position temp; </P> <p> temp = (node *) malloc (sizeof (node); <br/> If (null = temp) <br/> return 1; <br/> else <br/> {<br/> free (temp); <br/> return 0; <br/>}</P> <p> splaytree additem (Splaytree tree, const item) <br/>{< br/> position new_node, scan = tree; </P> <p> new_node = make_node (item ); <br/> If (null = new_node) <br/> return tree; <br/> If (splaytreeisempty (tree) <br/> return tree = new_node; <br/> while (scan! = NULL) <br/>{< br/> If (left_is_greater_than_right (item, scan-> item )) <br/>{< br/> If (null = scan-> right) <br/> break; <br/> else <br/> scan = scan-> right; <br/>}< br/> else if (left_is_lesser_than_right (item, scan-> item )) <br/>{< br/> If (null = scan-> left) <br/> break; <br/> else <br/> scan = scan-> left; <br/>}< br/>/* repeated addition may occur */<br/> else <br/> return tree; <br/>}< br/> If (left_is _ Greater_than_right (item, scan-> item) <br/> scan-> right = new_node; <br/> else if (left_is_lesser_than_right (item, scan-> item )) <br/> scan-> left = new_node; </P> <p> return tree; <br/>}</P> <p> splaytree insert (splaytree tree, const item) <br/>{< br/> position new_node; </P> <p> If (null = tree) <br/>{< br/> new_node = make_node (item); <br/> return new_node; <br/>}< br/> If (left_is_great Er_than_right (item, tree-> item) <br/> tree-> right = insert (tree-> right, item); <br/> else if (left_is_lesser_than_right (item, tree-> item) <br/> tree-> left = insert (tree-> left, item ); <br/>/* repeated addition */<br/> else <br/> return tree; </P> <p> return tree; <br/>}</P> <p> position findmax (const splaytree tree) <br/>{< br/> position scan = tree; </P> <p> If (null = scan) <br/> return NULL; <Br/> while (scan-> right! = NULL) <br/> scan = scan-> right; </P> <p> return scan; <br/>}</P> <p> position findmin (const splaytree tree) <br/>{< br/> position scan = tree; </P> <p> If (null = scan) <br/> return NULL; <br/> while (scan-> left! = NULL) <br/> scan = scan-> left; </P> <p> return scan; <br/>}</P> <p> splaytree find (splaytree tree, const item) <br/>{< br/> position target, parent, grandfather, greatgrandfather; </P> <p> Target = discover_item (tree, item); <br/>/* if not found */<br/> If (null = target) <br/> return tree; <br/>/* cycle condition: the current node is not the root node */<br/> while (parent = the_parent_of (tree, target ))! = NULL) <br/>{< br/>/* If the parent node is the root node */<br/> If (tree = parent) <br/>{< br/> If (left_is_greater_than_right (item, parent-> item) <br/> Target = single_rotate_with_right (parent ); <br/> else <br/> Target = single_rotate_with_left (parent ); <br/>}< br/> else <br/> {<br/> grandfather = the_parent_of (tree, parent ); <br/>/* if it is on the left of the parent node */<br/> If (left_is_lesser_than_right (Target-> item, parent-> item) <br/> {<Br/> If (left_is_greater_than_right (parent-> item, grandfather-> item) <br/> Target = zig_zag_rotate_with_right (grandfather ); /* rotate in the right shape */<br/> else <br/> Target = zig_zig_rotate_with_left (grandfather ); /* rotate in a single font on the left */<br/>}< br/>/* if it is on the right of the parent node */<br/> else <br/> {<br/> If (left_is_greater_than_right (parent-> item, grandfather-> item) <br/> Target = zig_zig_rotate_with_right (grandfather);/* rotate in a right shape */ <Br/> else <br/> Target = zig_zag_rotate_with_left (grandfather ); /* rotate in the left shape */<br/>}< br/>/* If the root node is above the master node */<br/> If (grandfather! = Tree) <br/>{< br/> greatgrandfather = the_parent_of (tree, grandfather); <br/> If (left_is_greater_than_right (grandfather-> item, greatgrandfather-> item )) <br/> greatgrandfather-> right = target; <br/> else <br/> greatgrandfather-> left = target; <br/>/* update the cycle condition. that is, the target node is located in the tree */<br/> Target = discover_item (tree, item ); <br/>}</P> <p> return target; <br/>}</P> <p> splaytree find_by_use _ Stack (splaytree, const item) <br/>{< br/> stack; <br/> position target, parent, grandfather, greatgrandfather; </P> <p> Target = discover_item (tree, item); <br/> If (null = target) <br/> return tree; <br/> stack = discover_item_by_use_stack (tree, item); <br/> while (! Stack_is_empty (& stack) <br/>{< br/> parent = Top (& stack, 1 ); <br/>/* If the parent node is the root node (that is, there is only one node in the stack) */<br/> if (1 = stack_item_count (& stack )) <br/>{< br/> If (left_is_greater_than_right (item, parent-> item) <br/> Target = single_rotate_with_right (parent ); <br/> else <br/> Target = single_rotate_with_left (parent); <br/> POP (& stack, 1 ); <br/>}< br/> else <br/>{< br/> grandfather = Top (& stack, 2); <br/>/ * If it is on the left of the parent node */<br/> If (left_is_lesser_than_right (Target-> item, parent-> item )) <br/>{< br/> If (left_is_greater_than_right (parent-> item, grandfather-> item) <br/> Target = zig_zag_rotate_with_right (grandfather ); /* rotate in the right shape */<br/> else <br/> Target = zig_zig_rotate_with_left (grandfather ); /* rotate in a single font on the left */<br/>}< br/>/* if it is on the right of the parent node */<br/> else <br/> {<br/> If (left_is_greater_than_right (parent-> item, g Randfather-> item) <br/> Target = zig_zig_rotate_with_right (grandfather);/* rotate in a right shape */<br/> else <br/> Target = zig_zag_rotate_with_left (grandfather ); /* rotate in the left font shape */<br/>}< br/> If (stack_item_count (& stack)> 2) <br/>{< br/> greatgrandfather = Top (& stack, 3); <br/> If (left_is_greater_than_right (grandfather-> item, greatgrandfather-> item )) <br/> greatgrandfather-> right = target; <br/> else <br/> Greatgrandfather-> left = target; <br/>}< br/> POP (& stack, 2 ); <br/>}</P> <p> return target; <br/>}</P> <p> splaytree Delete (splaytree, const item) <br/>{< br/> splaytree left_subtree, right_subtree; <br/> position new_root, temp; </P> <p>/* if not found */<br/> If (null = discover_item (tree, item) <br/> return tree; <br/> tree = find_by_use_stack (tree, item); <br/> left_subtree = tree-> Left; <br/> right_subtree = tree-> right; <br/> temp = tree; <br/> If (null = left_subtree & null = right_subtree) <br/> tree = NULL; <br/>/* If the left subtree is empty, right subtree is not empty */<br/> else if (null = tree-> left) <br/> {<br/> new_root = findmin (right_subtree ); <br/> tree = find_by_use_stack (right_subtree, new_root-> item); <br/>}< br/>/* otherwise, the left subtree is not empty, the system is currently processing the ideal situation. whether the right subtree is empty does not matter */<br/> else <br/> {<br/> new_root = fin Dmax (left_subtree); <br/> left_subtree = find_by_use_stack (left_subtree, new_root-> item ); <br/>/* If the node of the maximum data in the left subtree is rotated to the root node, it must not have the right subtree */<br/> left_subtree-> right = right_subtree; <br/> tree = left_subtree; <br/>}< br/> free (temp); </P> <p> return tree; <br/>}</P> <p> void inordertraversal (const splaytree tree, void (* pfun) (const item )) <br/>{< br/> If (tree! = NULL) <br/>{< br/> inordertraversal (tree-> left, pfun); <br/> (* pfun) (tree-> item ); <br/> inordertraversal (tree-> right, pfun ); <br/>}</P> <p>/* local function definition */</P> <p> position make_node (const item) <br/>{< br/> position new_node; </P> <p> new_node = (node *) malloc (sizeof (node )); <br/> If (null = new_node) <br/> return NULL; <br/> else <br/>{< br/> copy_item_to_node (& item, & new_node); <B R/> new_node-> left = NULL; <br/> new_node-> right = NULL; <br/> return new_node; <br/>}< br/> int left_is_greater_than_right (const item left, const item right) <br/>{< br/> return left> right; <br/>}</P> <p> int left_is_lesser_than_right (const item left, const item right) <br/>{< br/> return left <right; <br/>}</P> <p> void copy_item_to_node (const item * const pitem, position * const PN Ew_node) <br/>{< br/> (* pnew_node)-> item = * pitem; <br/>}</P> <p> position discover_item (const splaytree, const item) <br/>{< br/> position scan = tree; </P> <p> while (scan! = NULL) <br/>{< br/> If (left_is_greater_than_right (item, scan-> item) <br/> scan = scan-> right; <br/> else if (left_is_lesser_than_right (item, scan-> item) <br/> scan = scan-> left; <br/>/* found, returns the pointer */<br/> else <br/> return scan; <br/>}</P> <p> return NULL; <br/>}</P> <p> position the_parent_of (const splaytree, const position target) <br/>{< br/> position parent = tree, scan = tree; </P> <p> If (tree = target) <br/> return NULL; <br/> while (scan! = NULL) <br/>{< br/> If (left_is_greater_than_right (Target-> item, scan-> item) <br/>{< br/> parent = scan; <br/> scan = scan-> right; <br/>}< br/> else if (left_is_lesser_than_right (Target-> item, scan-> item )) <br/>{< br/> parent = scan; <br/> scan = scan-> left; <br/>}< br/> else <br/> return parent; <br/>}</P> <p> return NULL; <br/>}</P> <p> position single_rotate_with_left (position K2) <br />{< Br/> position k1 = k2-> left; </P> <p> k2-> left = K1-> right; <br/> K1-> right = k2; </P> <p> return K1; <br/>}< br/> position single_rotate_with_right (position K2) <br/>{< br/> position k1 = k2-> right; </P> <p> k2-> right = K1-> left; <br/> K1-> left = k2; </P> <p> return K1; <br/>}</P> <p>/* One-font rotation is performed from K3-K2-K1 */<br/> position zig_zig_rotate_with_left (position K3) <br/>{< br/> position k2 = K3-> left; <br/> position k1 = k2-> left; </P> <p> K3-> left = k2-> right; <br/> k2-> right = K3; <br/> k2-> left = K1-> right; <br/> K1-> right = k2; </P> <p> return K1; <br/>}< br/> position zig_zig_rotate_with_right (position K3) <br/>{< br/> position k2 = K3-> right; <br/> position k1 = k2-> right; </P> <p> K3-> right = k2-> left; <br/> k2-> left = K3; <br/> k2-> right = K1-> left; <B R/> K1-> left = k2; </P> <p> return K1; <br/>}</P> <p>/* The font is rotated from K1-K2-K3 */<br/> position zig_zag_rotate_with_left (position K3) <br/>{< br/> position k2 = K3-> left; <br/> position k1 = k2-> right; </P> <p> k2-> right = K1-> left; <br/> K1-> left = k2; <br/> K3-> left = K1-> right; <br/> K1-> right = K3; </P> <p> return K1; <br/>}< br/> position zig_zag_rotate_with_right (position K3) <br/> {< Br/> position k2 = K3-> right; <br/> position k1 = k2-> left; </P> <p> k2-> left = K1-> right; <br/> K1-> right = k2; <br/> K3-> right = K1-> left; <br/> K1-> left = K3; </P> <p> return K1; <br/>}</P> <p>/* function definitions of some stacks */<br/> void initialize_stack (stack * pstack) <br/>{< br/> * pstack = NULL; <br/>}</P> <p> int stack_is_empty (const stack * const pstack) <br/>{< br/> return null = * pstack; <br/>}</P> <P> int stack_is_full (void) <br/>{< br/> stack_node * temp; </P> <p> temp = (stack_node *) malloc (sizeof (stack_node); <br/> If (null = temp) <br/> return 1; <br/> else <br/> {<br/> free (temp); <br/> return 0; <br/>}</P> <p> int push (stack * pstack, const stack_item item) <br/> {<br/> stack_node * new_node; </P> <p> new_node = (stack_node *) malloc (sizeof (stack_node )); <br/> If (null = new _ Node) <br/> return 0; <br/> new_node-> item = item; <br/> If (stack_is_empty (pstack )) <br/>{< br/> new_node-> next = NULL; <br/> * pstack = new_node; <br/>}< br/> else <br/>{< br/> new_node-> next = * pstack; <br/> * pstack = new_node; <br/>}</P> <p> return 1; <br/>}</P> <p> stack_item top (const stack * const pstack, const int number) <br/> {<br/> stack_node * scan = * pstack; <br/> int COUNT = 1; </P> <P> while (count <number) <br/>{< br/> If (null = scan) <br/> return NULL; <br/> scan = scan-> next; <br/>/* Don't forgrt this line !!! */<Br/> count ++; <br/>}</P> <p> return scan-> item; <br/>}</P> <p> int stack_item_count (const stack * const pstack) <br/>{< br/> stack_node * temp = * pstack; <br/> int COUNT = 0; </P> <p> while (temp! = NULL) <br/>{< br/> temp = temp-> next; <br/>/* Don't forget this line !!! */<Br/> count ++; <br/>}</P> <p> return count; <br/>}</P> <p> int POP (stack * pstack, const int number) <br/>{< br/> stack_node * temp; <br/> int COUNT = 0; </P> <p> while (count <number) <br/> {<br/> If (null = * pstack) <br/> return 0; <br/> temp = * pstack; <br/> * pstack = (* pstack)-> next; <br/> free (temp ); <br/>/* Don't forget this line !!! */<Br/> count ++; <br/>}</P> <p> return 1; <br/>}</P> <p> void empty_the_stack (stack * pstack) <br/>{< br/> stack_node * temp; </P> <p> while (* pstack! = NULL) <br/>{< br/> temp = * pstack; <br/> * pstack = (* pstack)-> next; <br/> free (temp); <br/>}</P> <p> stack discover_item_by_use_stack (const splaytree tree, const item) <br/>{< br/> stack; <br/> position scan = tree; </P> <p> initialize_stack (& stack ); <br/> while (scan! = NULL) <br/>{< br/> push (& stack, scan); <br/> If (left_is_greater_than_right (item, scan-> item )) <br/> scan = scan-> right; <br/> else if (left_is_lesser_than_right (item, scan-> item) <br/> scan = scan-> left; <br/> else <br/> {<br/>/* Pop Up the node found by the target */<br/> POP (& stack, 1 ); <br/> return stack; <br/>}< br/> empty_the_stack (& stack); </P> <p> return NULL; <br/>}