Back to index

im-sdk  12.3.91
Functions
tree.c File Reference
#include <stdio.h>
#include <assert.h>
#include "tree.h"

Go to the source code of this file.

Functions

TreeNode * _tree_search_hangul (TreeNode *node, unsigned char *hangul)
void tree_init (Tree *tree)
void tree_inorder_traverse (TreeNode *node, void *(*func)(void *))
int tree_compare_hangul (TreeNode *a, unsigned char *b)
TreeNode * tree_search_hangul (Tree *tree, unsigned char *hangul)
void tree_print (Tree *tree, void *(*print_function)(void *))
void tree_shallow_insert (Tree *tree, TreeNode *tnode)
void tree_build_from_array (Tree *tree, TreeNode *data, int first, int last)
TreeNode * tree_node_shallow_new (HHEntry data)

Function Documentation

static TreeNode * _tree_search_hangul ( TreeNode *  node,
unsigned char *  hangul 
)

Definition at line 51 of file tree.c.

{
  assert (hangul != NULL);
  if (hangul == NULL)
    return NULL;

  while (node){
    if (!strcmp (node->data->hangul, hangul))
      return node;
    else if (strcmp (node->data->hangul, hangul) > 0)
      node = node->right;
    else
      node = node->left;
  }
  return NULL;
}

Here is the caller graph for this function:

void tree_build_from_array ( Tree tree,
TreeNode *  data,
int  first,
int  last 
)

Definition at line 146 of file tree.c.

{
  int middle;
  if (first <= last){
    middle = (first + last) / 2;
    tree_shallow_insert (tree, data + middle);
    tree_build_from_array (tree, data, first, middle - 1);
    tree_build_from_array (tree, data, middle + 1, last);
  }
    
}

Here is the call graph for this function:

Here is the caller graph for this function:

int tree_compare_hangul ( TreeNode *  a,
unsigned char *  b 
)

Definition at line 37 of file tree.c.

{
  return  strcmp (a->data->hangul, b);
}
static void tree_init ( Tree tree)

Definition at line 9 of file tree.c.

{
  assert (tree != NULL);
  if (tree->root != NULL){
    /* This function is meant to be called at the very beginnin.
       I'll not take care of this case for now...no time.. */
    
    fprintf (stderr, "I can't initialize non-empty tree");
    exit (-1);
  }

  tree->root = NULL;
}
void tree_inorder_traverse ( TreeNode *  node,
void *(*)(void *)  func 
)

Definition at line 27 of file tree.c.

{
  if (node != NULL){
    tree_inorder_traverse (node->left, func);
    func ((void *)node->data);
    tree_inorder_traverse (node->right, func);
  }
}

Here is the caller graph for this function:

TreeNode* tree_node_shallow_new ( HHEntry  data)

Definition at line 164 of file tree.c.

{
  TreeNode *p_newnode =
    (TreeNode *) calloc (1, sizeof (TreeNode));
  p_newnode->data = data;
  return p_newnode;
}
void tree_print ( Tree tree,
void *(*)(void *)  print_function 
)

Definition at line 94 of file tree.c.

{
  TreeNode *root = tree->root;
  tree_inorder_traverse (root, print_function);
}

Here is the call graph for this function:

Here is the caller graph for this function:

TreeNode* tree_search_hangul ( Tree tree,
unsigned char *  hangul 
)

Definition at line 43 of file tree.c.

{
  TreeNode *ret_node = NULL;
  ret_node = _tree_search_hangul (tree->root, hangul);
  return ret_node;
}

Here is the caller graph for this function:

void tree_shallow_insert ( Tree tree,
TreeNode *  tnode 
)

Definition at line 106 of file tree.c.

{
  TreeNode *ptr = tree->root ;
  TreeNode *prev;
  
  int comp_return;

  assert (tree != NULL);
  assert (tnode != NULL);

  if (tree == NULL || tnode == NULL){
    fprintf (stderr, "wrong parameter passed\n");
    exit (-1);
  }
  if (!ptr){
    tree->root = tnode;
    return;
  }

  while (ptr){
    prev = ptr;
    comp_return = hhitem_comp (ptr->data, tnode->data);
    if (comp_return < 0)
      ptr = ptr->left;
    else if (comp_return >0)
      ptr = ptr->right;
    else {
      /* no duplication expected, just return */
      return;
    }
  }
  if (hhitem_comp (prev->data, tnode->data) < 0)
    prev->left = tnode;
  else
    prev->right = tnode;
  
}

Here is the call graph for this function:

Here is the caller graph for this function: