Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C language binary tree counts words     - Four safety delete files under Linux tools (Linux)

- LVM management reduces swap partition space to the root partition (Linux)

- The Objects in JavaScript (Programming)

- CentOS6.x and Windows XP and Windows Server 2003 Open IPv6 related matters (Linux)

- Autojump: an advanced cd command in the Linux file system fast navigation (Linux)

- Linux Live CD lets your PC is no longer secure (Linux)

- Vim useful plugin: vundle (Linux)

- apt-get install openstack pkg Troubleshooting (Linux)

- MongoDB 3.2 Cluster Setup (Database)

- Linux find command usage summary (Linux)

- C # how to generate a folder or file automatically rename (Programming)

- Android Service service applications and the phone SMS Listener Listener (Programming)

- Oracle11g CRS-0184 Problem Solving (Database)

- Disk Management LVM (Linux)

- Linux system security infrastructure Highlights (Linux)

- Ubuntu 14.04 users how to install VLC 2.2.0 (Linux)

- Installed in the desktop version of Ubuntu Unity Tweak Tool (Linux)

- Oracle Database asynchronous IO cause slow query response (Database)

- Zabbix system email alert Python script (Server)

- redis configuration in detail (English) (Database)

 
         
  C language binary tree counts words
     
  Add Date : 2018-11-21      
         
         
         
  Tencent has just participated in the 2015 online mock exams; the first question is the word count program design four big questions; to remember this day, today I'm going to look through the code; I will use the core data structure is a binary tree ;

Problem

I need to count the words are hard-coded in the program directly; doing so because omit the file input and output brought about confusion; my each article, said only a general topic; I would also facilitate the future review;

Solution

First, we need to define a structure, as shown in the code:

const int LONGEST_WORD = 32; // The longest word size

struct binary_tree {
    char str [LONGEST_WORD];
    int count;
    struct binary_tree * left;
    struct binary_tree * right;
};

typedef struct binary_tree node;

Note that we assume that the longest word is defined as a constant, where I think the length of this 32 should be all right; if you want to count the article is a chemical paper, I suggest you increase the number, since the formula is usually very long; and is that our structures; this should be easy to understand; because the C language does not provide the type BOOL I want, write it yourself thus the following code; this definition is useful, it is usually more popular than define;

enum BOOL {
    NO,
    YES
};

typedef enum BOOL BOOL;

Next, we need to know how to compare the size of the between words;

Therefore, a function called cmp;

Code as follows:

BOOL cmp (char * s, char * t)
{
    int i;
    for (i = 0; s [i] == t [i]; i ++)
        if (s [i] == '\ 0')
            return NO;
    ? Return (s [i] - t [i]) <0 NO: YES;
}

While traversing two strings and returns the value of a process;

This will only return two cases NO / YES, otherwise returns three values (-1, 0, positive number);

In that case, it is not conducive to our future work;

Then, that is, if we return YES (what) (what to do);

If it returns NO should we (how) (what to do);

Therefore, we need to insert a function, the left and right respectively inserted into two different sub-tree data;

void insert (node ** tree, char * val) {
    node * temp = NULL;
    if (! (* tree)) {
        temp = (node *) malloc (sizeof (node));
        temp-> left = temp-> right = NULL;
        temp-> str = val; // issue code ...
        temp-> count = 1;
        * Tree = temp;
        return;
    }

    if (cmp (val, (* tree) -> str)) {
        insert (& (* tree) -> left, val);
    } Else if (cmp (val, (* tree) -> str)) {
        insert (& (* tree) -> right, val);
    } Else {
        (* Tree) -> count ++;
    }
}

This code and the aforementioned (C language binary tree) inside the code is almost the same, where there is detailed; here mainly explain the issue code annotated with the line, if this line is not modified, the program will jump collapse; However, I will not intentionally modify it right away and continue to write down; we need a function, the destruction of nodes:

void deltree (node * tree) {
    if (tree) {
        deltree (tree-> left);
        deltree (tree-> right);
        free (tree);
    }
}

In order to view our results, we need a way to navigate;

Here we chose it in order!

void print_inorder (node * tree) {
    if (tree) {
        print_inorder (tree-> left);
        printf ( "[% s \ t \ t \ t] count: [% d] \ n", tree-> str, tree-> count);
        print_inorder (tree-> right);
    }
}

We head file stdio.h / stdlib.h after introduction;

