Defining and implementing a two-fork tree

Source: Internet
Author: User


/*


1. Node: node contains a data element and several points to its subtree branch
2. Degrees node: The number of nodes has become a subtree of nodes
3. Leaf node: node 0 is called Leaf Junction.
4. Branch nodes: nodes with degrees not 0 are called branch nodes
5. The degree of the tree: the maximum value of the degree of all nodes in the tree
6. Two fork tree: is a set of N (n>=0) finite nodes. The n=0 tree is called an empty binary tree. The N=1 tree has only one root node;
N〉1 's two-fork tree consists of a root node and up to two sub-binary trees, called Saozi right subtree, which intersect each other.
Binary tree is not an ordered tree, this is due to a node in a binary tree, even if there is only one subtree to distinguish whether it is Zuozi or right sub-tree;
And for an ordered tree. Assuming that a node has only one subtree, it must be the first subtree.

7. There are 5 types of all nodes of the two fork tree: Empty nodes, no left and right sub-tree nodes, only left dial hand tree nodes. There are only nodes with right and left subtree.


8. Full two fork tree: in a binary tree. Assuming that all branch nodes have Saozi right subtrees, and that all leaf nodes are on the same layer, this binary tree is called a full two-fork tree


9. Fully binary tree: Suppose that a two-tree with n nodes has the structure of the top n nodes of a two-fork tree, which is called a fully binary tree.


102 The nature of the fork tree:
(1): If the number of layers of the root node is specified as 0, then a non-empty binary tree on the layer I has a maximum of 2^i (i>=0) nodes


(2):
If the depth of the two-fork tree with only the root node is 0, then the maximum number of nodes for a two-fork tree with a depth of K is 2^ (k+1)-1 (k>=-1)
(3):
For a non-empty two-fork tree. Assuming that the number of leaf nodes is n0, the number of nodes with a degree of 2 is N2. Then there are n0=n2+1
(4):
The total binary tree with n nodes has a depth k of the smallest integer greater than or equal to ln (n+1)-1
(5):
For a fully binary tree with n nodes, assuming that all the node numbers are numbered from 0 sequentially according to the top-down and left-to-right order, the nodes with the ordinal I (0<=i<n) are:
Suppose i〉0. The sequence number of the parent node of the I node is (i-1)/2 (/is divisible); suppose i=0. The number is the I node is the root node, no parent node
Assuming 2i+1<n, the ordinal number of the left child node of the I node is 2i+1; Assuming 2i+1>=n, the ordinal is I node no left child
Assuming 2i+2<n, the ordinal number of the right child node of the I node is 2i+2; 2i+2>=n. The number is I node no right child


11. Storage structure of two-fork tree
1. Sequential storage structure of two-fork tree
Using the property 5, for a fully binary tree can be used to store a one-dimensional array, assuming not a completely binary tree. is able to fill the empty node, making it a completely binary tree in storage,
But for a non-total binary tree, a lot of space may be wasted.




2. Two-tree chain storage structure
The chain storage structure of binary tree is to establish the relationship between nodes in two-fork tree with pointers, and the most frequently used chain storage structure of binary tree is two fork chain. The two-strand storage structure of a binary tree is a frequently used
Binary tree storage structure.

The advantages of the binary chain storage structure. Simple structure. It is easy to construct a two-fork tree of whatever shape. And it can easily realize most operation of two-fork tree.
The disadvantage of the binary chain storage structure is that it is more troublesome to find the parent node operation of the current node.


3. Simulation pointer storage structure for two-fork tree
Using one-dimensional arrays and struct-body implementations. The operation of binary tree by using the subscript of array to simulate the pointer

*/


