Shallow into the tree with two fork tree

Source: Internet
Author: User

1. First knowledge of trees

First of all, look at some very important concepts of trees. The degree of the node is the number of subtrees owned by this node, and if the degree is 0 then this node is called the leaf node. The depth of the tree is the maximum number of layers for all nodes in the tree, and the degree of the tree is the maximum of each node degree. If the left and right sub-tree of each node of the tree is ordered, it is an ordered tree, and the other is an unordered tree. Tree traversal is divided into pre-order, middle-order and post-sequence, there are many types of trees, the following are some common tree types.

Binary tree: Two each node of the tree will not have more than 2 child nodes, and it is an ordered tree, the left and right subtree can not be exchanged, if the exchange of the left and right sub-tree will become another different tree.

Full two fork tree: In a binary tree, if all non-leaf nodes have 2 child nodes, and all leaf nodes are on the same layer (that is, all leaf nodes to the root node of the same path) is a full two fork tree. The degree of its nodes is only 0 and 2.

Fully binary tree: You can think of it as a two-fork tree from right to left minus the leaf node. It is characterized by that all leaf nodes are only on the lower or lower level, and the lowest leaf nodes are on the left. A complete binary tree with a depth of n must be full of two forks in the n-1 layer.

Binary sort tree: Also known as binary search tree, the feature is that all nodes of Zuozi are small root node, all nodes of right subtree are small root node and the Zuozi of one node and right subtree are two forks sort tree, so there is no equal tree in binary sort tree.

Red-Black Tree: Its data structure is the value, left child node, right child node, color, and parent node. When it is not empty, all nodes are red or black-colored nodes and the root node is black, all null nodes are leaf nodes and the color is black, all red node's child nodes are black, and all paths from any node to its leaf node contain the same number of black nodes.

Balanced binary tree: it is on the basis of the binary sorting tree added a feature, its left and right sub-tree height difference can not exceed 1, if due to insert or delete break this rule will need to rotate restore balance.

2. The nature of the two-fork tree

The binary tree has some of the following major properties. A binary tree layer I has a maximum of 2^ (i-1) nodes; a two-fork tree with a depth of K has a maximum of 2^k-1 nodes; for a non-empty two-fork tree, if the leaf node is n0 and the node tree of 2 is N2, there is k=j+1. The total node of this binary tree is n, which is equal to the sum of 0, 1, and 2 of the child nodes respectively, so that an equation n=n1+n2+n3 can be obtained. The next step is to take advantage of the branches in the binary tree, assuming that the number of branches is m, except that the root node has no parent node, all the remaining nodes have a parent node that has a "branch", for nodes with 2 child nodes, each of these nodes represents 2 branches, and the node of a child node represents a branch. This can get another two equation m=n-1,m=n1+2n2. N0=n2+1 can be obtained by these two equations;

A full binary tree with n nodes has a depth of k, then K is lbn+1, by the definition of a complete binary tree, the range of nodes with a depth of K is 2^ (k-1) <=n<2^k-1, the logarithm of the inequality can be lbn<k<=lbn+1, so K is lbn+1. If a full binary tree with n nodes is numbered from left to right from top to bottom, for a node with any ordinal number k, it is not 1 o'clock the parent node ordinal is K/2. If 2k<=n, then its left child node ordinal is 2k, if 2k>n then the node is the left child node. If 2k+1<=n, the right child node is 2k+1 and the node has no right child node when 2k+1>n.

3. Two cross-tree traversal

struct binarytree{    int value;    binarytree* Lefttree;    binarytree* righttree;};/ /Pre-order traversal void Fronttraverse (binarytree* root) {    if (root!=null)    {        if (root->lefttree)            Fronttraverse ( Root->lefttree);        printf ("%d", root->value);        if (root->righttree)            fronttraverse (root->righttree);    }} Middle order traversal void Middletraverse (binarytree* root) {    if (root!=null)    {        printf ("%d", root->value);        if (root->lefttree)            fronttraverse (root->lefttree);        if (root->righttree)            fronttraverse (root->righttree);    }} Post-post traversal of void Behindtraverse (binarytree* root) {    if (root!=null)    {        if (root->lefttree)            Fronttraverse (root->lefttree);        if (root->righttree)            fronttraverse (root->righttree);        printf ("%d", Root->value);}    }

4. Basic operation of two-fork tree