The main int main (int argc, char ** arg {

    node * root;
    node * tmp;
    // Int i;

    root = NULL;
    / * Inserting nodes into tree * /
    insert (& root, "hello");
    insert (& root, "hey");
    insert (& root, "hello");
    insert (& root, "ok");
    insert (& root, "hey");
    insert (& root, "hey");
    insert (& root, "hey");

    printf ( "In Order Display \ n");
    print_inorder (root); / * Deleting all nodes of tree * /

    deltree (root);
}

Sure enough, our issue code in question because the string can not be like the other, such as an int directly with the '=' assignment;

So we need a function cpy:

void mystrcpy (char * s, char * t)
{
    while ((* s ++ = * t ++)! = '\ 0')
        ;
}

All code is as follows:

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


const int LONGEST_WORD = 32; // The longest word size

struct binary_tree {
    char str [LONGEST_WORD];
    int count;
    struct binary_tree * left;
    struct binary_tree * right;
};

typedef struct binary_tree node;

enum BOOL {
    NO,
    YES
};

typedef enum BOOL BOOL;

BOOL cmp (char * s, char * t)
{
    int i;
    for (i = 0; s [i] == t [i]; i ++)
        if (s [i] == '\ 0')
            return NO;
    ? Return (s [i] - t [i]) <0 NO: YES;
}
void mystrcpy (char * s, char * t)
{
    while ((* s ++ = * t ++)! = '\ 0')
        ;
}

void insert (node ** tree, char * val) {
    node * temp = NULL;
    if (! (* tree)) {
        temp = (node *) malloc (sizeof (node));
        temp-> left = temp-> right = NULL;
        // Temp-> str = val; // issue code ...
        mystrcpy (temp-> str, val);
        temp-> count = 1;
        * Tree = temp;
        return;
    }

    if (cmp (val, (* tree) -> str)) {
        insert (& (* tree) -> left, val);
    } Else if (cmp (val, (* tree) -> str)) {
        insert (& (* tree) -> right, val);
    } Else {
        (* Tree) -> count ++;
    }
}

void deltree (node * tree) {
    if (tree) {
        deltree (tree-> left);
        deltree (tree-> right);
        free (tree);
    }
}

void print_inorder (node * tree) {
    if (tree) {
        print_inorder (tree-> left);
        printf ( "[% s \ t \ t \ t] count: [% d] \ n", tree-> str, tree-> count);
        print_inorder (tree-> right);
    }
}


int main (int argc, char ** argv)
{
    node * root;
    node * tmp;
    // Int i;

    root = NULL;
    / * Inserting nodes into tree * /
    insert (& root, "hello");
    insert (& root, "hey");
    insert (& root, "hello");
    insert (& root, "ok");
    insert (& root, "hey");
    insert (& root, "hey");
    insert (& root, "hey");


    printf ( "In Order Display \ n");
    print_inorder (root);


    / * Deleting all nodes of tree * /

    deltree (root);
}

Discussion

Then this procedure has been completed it!

There are many that can be optimized, it can also add more features;

For example, to find the number of times a specific character appears;

Or a specific number of lines of characters appearing, etc. can be;
     
         
         
         
  More:      
 
- Installation and configuration to compile MySQL 5.6.10 under CentOS 5.9 (Database)
- To build Spring RestTemplate use HttpClient4 (Programming)
- secureCRT remote login Linux must first open the connection protocol (Linux)
- MongoDB slice simple example (Database)
- To install file manager Nautilus 3.12.2 under ubuntu (Linux)
- Oracle local user login authentication fails ORA-01031 insufficient privileges (Database)
- Spring AOP (Programming)
- A summary of Java multi-threaded programming - acquaintance multithreading (Programming)
- Java concurrent programming using the synchronized keyword ReentrantLock alternative primitive (Programming)
- PXE + Kickstart automatically install CentOS 6.5 (Linux)
- Ubuntu iptables prevent IP attacks (Linux)
- Stunning exclamation point at the Linux command line (Linux)
- To install Redis under Linux (Database)
- Delegate in C # (Programming)
- To remove those IP is prohibited Fail2ban on CentOS 6/7 (Server)
- Let CentOS6 yum upgrade to support more source rpm package (Linux)
- KVM usb passthrough configuration (Linux)
- The basic principles for the protection of a good linux server security (Linux)
- Docker - for the development and deployment of unified lightweight Linux containers (Linux)
- How to Install Telegram instant messaging software on Ubuntu (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.