<span style= "FONT-SIZE:18PX;" > #include <stdio.h> #include <malloc.h>typedef char datatype;typedef struct Node{datatype data;// Data domain struct node *leftchild;//left dial hand tree pointer struct node *rightchild;//right subtree pointer}bitreenode;//node structure definition//Initialize void Initiate (Bitreenode **root) {*root= (Bitreenode *) malloc (sizeof (Bitreenode));(*root)->leftchild=null; (*root)->rightchild=null;} Left Insert node//If the current node Curr non-empty. The Curr Zuozi inserts a new node with an element value of X//The left subtree of the node referred to in the original Curr becomes the new insertion node Zuozi//If the insert succeeds. Returns a pointer to the new insertion node, otherwise returns a null pointer Bitreenode *insertleftnode (bitreenode *curr,datatype x) {Bitreenode *s,*t;if (curr==null) {// Infers whether the current node is empty return null;//is empty returns null}t=curr->leftchild;//the left subtree of the node to which the original Curr is stored s= (Bitreenode *) malloc (sizeof (Bitreenode );//Create node Space s->data=x;//assignment s->leftchild=t;//the left subtree of the new insertion node is the original Curr left subtree s->rightchild=null;//the right subtree is empty curr-> leftchild=s;//The new node becomes the left subtree of the Curr return curr->leftchild;//returns a pointer to the new insertion node}//the right Insert node//If the current node is curr non-empty. The right subtree of the Curr inserts a new node with an element value of X//The right subtree of the node referred to in the original Curr becomes the right subtree of the new insertion node//If the insert succeeds, returns a pointer to the new insertion node, otherwise returns a null pointer Bitreenode *insertrightnode ( Bitreenode *curr,datatype x) {Bitreenode *S,*t;if (curr==null) {//infers if the current node is empty return null;//is empty returns null}t=curr->rightchild;//the right subtree Curr (s= *) of the node to which the original Bitreenode was saved. malloc (sizeof (Bitreenode));//Create node Space s->data=x;//assignment s->rightchild=t;//the right subtree of the new insertion node is the right subtree of the original Curr s->leftchild= null;//right subtree is empty curr->rightchild=s;//new node becomes Curr right subtree return curr->rightchild;//returns a pointer to a new insertion node}//left Delete subtree//If Curr is not empty, Delete the Zuozi//of the node referred to by Curr if the deletion succeeds, the parent node of the deleted node is returned. Otherwise, return null pointer bitreenode *deletelefttree (Bitreenode *curr) {//If the current node is empty or the left subtree is empty then return Nullif (curr==null| | Curr->leftchild==null) {return NULL;} Release node//destroy (&curr->leftchild); curr->leftchild=null;//Delete, the left subtree of the current node is Nullreturn curr;//returns the parent node of the deleted node}/ /Right Delete subtree//If Curr is not empty. Delete the right subtree of the node referred to by Curr//If the deletion succeeds, the parent node of the deleted node is returned, otherwise the null pointer bitreenode *deleterighttree (Bitreenode *curr) {// Returns NULLIF if the current node is empty or if the right subtree is empty (curr==null| | Curr->rightchild==null) {return NULL;} Release node//destroy (&curr->rightchild); curr->rightchild=null;//Delete, the right subtree of the current node is Nullreturn curr;//returns the parent node of the deleted node }void Visit (DataType Item) {printf ("%c", item);} Pre-sequence traversal/*1. Access root node 2. Pre-order traversal of the left subtree of the root node 3. The right subtree of the root node is traversed by the pre-order */void PreordeR (Bitreenode *root,void Visit (DataType Item)) {///pre-order traversal binary tree root, access operation is Visit () function if (root!=null) {Visit (root->data);// Access data preorder (root->leftchild,visit);//Visit Left Dial hand tree preorder (root->rightchild,visit);//rhetorical question right subtree}}//middle sequence traversal/*1. The middle sequence traverses the left subtree of the root node 2. Access root node 3. The right subtree of the root node in the middle order traversal */void inorder (Bitreenode *root,void Visit (DataType Item)) {//middle sequence traverse binary tree root, access operation is Visit () function if (root!=null) {inorder (root->leftchild,visit);//interview Left Dial hand tree Visit (root->data);//Access Data inorder (root-> RIGHTCHILD,VISIT);//Visit Right subtree}}//post-traverse/*1. Post-traverse the left subtree of the root node 2. Post-sequential traversal of the right subtree of the root node 3. Access root node */void postorder (Bitreenode *root,void Visit ( DataType item) {//middle order traversal binary tree root, access operation is Visit () function if (root!=null) {postorder (root->leftchild,visit);//Visit Left Dial hand tree Postorder (root->rightchild,visit);//Visit Right subtree Visit (root->data);//Access root node data}}//undo two fork Tree operation Void Destroy (Bitreenode **root) {if ( *root)!=null&& (*root)->leftchild!=null) {Destroy (& (*root)->leftchild);} if ((*root)!=null&& (*root)->rightchild!=null) {Destroy (& (*root)->rightchild);} Free (*root);} void PrintbitrEE (bitreenode *root,int N) {//counterclockwise rotated 90 degrees. Print binary tree root. n is the indent layer, the initial value is 0int i;if (root==null) {return;//Recursive Exit}printbitree (root->rightchild,n+1);//traverse print right subtree//visit root node for (i=0;i <n-1;i++) {printf ("");} if (n>0) {printf ("---");p rintf ("%c\n", Root->data);} Printbitree (root->leftchild,n+1);//traverse print right subtree}//find data Element Bitreenode *search (bitreenode *root,datatype x) {// Find if the data element x is in the binary tree root//Find to return the node pointer. Returns null pointer Bitreenode *find=null;if (root!=null) {if (root->data==x) {find=root;} not found Else{find=search (root->leftchild,x);//Find if (find==null) {Find=search (root->rightchild,x) in the left subtree;//Find in the right subtree}}} Return find;//Returns the lookup flag}void main () {Bitreenode *root,*p,*find;char x= ' E '; Initiate (&root);//initial header pointer P=insertleftnode ( Root, ' a ');//Insert Left subtree P=insertleftnode (p, ' B ') at the head node;//Give ' A ' A ' Insert left subtree P=insertleftnode (p, ' D ');//p=insertrightnode (P, ' G ');//p =insertrightnode (Root->leftchild, ' C ');//Give ' A ' A ' Insert right subtree Insertleftnode (p, ' E ');//insertrightnode (P, ' F ');//Printbitr    EE (root,0);//rotate 90-degree print tree printf ("Pre-sequence Traversal:"); Preorder (root->leftchild,visit);p rintf ("\ nSequential traversal: "); Inorder (root->leftchild,visit);p rintf (" \ n post-order traversal: "); Postorder (Root->leftchild,visit), Find=search (root,x), if (find!=null) {printf ("\ n data element%c in binary tree \ n", x);}       else{printf ("\ n data element%c is not in binary tree \ n", x);} Destroy (&root);} </span>































Defining and implementing a 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.