File: bintree.c

package info (click to toggle)
abiword 0.7.7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 20,604 kB
  • ctags: 18,358
  • sloc: cpp: 88,791; ansic: 66,296; sh: 7,777; makefile: 3,397; xml: 687; perl: 361; awk: 273; sed: 36; csh: 28
file content (180 lines) | stat: -rw-r--r-- 4,168 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Caolan.McNamara@ul.ie
http://www.csn.ul.ie/~caolan

Released under the GPL, see COPYING
*/

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

#include "bintree.h"

void InitBintree(BintreeInfo *tree,int (*func1)(void *,void *),int (*func2)(void *,void *))
	{
	tree->Root = NULL;               /* root of binary tree */
	tree->no_in_tree=0;
	tree->CompLT = func1;
	tree->CompEQ = func2;
	}

Node *InsertNode(BintreeInfo *tree,void *Data) {
    Node *X, *Current, *Parent;

   /***********************************************
    *  allocate node for Data and insert in tree  *
    ***********************************************/

    /* find X's parent */
    Current = tree->Root;
    Parent = 0;
    while (Current) {
        if (tree->CompEQ(Data, Current->Data)) return (NULL);
        Parent = Current;
        Current = tree->CompLT(Data, Current->Data) ?
            Current->Left : Current->Right;
    }

    /* setup new node */
    if ((X = (Node *)malloc (sizeof(Node))) == 0) {
        fprintf (stderr, "insufficient memory (InsertNode)\n");
        exit(1);
    }
	tree->no_in_tree++;
    X->Data = Data;
    X->Parent = Parent;
    X->Left = NULL;
    X->Right = NULL;

    /* insert X in tree */
    if(Parent)
		{
        if (tree->CompLT(Data, Parent->Data))
            Parent->Left = X;
        else
            Parent->Right = X;
		}
    else
        tree->Root = X;

    return(X);
}


void DeleteNode(BintreeInfo *tree,Node *Z) 
	{
    Node *X, *Y;

   /*****************************
    *  delete node Z from tree  *
    *****************************/

    /* Y will be removed from the parent chain */
    if (!Z || Z == NULL) return;


	tree->no_in_tree--;

    /* find tree successor */
    if (Z->Left == NULL || Z->Right == NULL)
        Y = Z;
    else {
        Y = Z->Right;
        while (Y->Left != NULL) Y = Y->Left;
    }

    /* X is Y's only child */
    if (Y->Left != NULL)
        X = Y->Left;
    else
        X = Y->Right;

    /* remove Y from the parent chain */
    if (X) 
		X->Parent = Y->Parent;
    if (Y->Parent)
		{
        if (Y == Y->Parent->Left)
            Y->Parent->Left = X;
        else
            Y->Parent->Right = X;
		}
    else
        tree->Root = X;

    /* Y is the node we're removing */
    /* Z is the data we're removing */
    /* if Z and Y are not the same, replace Z with Y. */
    if (Y != Z) {
        Y->Left = Z->Left;
        if (Y->Left) Y->Left->Parent = Y;
        Y->Right = Z->Right;
        if (Y->Right) Y->Right->Parent = Y;
        Y->Parent = Z->Parent;
        if (Z->Parent)
			{
            if (Z == Z->Parent->Left)
                Z->Parent->Left = Y;
            else
                Z->Parent->Right = Y;
			}
        else
            tree->Root = Y;
        free (Z);
    } else {
        free (Y);
    }
}

Node *FindNode(BintreeInfo *tree,void *Data) 
	{

   /*******************************
    *  find node containing Data  *
    *******************************/

    Node *Current = tree->Root;
    while(Current != NULL)
        if(tree->CompEQ(Data, Current->Data))
            return (Current);
        else
            Current = tree->CompLT (Data, Current->Data) ?
                Current->Left : Current->Right;
    return(0);
}


Node *NextNode(BintreeInfo *tree,Node *element)
	{
	Node *father_of_element;
	Node *father_of_forefather;

	if (!element)
		{
		father_of_element = tree->Root;
		if (father_of_element != NULL)
			while (father_of_element->Left != NULL)
				father_of_element = father_of_element->Left;
		}
	else
		{
		father_of_element = element;
		if (father_of_element->Right != NULL)
			{
			father_of_element = father_of_element->Right;
			while (father_of_element->Left != NULL)
				father_of_element = father_of_element->Left;
			}
		else
			{
            father_of_forefather = father_of_element->Parent;
            while (father_of_forefather && (father_of_forefather->Right == father_of_element))
                {
                father_of_element = father_of_forefather;
            	father_of_forefather = father_of_element->Parent;
        		}
            father_of_element = father_of_forefather;
			}
		}
	return father_of_element;
	}