Data structure--tree

Source: Internet
Author: User

1, definition: Tree is a nonlinear structure, is a one-to-many data structure.

The structure of the analysis tree, we use recursive method, the root node below can be considered as a subtree.

2, the storage structure of the tree:

We usually store it in the child's brother law. That is, the child on the left of the left side of the node at the end of the node, and the right sibling of the knot is placed on the right side of the knot.

This creates a two-fork tree.

Binary trees and trees can correspond to each other.

3. Binary tree and its properties

In short, the binary tree has many special properties, the direct study of the tree will be a bit troublesome, so we study the tree by studying the binary tree.

Many of the features of a binary tree can help us solve many problems, such as finding problems and sorting problems.

4, Binary tree storage

Binary tree storage is generally used in the form of a linked list, that is, a two-linked list.

5. Traverse the binary tree

Pre-sequence traversal, middle sequence traversal, post-order traversal

6, the establishment of two-fork tree

As an example, a two-fork tree is established by the previous sequential traversal method. The core is a recursive approach.

#include <stdlib.h> #include "conio.h" #include <stdio.h>//define the nodes of the tree typedef struct treenode{   Char data;   struct TreeNode * lchild;   struct TreeNode * rchild;} Treenode,*bitree; Bitree Createtree (Bitree T) {  char ch;  scanf ("%c", &ch);  if (ch = = ' # ')  T = NULL;  else  {  T = (TreeNode *) malloc (sizeof (TreeNode));      T->data = ch;  T->lchild=createtree (t->lchild);  T->rchild=createtree (T->rchild);  }  return T;} void Pretree (Bitree t) {   if (t)   {      printf ("%c", t->data);      Pretree (t->lchild);  Pretree (T->rchild);   }} void Main () {  bitree T;  t = Createtree (t);  Pretree (T);}

7, tree, binary tree, forest mutual transformation

(1) Tree---"Two fork tree: left the first child on the left child, right brother on the right child

(2) Forest--"Two fork tree: convert each tree to two fork tree, then take the root node of the latter tree as the last tree right child

(3) Binary tree-"tree"


8, Heffman


Data structure--tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.