Tree and two-fork tree

Source: Internet
Author: User

Tree : A finite set of n (n≥0) nodes.

In any non-empty tree: 1. When there is only one node 2.n>1 that is called the root, the remaining nodes can be divided into m (m>0) disjoint finite sets T1,t2,..., Tn,ti is called a subtree.

nature of the tree : recursive hierarchy

Basic Terminology : Tree nodes: Data elements and several branches pointing to their subtrees

Leaf (terminal node): nodes with zero degrees (number of branches)

Branch nodes (non-terminal nodes): nodes with degrees greater than 0

Degree of node: number of Branches

The degree of the tree: the maximum value of the degree of all nodes in the tree

Depth (height) of the tree: the largest layer of leaf nodes in the tree

Child (Knot): The root of the knot point tree is called the child of the node

Ancestor (node): All nodes on the branch from the root to the node.

Descendants (nodes): Any node of a subtree with a node as its root

Brother (Knot): Children of the same parents are called brothers.

Cousin (knot): Parents in the same layer of the knot of each other called cousins

Ordered tree: The subtree of the nodes in the tree is sequentially (not interchangeable) from left to right.

Unordered tree: The subtree of the nodes in the tree is left-to-right without order (can be interchanged).

Forest: M (m≥0) a collection of disjoint trees

two fork tree recursive definition : A finite set of nodes of N (n>=0), or an empty one, or a root node and two disjoint left and right subtrees, and a tree of about two forks.

Five different forms of binary tree:

The nature of the binary tree: (Proof process slightly)

Property 1: There are at most 2i-1 nodes (I>=1) on the first layer of a binary tree.

Property 2: A two-tree with a depth of K has a maximum of 2k-1 nodes (k>=1).

Property 3: To any binary tree, if its terminal node is n0, the degree of 2 of the node is N2, then n0=n2+1.

Property 4: Full binary tree with n nodes with a depth of log2n +1

full two fork Tree : A two-prong tree with a depth of K and a 2k-1 node.

A complete binary tree : If a depth is k, the nodes in the two-tree with n nodes can correspond to nodes of the full two-tree numbering from 1 to n in sequential numbers with a depth of K. All leaf nodes appear on the K-level or k-1 layer. If the maximum level of the right subtree of any node is L, the maximum level of the left subtree is 1 or l+1.

Binary Tree Storage structure: (1) sequential storage structure

(2) Chain-type storage structure

The following code implements a chain-store structure

1#include <stdio.h>2#include <stdlib.h>3     4 #defineOK 15 #defineERROR-16 #defineOVERFLOW-27  8         9typedefintTElem;TentypedefintStatus; One      AtypedefstructBitnode - { - TElem data; the     structBitnode *lchild,*rchild;//child hands left and right -} bitnode,*Bitree; -  - /** * Function prototype **/ +Status Createbitree (Bitree &T); - //Ansion sequence Input binary tree node value, empty characters represents an empty tree, constructs a two fork tree table representation of the two-fork tree T +Status Preordertraverse (Bitree t,status (*visit) (TElem e)); A //using binary tree table storage structure, visit is the application function of corresponding node operation. at //first-order traversal of binary tree T, call function visit once and only once for each node. - //once visit () fails, the operation fails -Status Inordertraverse (Bitree t,status (*visit) (TElem e)); - //the middle sequence traverses the binary tree T, calling the function visit once and only once for each node. -Status Postordertraverse (Bitree t,status (*visit) (TElem e)); - //Post -order traversal of the binary tree T, call function for each node visit once and only once in voidvisit (TElem e); - //Output to   + voidvisit (TElem e) - { theprintf"%d", E);  * }  $ Panax NotoginsengStatus Createbitree (Bitree *T) - { the TElem E; +scanf"%d",&e); A      the     if(e==0) +     { -*t=NULL; $     } $      -     Else -         { the*t = (bitree) malloc (sizeof(Bitnode)); -                 if(!T)Wuyi                 { the exit (OVERFLOW); -                  } Wu(*t)->data =e; -Createbitree (& (*t)->lchild);//Create a left sub-tree AboutCreatebitree (& (*t)->rchild);//Create right subtree $     } -     return 0; - }  -  AStatus Preordertraverse (Bitree T,void(*visit) (TElem))  + { the      -     if(T) $     { theVisit (t->data);  thePreordertraverse (t->lchild,visit); thePreordertraverse (t->rchild,visit); the  -     } in     Else returnOK; the } the  About  theStatus Inordertraverse (Bitree T,void(*visit) (TElem)) the { the     if(T) +     { -Inordertraverse (t->lchild,visit); theVisit (t->data);BayiInordertraverse (t->rchild,visit); the     }     the      - } -  theStatus Postordertraverse (Bitree T,void(*visit) (TElem)) the { the     if(T) the     { -Postordertraverse (t->lchild,visit); thePostordertraverse (t->rchild,visit); theVisit (t->data); the     }94 }  the  the intMain () the {98 Bitree T; About      -printf"Create a tree, enter 0 for the empty number");101Createbitree (&T);102printf"first-order traversal:");103Preordertraverse (T, *visit);104printf"\ n Sequence traversal:"); theInordertraverse (T, *visit);106printf"\ nthe post-traversal:");107Postordertraverse (T, *visit);108printf"\ n"); 109}

Tree and two-fork 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.