#include <stdio.h> #include <stdlib.h>struct binarytree{int value;    Binarytree* Lefttree;    Binarytree* Righttree;        BinaryTree () {lefttree=null;    Righttree=null;    }};//Initialize binary tree void Initbitree (binarytree* root) {binarytree* node1=new BinaryTree ();    binarytree* node2=new BinaryTree ();    binarytree* node3=new BinaryTree ();    binarytree* node4=new BinaryTree ();    binarytree* node5=new BinaryTree ();    node1->value=4;    node2->value=12;    node3->value=5;    node4->value=10;    node5->value=15;    root->lefttree=node1;    root->righttree=node2;    node1->righttree=node3;    node2->lefttree=node4; NODE2-&GT;RIGHTTREE=NODE5;}    Middle order traversal void Middletraverse (binarytree* root) {if (root==null) return;    if (root->lefttree) middletraverse (Root->lefttree);    printf ("%d", root->value); if (root->righttree) middletraverse (root->righttree);}  Added two fork tree node void Addbitree (binarytree* root,int key) {  if (root==null) return;    binarytree* keynode=new BinaryTree ();    keynode->value=key;    Binarytree* Node=root;    Binarytree* PrevNode;            if (Key>node->value) {while (Node&&node->value<key) {prevnode=node;        node=node->righttree;        The IF (node==null)//node node may be NULL at this time prevnode->righttree=keynode;            else {//Now node's value is larger than key prevnode->righttree=keynode;        keynode->righttree=node;            }} else {while (node&&node->value>key) {prevnode=node;        node=node->lefttree;        The IF (node==null)//node node may be NULL at this time prevnode->lefttree=keynode;            else {//Now node's value is smaller than key prevnode->lefttree=keynode;        keynode->lefttree=node; }    }}
Find a node, get a reference to this node, and the parent node void Querybitree (BinaryTree *root,int key,binarytree **keynode,binarytree **parentnode) {if ( Root==null) return; if (Root->value==key) {*keynode=root; Return } binarytree* Node=root; if (Root->lefttree&&root->lefttree->value==key) {*keynode=root->lefttree; *parentnode=root; Return } if (Root->righttree && root->righttree->value==key) {*keynode=root->righttree; *parentnode=root; Return } if (Root->value>key) Querybitree (Root->lefttree,key,keynode,parentnode); if (Root->value<key) Querybitree (Root->righttree,key,keynode,parentnode);}
Delete a node void Deletebitree (binarytree* root,int key) {if (root==null) return; First find this node bool Isleft=false; Binarytree* Keynode=null; Binarytree* Parentnode=null; Querybitree (Root,key,&keynode,&parentnode); if (Parentnode&&parentnode->lefttree==keynode) isleft=true; 1. If the node has a right subtree, looking for the leftmost node of the right subtree, this node may also be the root node. if (keynode->righttree) {binarytree* leftnode=keynode->righttree; If the left node of the right child node of the deleted node is empty if (leftnode->lefttree==null) {//assigns the value of the right child node to Keynode and deletes the Keynode->rightno De keynode->value=keynode->righttree->value; keynode->righttree=keynode->righttree->righttree; } else {binarytree* parentleftnode; Keep looking for the left-most node of the right subtree while (Leftnode->lefttree) {//leftnode's parent node Parentle Ftnode=leftnode; leftnode=leftnode->lefttree; } At this point the Leftnode is the leftmost node in the right subtree and assigns the value of the node to Keynode keynode->value=leftnode->value; parentleftnode->lefttree=leftnode->righttree; } Delete Leftnode; Return }//2. If the node has only left dial hand trees, here is a discussion, because the node to be deleted is probably the root node if (keynode->lefttree&&keynode->righttree==null) { If the node being removed at this time is the root node if (parentnode==null) root=root->lefttree; else {//if the right subtree of the deleted node is empty, remove this node directly if (Isleft) PARENTNODE-&GT;LEFTTREE=KEYNODE-&G T;lefttree; else parentnode->righttree=keynode->lefttree; Delete Keynode; } return; }//3. If the node itself is a leaf node if (keynode->lefttree==null&&keynode->righttree==null) {//If the node is the root node at this time if (keynode==root) root=null; else {if (isleft) parentnode->lefttree=null; else parentnode->lefttree=null; } Delete Keynode; }}void Main () {binarytree* root=new BinaryTree (); root->value=6; Initializes the binary tree initbitree (root); New node 9,5 addbitree (root,3); Middletraverse (root); printf ("\ n"); Addbitree (root,9); Middletraverse (root); printf ("\ n"); Delete node//deletebitree (root,3); Deletebitree (root,4); Deletebitree (root,5); Deletebitree (root,6); Deletebitree (root,9); Deletebitree (root,12); Middletraverse (root); printf ("\ n");}

This is what I wrote about the binary tree creation, new, find and delete code, if there are errors, please point out the friends!

Shallow into the tree with 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.