//SeqList.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
typedef void SeqList;
typedef void SeqListNode;
SeqList* SeqList_Create(int capacity);
void SeqList_Destroy(SeqList* list);
void SeqList_Clear(SeqList* list);
int SeqList_Length(SeqList* list);
int SeqList_Capacity(SeqList* list);
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
SeqListNode* SeqList_Get(SeqList* list, int pos);
SeqListNode* SeqList_Delete(SeqList* list, int pos);
#endif
//SeqList.c
#include <stdio.h>
#include <malloc.h>
#include "SeqList.h"
typedef unsigned int TSeqListNode;
typedef struct _tag_SeqList
{
int capacity;
int length;
TSeqListNode* node;
} TSeqList;
SeqList* SeqList_Create(int capacity) // O(1)
{
TSeqList* ret = NULL;
if( capacity >= 0 )
{
ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode) * capacity);
}
if( ret != NULL )
{
ret->capacity = capacity;
ret->length = 0;
ret->node = (TSeqListNode*)(ret + 1);
}
return ret;
}
void SeqList_Destroy(SeqList* list) // O(1)
{
free(list);
}
void SeqList_Clear(SeqList* list) // O(1)
{
TSeqList* sList = (TSeqList*)list;
if( sList != NULL )
{
sList->length = 0;
}
}
int SeqList_Length(SeqList* list) // O(1)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->length;
}
return ret;
}
int SeqList_Capacity(SeqList* list) // O(1)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->capacity;
}
return ret;
}
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) // O(n)
{
TSeqList* sList = (TSeqList*)list;
int ret = (sList != NULL);
int i = 0;
ret = ret && (sList->length + 1 <= sList->capacity);
ret = ret && (0 <= pos);
if( ret )
{
if( pos >= sList->length )
{
pos = sList->length;
}
for(i=sList->length; i>pos; i--)
{
sList->node[i] = sList->node[i-1];
}
sList->node[i] = (TSeqListNode)node;
sList->length++;
}
return ret;
}
SeqListNode* SeqList_Get(SeqList* list, int pos) // O(1)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = NULL;
if( (sList != NULL) && (0 <= pos) && (pos <= sList->length) )
{
ret = (SeqListNode*)(sList->node[pos]);
}
return ret;
}
SeqListNode* SeqList_Delete(SeqList* list, int pos) // O(n)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = SeqList_Get(list, pos);
int i = 0;
if( ret != NULL )
{
for(i=pos+1; i<sList->length; i++)
{
sList->node[i-1] = sList->node[i];
}
sList->length--;
}
return ret;
}
//BTree.h
#ifndef _BTREE_H_
#define _BTREE_H_
#define BT_LEFT 0
#define BT_RIGHT 1
typedef unsigned long long BTPos; //64bit
typedef void BTree;
typedef struct _tag_BTreeNode BTreeNode; //define the node pointer
struct _tag_BTreeNode
{
BTreeNode* left;
BTreeNode* right;
};
typedef void (BTree_Printf)(BTreeNode*);
BTree* BTree_Create();
void BTree_Destroy(BTree* tree);
void BTree_Clear(BTree* tree);
int BTree_Insert(BTree* tree,BTreeNode* node,BTPos pos,int count,int flag);
void BTree_Display(BTree* tree,BTree_Printf* pFunc,int gap,char div);
BTreeNode* BTree_Delete(BTree* tree,BTPos pos,int count);
BTreeNode* BTree_Get(BTree* tree,BTPos pos,int count);
BTreeNode* BTree_Root(BTree* tree);
int BTree_Height(BTree* tree);
int BTree_Count(BTree* tree);
int BTree_Degree(BTree* tree);
#endif
//BTree.c
#include "BTree.h"
#include <malloc.h>
#include <stdio.h>
typedef struct _tag_TBTree TBTree;
struct _tag_TBTree //define the node of header
{
int count;
BTreeNode* root;
};
static int recursive_degree(BTreeNode* root)
{
int ret = 0;
if(root!=NULL)
{
if(root->left!=NULL)
{
ret++;
}
if(root->right!=NULL)
{
ret++;
}
if(ret == 1)
{
int ld = recursive_degree(root->left);
int rd = recursive_degree(root->right);
if(ret < ld)
{
ret = ld;
}
if(ret < rd)
{
ret = rd;
}
}
}
return ret;
}
static int recursive_height(BTreeNode* root)
{
int ret = 0;
if(root!=NULL)
{
int lh = recursive_height(root->left);
int rh = recursive_height(root->right);
ret = ((lh>rh)?lh:rh) + 1;
}
return ret;
}
static int recursive_count(BTreeNode* node)
{
int ret = 0;
if(node!=NULL)
{
ret = recursive_count(node->left) + 1 + recursive_count(node->right);
}
return ret;
}
static void recursive_display(BTreeNode* node,BTree_Printf* pFunc,int format,int gap,char div)
{
int i = 0;
if((node!=NULL)&&(pFunc!=NULL))
{
for(i=0;i<format;i++)
{
printf("%c",div);
}
pFunc(node);
printf("\n");
if((node->left!=NULL)||(node->right!=NULL))
{
recursive_display(node->left,pFunc,format+gap,gap,div);
recursive_display(node->right,pFunc,format+gap,gap,div);
}
}
else //when there has only-left child node or only-right child node,print the char to fill the place
{
for(i=0;i<format;i++)
{
printf("%c",div);
}
printf("\n");
}
}
BTree* BTree_Create()
{
TBTree* ret = (TBTree*)malloc(sizeof(TBTree));
if(ret != NULL)
{
ret->count = 0;
ret->root = NULL;
}
return ret;
}
void BTree_Destroy(BTree* tree)
{
free(tree);
}
void BTree_Clear(BTree* tree)
{
TBTree* btree = (TBTree*)tree;
if(btree != NULL)
{
btree->count = 0;
btree->root = NULL;
}
}
int BTree_Insert(BTree* tree,BTreeNode* node,BTPos pos,int count,int flag) // pos :position; count :moving times;
{
int ret = 0;
int bit = 0;
TBTree* btree = (TBTree*)tree;
ret = (btree!=NULL)&&(node!=NULL)&&((flag==BT_LEFT)||(flag==BT_RIGHT));
if(ret)
{
BTreeNode* parent = NULL;
BTreeNode* current = btree->root;
node->left = NULL;
node->right = NULL;
while((count>0)&&(current!=NULL))
{
bit = pos & 1;
pos = pos >> 1;
parent = current;
if(bit==BT_LEFT)
{
current = current->left;
}
else if(bit==BT_RIGHT)
{
current = current->right;
}
count--;
}
if(flag==BT_LEFT) //when the current node has some children node,the current node should be inserted after the node
{
node->left = current;
}
else if(flag == BT_RIGHT)
{
node->right = current;
}
if(parent!=NULL)
{
if(bit == BT_LEFT)
{
parent->left = node;
}
else if(bit == BT_RIGHT)
{
parent->right = node;
}
}
else //insert the first node
{
btree->root = node;
}
btree->count++;
}
return ret;
}
void BTree_Display(BTree* tree,BTree_Printf* pFunc,int gap,char div)
{
TBTree* btree = (TBTree*)tree;
if((btree!=NULL)&&(pFunc!=NULL))
{
recursive_display(btree->root,pFunc,0,gap,div);
}
}
BTreeNode* BTree_Delete(BTree* tree,BTPos pos,int count)
{
BTreeNode* ret = NULL;
TBTree* btree = (TBTree*)tree; //做一个壳
int bit = 0;
if(btree!=NULL)
{
BTreeNode* parent = NULL;
BTreeNode* current = btree->root;
while((count>0)&&(current!=NULL))
{
bit = pos & 1;
pos = pos >> 1;
parent = current;
if(bit==BT_LEFT)
{
current = current->left;
}
else if(bit==BT_RIGHT)
{
current = current->right;
}
count--;
}
if(parent!=NULL)
{
if(bit==BT_LEFT)
{
parent->left = NULL;
}
else if(bit == BT_RIGHT)
{
parent->right = NULL;
}
}
else //the only one node
{
btree->root = NULL;
}
ret = current; //important
btree->count = btree->count - recursive_count(ret);
}
return ret;
}
BTreeNode* BTree_Get(BTree* tree,BTPos pos,int count)
{
BTreeNode* ret = NULL;
TBTree* btree = (TBTree*)tree;
int bit = 0;
if(btree!=NULL)
{
BTreeNode* parent = NULL;
BTreeNode* current = btree->root;
while((count>0)&&(current!=NULL))
{
bit = pos & 1;
pos = pos >> 1;
parent = current; //record the current node
if(bit == BT_LEFT)
{
current = current->left;
}
else if(bit == BT_RIGHT)
{
current = current->right;
}
count--;
}
ret = current; //important
}
return ret;
}
BTreeNode* BTree_Root(BTree* tree)
{
BTreeNode* ret = NULL;
TBTree* btree = (TBTree*)tree;
if(btree != NULL)
{
ret = btree->root;
}
return ret;
}
int BTree_Height(BTree* tree)
{
int ret = 0;
TBTree* btree = (TBTree*)tree;
if(btree!=NULL)
{
ret = recursive_height(btree->root);
}
return ret;
}
int BTree_Count(BTree* tree)
{
int ret = 0;
TBTree* btree = (TBTree*)tree;
if(btree!=NULL)
{
ret = btree->count;
}
return ret;
}
int BTree_Degree(BTree* tree)
{
int ret = 0;
TBTree* btree = (TBTree*)tree;
if(btree!=NULL)
{
ret = recursive_degree(btree->root);
}
return ret;
}
//main.c
/*-----------
time: 2016-4-8
version: DEV-C++ 5.2.0.3
-----------*/
#include <stdio.h>
#include <stdlib.h>
#include "BTree.h"
#include "SeqList.h"
struct Node
{
BTreeNode header;
char v;
};
void print_data(BTreeNode* node)
{
if(node!=NULL)
{
printf("%c",((struct Node*)node)->v);
}
}
void pre_order_traversal(BTreeNode* root) //前序遍历
{
if(root!=NULL)
{
printf("%c,",((struct Node*)root)->v);
pre_order_traversal(root->left);
pre_order_traversal(root->right);
}
}
void thread_via_left(BTreeNode* root,BTreeNode** pt) // 线索化,通过节点空指针实现
{
if((root!=NULL)&&(pt!=NULL))
{
if(*pt != NULL)
{
(*pt)->left = root;
*pt = NULL;
}
if(root->left == NULL)
{
*pt = root;
}
thread_via_left(root->left,pt);
thread_via_left(root->right,pt);
}
}
void thread_via_list(BTreeNode* root,SeqList* list) //线索化,通过单链表实现
{
if((root!=NULL)&&(list!=NULL))
{
SeqList_Insert(list,(SeqListNode*)root,SeqList_Length(list));
thread_via_list(root->left,list);
thread_via_list(root->right,list);
}
}
int main(int argc, char *argv[])
{
BTree* tree = BTree_Create();
SeqList* list = NULL;
BTreeNode* current = NULL;
int i = 0;
struct Node n1 = {{NULL,NULL},'A'};
struct Node n2 = {{NULL,NULL},'b'};
struct Node n3 = {{NULL,NULL},'C'};
struct Node n4 = {{NULL,NULL},'D'};
struct Node n5 = {{NULL,NULL},'E'};
struct Node n6 = {{NULL,NULL},'F'};
struct Node n7 = {{NULL,NULL},'G'};
BTree_Insert(tree,(BTreeNode*)&n1,0,0,0); //A
BTree_Insert(tree,(BTreeNode*)&n2,0x00,1,0); //b
BTree_Insert(tree,(BTreeNode*)&n3,0x01,1,0); //C
BTree_Insert(tree,(BTreeNode*)&n4,0x00,2,0); //D
BTree_Insert(tree,(BTreeNode*)&n5,0x02,2,0); //E
BTree_Insert(tree,(BTreeNode*)&n6,0x01,2,0); //F
BTree_Insert(tree,(BTreeNode*)&n7,0x03,2,0); //F
BTree_Display(tree,print_data,2,'-');
printf("pre_order_traversal (前序遍历): \n");
pre_order_traversal(BTree_Root(tree));
printf("\n");
printf("Thread Via List: \n");
current = BTree_Root(tree);
list = SeqList_Create(BTree_Count(tree)); //申请单链表
thread_via_list(current,list);
if(current!=NULL)
{
printf("thread_via_left: \n");
for(i=0;i<SeqList_Length(list);i++)
{
printf("%c,",((struct Node*)SeqList_Get(list,i))->v);
}
}
BTree_Destroy(tree);
return 0;
}