Binary Tree | collection 1 (Introduction)

Binary Tree | collection 1 (Introduction)

Tree : and arrays, linked lists, queues, and the different stacks, which are linear data structure, a tree is a hierarchical data structure.

The concept of the tree : the top node is called the root of the tree. Element located directly under the element referred to as its child elements. Something just above elements is called its parent element. For example, 'a' is 'f' progeny, 'f' is 'a' parent. Finally, there is no element of sub-elements is called a leaf (leaves).

    tree
    ----
      j    <-- root
    /   \
   f      k  
 /   \      \
a     h      z    <-- leaves 

Why trees?

  1. Use the tree One reason may be that you want to store natural hierarchy information form. For example, the file system on your computer:

     file system
     -----------
         /    <-- root
     /      \
     ...      home
          /          \
        ugrad        course
         /       /      |     \
     ...      cs101  cs112  cs113  
  2. Tree (have some sort, such as BST) to provide moderate access / search (faster than a linked list, slower than the array).
  3. Tree provides moderate insertion / deletion (faster than the array, unordered list than slower).
  4. As the list, with different arrays, the tree does not limit the number of nodes, because nodes are linked using the pointer.

The main application of the tree include :

  1. Operating hierarchical data.
  2. Make the information easy to search (see tree traversal).
  3. Operating list of data sorted.
  4. As for the resultant digital image to obtain a visual effect workflow.
  5. Router algorithm.
  6. In the form of multi-stage decision-making (see commercial chess).

Binary Tree : Tree elements up to two sub-elements is called binary. As the binary tree for each element can have only two sub-elements, so we usually name them Left and Right child elements.

C represents the binary tree : the tree represented by the topmost node of the tree points to a pointer. If the tree is empty, the root is empty.
Tree node contains the following components.

  1. data.
  2. Pointer to the left child objects.
  3. A pointer to the right child objects.

In C, we can use the structure to represent tree nodes. The following is an example of a tree node has an integer data.

struct node  
{ 
  int data; 
  struct node *left; 
  struct node *right; 
};

C language first simple tree .
Let's create a simple tree, there are four nodes in C. Create a tree shown below.

    tree
    ----
      1    <-- root
    /   \
   2     3  
  /   
 4

Code

#include <stdio.h>

typedef struct bitreeNode {
    int data;
    struct bitreeNode *left;
    struct bitreeNode *right; 
} bitreeNode;


// 给定一个 data,创建一个二叉树节点
bitreeNode *newBitreeNode(int data)
{
    bitreeNode *node = (bitreeNode*)malloc(sizeof(bitreeNode));
    if(node == NULL)
        return NULL;
    node->data = data;
    node->left = NULL;
    node->right = NULL;

    return node;
}

int main()
{
    // 创建根节点
    bitreeNode *root = newBitreeNode(1);
    /* 下面是上述语句后的树 
  
        1 
      /   \ 
     NULL  NULL   
    */

    root->left = newBitreeNode(2);
    root->right = newBitreeNode(3);
    /* 2和3分别成为1的左右儿子
           1 
         /   \ 
        2      3 
     /    \    /  \ 
    NULL NULL NULL NULL 
    */
    root->left->left = newBitreeNode(4);
     /* 4成为2的左儿子
           1 
       /       \ 
      2          3 
    /   \       /  \ 
   4    NULL  NULL  NULL 
  /  \ 
NULL NULL 
    */
}

Summary : tree is a hierarchical data structure. The main purpose of the tree including the maintenance of hierarchical data, provide appropriate access and insert / delete operations. Binary tree is a special case, where each node has at most two child nodes.

Here is a collection of this post 2 and 3 collections

Guess you like

Origin www.cnblogs.com/wjundong/p/11909861.html
Recommended