Data structure--two fork tree (binary Trees)

Source: Internet
Author: User

Nonlinear data structures

Density of the tree = node count/height

Two-fork Tree class

1 #pragmaOnce2 3 classStnode4 {5      Public:6         intNodeValue;//node Data7 8Stnode *left, *right, *parent;//Child pointers and pointer to the node ' s parent9 Ten             //Constructor OneStnode (Const intItem, Stnode *lptr = null, stnode*rptr = NULL, Stnode *pptr =NULL): A NodeValue (item), left (LPTR), right (Rptr), parent (PPTR) -    {} - }; the  - classStree - { -  Public: +Stree ();//constructor. Initialize root to NULL and size to 0 -~stree ();//destructor +     BOOLInsertConst intitem); A     voidOutput (); at  - Private: -Stnode *root;//pointer to tree root -     intTreeSize;//Number of elements in the tree -Stnode *creatstnode (Const intItem, Stnode *lptr, Stnode *rptr, stnode*pptr); - }; in  -Stnode * Stree::creatstnode (Const intItem, Stnode *lptr, Stnode *rptr, Stnode *pptr) to { +stnode*NewNode; -  the     //Initialize the data and all pointers *NewNode =NewStnode (item, LPTR, Rptr, pptr); $ Panax Notoginseng     returnNewNode; -}

Full binary tree (complete tree):

All non-leaf nodes have two subnodes or one left dial hand node. Build on the left-to-right order.

Full binary tree h= (int) with n elements (log2 (n))

Traverse:

1. Hierarchical traversal

Traverse from left to right by layer.

1 //sequence Traversal binary tree2 voidStree::levelbylevel (Stnode *root)3 { 4Std::queue<stnode*> Q;//Construction Team5Q.push (root);//The root node is enqueued6Stnode *cur;7      while(!q.empty ())8     { 9Cur=q.front ();//get the first element of the queueTenQ.pop ();//first element out of the team OneTemp. Format ("%d", Cur->nodevalue);//the value of the output node Astr+=temp; -          -         if(Cur->left!=null)//if the left subtree of the node is not empty. the         {  -Q.push (cur->Left );  -         }  -         if(Cur->right!=null)//if the right sub-tree of the node is not empty. +         {  -Q.push (cur->Right );  +         }  A     }  at}
View Code

2, Middle sequence traversal (LDR)

Access the left node data until the left is empty, then access the intermediate (parent node) data, and then access the right child node data.

Steal a picture of Baidu:

3. Pre-sequence traversal (DLR)

First, access the parent node data, and then access the left Dial hand node, the last right child node. Reached is access, the root node is the first one to traverse.

The preceding sequence traversal result is: ABDGJEHCFI

4. Post-sequential traversal (LRD)

First, access the left Dial hand node data, then access the right child node, the last parent node. The root node is the last one to traverse.

The preceding sequence traversal result is: JGDHEBIFCA

Recursion of the tree

1. Recursive traversal of leaf nodes

void Countleaf (tnode<t> *t,int &count) {    if(t!=NULL)    {          If(t->left==null&&t->right==NULL)            count+ +;        Countleaf (T-left,count);        Countleaf (T-right,count);}    }

2, the height of the tree

1 intDepth (tnode<t> *t)2 {3     intDepthleft,depthright,depthval;4     if(t==NULL)5depthval=-1;6     Else7     {8Depthleft=depth (t->Left );9Depthright=depth (t->Right );TenDepthval=1+ (depthleft>depthright?)depthleft:depthright); One     } A     returnDepthval; -}

3. Delete whole tree

1 void deletetree (tnode<t> *T)2{3     if(t!=NULL)  4    {5         deletetree (t-> left); 6         DeleteTree (t-> right); 7         Delete t; 8     }9 }

Data structure--two fork tree (binary Trees)

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.