summaryrefslogtreecommitdiffstats
path: root/amarok/src/mediadevice/daap/mongrel/http11/tst_delete.c
blob: cb18f16a4965d10df988275f2f6eda47a2815e31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

#include "tst.h"
#include <stdio.h>
#include <stdlib.h>

void *tst_delete(unsigned char *key, struct tst *tst)
{
   struct node *current_node;
   struct node *current_node_parent;
   struct node *last_branch;
   struct node *last_branch_parent;
   struct node *next_node;
   struct node *last_branch_replacement;
   struct node *last_branch_dangling_child;
   int key_index;

   
   if(key[0] == 0)
      return NULL;
   if(tst->head[(int)key[0]] == NULL)
      return NULL;
   
   last_branch = NULL;
   last_branch_parent = NULL;
   current_node = tst->head[(int)key[0]];
   current_node_parent = NULL;
   key_index = 1;
   while(current_node != NULL)
   {
      if(key[key_index] == current_node->value)
      {
         
         if( (current_node->left != NULL) || (current_node->right != NULL) )
         {
            last_branch = current_node;
            last_branch_parent = current_node_parent;
         }
         if(key[key_index] == 0)
            break;
         else
         {
            current_node_parent = current_node;
            current_node = current_node->middle;
            key_index++;
            continue;
         }
      }
      else if( ((current_node->value == 0) && (key[key_index] < 64)) ||
         ((current_node->value != 0) && (key[key_index] <
         current_node->value)) )
      {
         last_branch_parent = current_node;
         current_node_parent = current_node;
         current_node = current_node->left;
         last_branch = current_node;
         continue;
      }
      else
      {
         last_branch_parent = current_node;
         current_node_parent = current_node;
         current_node = current_node->right;
         last_branch = current_node;
         continue;
      }
   
   }
   if(current_node == NULL)
      return NULL;
   
   if(last_branch == NULL)
   {
      
         next_node = tst->head[(int)key[0]];
         tst->head[(int)key[0]] = NULL;
   }
   else if( (last_branch->left == NULL) && (last_branch->right == NULL) )
   {
      
      if(last_branch_parent->left == last_branch)
         last_branch_parent->left = NULL;
      else
         last_branch_parent->right = NULL;
      
      next_node = last_branch;
   }
   else
   {
      
      if( (last_branch->left != NULL) && (last_branch->right != NULL) )
      {
         last_branch_replacement = last_branch->right;
         last_branch_dangling_child = last_branch->left;
      }
      else if(last_branch->right != NULL)
      {
         last_branch_replacement = last_branch->right;
         last_branch_dangling_child = NULL;
      }
      else
      {
         last_branch_replacement = last_branch->left;
         last_branch_dangling_child = NULL;
      }
      
      if(last_branch_parent == NULL)
         tst->head[(int)key[0]]=last_branch_replacement;
      else
      {
         if (last_branch_parent->left == last_branch)
            last_branch_parent->left = last_branch_replacement;
         else if (last_branch_parent->right == last_branch)
            last_branch_parent->right = last_branch_replacement;
         else
            last_branch_parent->middle = last_branch_replacement;
      }
      
      if(last_branch_dangling_child != NULL)
      {
         current_node = last_branch_replacement;
      
         while (current_node->left != NULL)
            current_node = current_node->left;
      
         current_node->left = last_branch_dangling_child;
      }
      
      next_node = last_branch;
   }
   
   do
   {
      current_node = next_node;
      next_node = current_node->middle;
      
      current_node->left = NULL;
      current_node->right = NULL;
      current_node->middle = tst->free_list;
      tst->free_list = current_node;
   }
   while(current_node->value != 0);
   
   return next_node;
   
}