Data structure Tree

Source: Internet
Author: User

Tree

A tree is a nonlinear data structure, which is a finite set of n (n>=0) nodes.

(1) In any tree, there is only one specific root node. Point A

(2) The number of sub-trees owned by a node is called the degree of junction . the degree of a in the figure is 3,c 1

(3) A node with a degree of 0 is called a leaf or terminal node . KLM Point

(4) A node with a degree not 0 is called a non-terminal node or a branch node . A B C ...

(5) the degree of the number is the maximum value of each node in the tree, the degree of the tree is 3

(6) The root of the subtree of the node is called the child of the knot, the left side is the left child , and the right child is the right one.

Accordingly, the knot is called the child's parents .

(7) The maximum level of the nodes in the tree is called the depth or height of the tree. The depth of the tree shown in the figure is 4

(8) The subtree of the nodes in the tree is viewed from left to right (i.e., the left child cannot be exchanged with the right child).

The tree is called an ordered tree , otherwise it is an unordered tree.

(9) The forest is a collection of m>=0 trees that are not intersected by M.

Two-fork Tree

The binary tree is a special kind of tree structure, and its nodes have at most only two subtrees, that is, there is no more than 2 nodes in the binary tree,

Moreover, the sub-tree of the binary tree has the left and right points, its order cannot be arbitrarily reversed.

(1) A two-fork tree with a depth of K and a 2k-1 node is called a full two-fork tree,a

(2) with a depth of K, a two-fork tree with n nodes, when and only if each of its nodes is with a depth of K

A full binary tree is called when the number from 1 to N in a two-ary tree corresponds to a node one by one. B

(3) at most 2i-1 nodes (I>=1) on the first layer of a binary tree

(4) Two forks with a depth of k at most 2k-1 nodes (k>=1)

The storage structure of binary tree

1. Sequential storage structure

With a set of contiguous storage units at the top and bottom, from the left and right to store a complete binary tree node element, note that this storage structure only applies to a complete binary tree , "0" means that there is no such node

2. Chained storage structure

Non-contiguous storage structure, using the pointer domain to connect each node to form a chain, respectively, there are two linked list (data fields and pointers to the left and right sub-tree, b) and the three-strand list method (data field, pointing to the parent node and the pointer to the left and right sub-tree, c).

two traversal of a fork tree

First Order traversal

If the binary tree is empty, then empty operation, otherwise

(1) Accessing the root node

(2) Sequence traversal Zuozi First ORDER traversal:-* a b C

(3) First order traversal of right sub-tree

Middle Sequence traversal

If the binary tree is empty, then empty operation, otherwise

(1) Middle sequence traversal Zuozi

(2) Access to the root node sequence traversal: A * b-c

(3) The middle sequence traverses the right sub-tree

Post-post traversal

If the binary tree is empty, then empty operation, otherwise

(1) post-Zuozi traversal

(2) post-sequential traversal of the right sub-tree: a b * C-

(3) Accessing the root node

Implementation of Binary tree:

1#include <stdio.h>2#include <stdlib.h>3typedefCharElemtype;//Data Type4typedefintStatus;//return value type5 #defineERROR 06 #defineOK 17 //Defining a binary tree structure8typedefstructbitnode{9Elemtype data;//DataTen     structBitnode *lchild, *rchlid;//left and right sub-tree pointers One}bitnode, *Bitree; A  - //create a two-fork tree -Status Createbitree (Bitree *T) the { - Elemtype ch; - elemtype C; -scanf"%c", &ch); +c =GetChar (); -     if(' '==ch) +*t =NULL; A     Else at     { -*t = (bitree)malloc(sizeof(Bitnode)); -         if(! (*T)) - exit (ERROR); -(*t)->data =ch; -printf"Enter the left child node of%c with a space indicating no node:", ch); inCreatebitree (& (*t),lchild); -printf"Enter the right child node of%c with a space indicating no such node:", ch); toCreatebitree (& (*t),rchlid); +     } -  the     returnOK; * } $ Panax Notoginseng //first-order traversal of binary tree - Status traversebitree (bitree T) the { +     if(! TreturnERROR; Aprintf"%c", t->data); theTraversebitree (t->lchild); +Traversebitree (t->rchlid); -     returnOK; $ } $  - //Middle sequence Traversal binary tree - Status inorderbitree (bitree T) the { -     if(! TreturnERROR;WuyiInorderbitree (t->lchild); theprintf"%c", t->data); -Inorderbitree (t->rchlid); Wu     returnOK; - } About  $ //Sequential traversal of binary tree - Status postorderbitree (bitree T) - { -     if(NULL = = T)returnERROR; APostorderbitree (t->lchild); +Postorderbitree (t->rchlid); theprintf"%c", t->data); -     returnOK; $ } the  the  the //two depth of the fork tree the inttreedeep (bitree T) - { in     intDeep =0; the     if(T) the     { About         intLeftdeep = Treedeep (t->lchild); the         intRightdeep = Treedeep (t->rchlid); theDeep = Leftdeep >= rightdeep? Leftdeep +1: Rightdeep +1; the     } +     returnDeep ; - } the Bayi //two fork Tree leaf node number the  the //Main function - intMainvoid) - { the Bitree T; the     intstr, num =0; theprintf"Enter the value of the first node, and a space indicates no leaf node: \ n"); theCreatebitree (&T); -printf"First Order traversal binary tree: \ n"); the Traversebitree (T); theprintf"\ n"); theprintf"Middle Sequence Traversal binary tree: \ n");94 Inorderbitree (T); theprintf"\ n"); theprintf"Post- secondary traversal of the binary tree: \ n"); the Postorderbitree (T);98printf"\ n"); Aboutstr =Treedeep (T); -printf"the depth of the tree is:%d", str);101printf"\ n");102System"Pause");103     return 0;104}

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.