Algorithm of binary tree and its FA

Source: Internet
Author: User

Binary tree belongs to the hierarchical data relations in the structure, and his ancestors-descendants, superiors-subordinates, whole-part and other similar relations, tree structure in the computer field has a wide range of applications, such as in the compiler of the golden mean syntax tree to represent the language structure of the meta-Program, In the data mining mean decision tree for data classification and so on. In my previous blog also mentioned is the relevant knowledge of binary tree focus. The unclear peers can refer to my article. If there are any irregularities, please advise us.

Here are a few common functions of the two-fork tree that I wrote when I was learning a binary tree, along with some of his constructors and so on.

#ifndef Bitree_h
#define Bitree_h
#include <iostream>
#include <iomanip>
using namespace Std;
Template <typename t>
struct Binode
{
T data;
Binode<t> *lchild, *rchild; The construction of nodes using recursive method
};
Template<typename t>
Class Bitree
{
Template<typename t>
Friend Ostream & operator<< (ostream &os, bitree<t> &AMP;BT);
Public
Bitree (T none); Construct an empty two-fork tree
Bitree (t ary[], int num, T none); Constructs a two-fork tree of NUM nodes.
~bitree (); Destructors
void Parent (T x); Corresponds to the parentin in the pretected
void print (Ostream &os); Traversal operations
void Count (); Corresponds to the countin in the protected
void Preorderprint (); Corresponds to the preorderprintin in the protected
void Depth (); Corresponds to the Depthin in the protected
void Postordern (); Corresponds to the Postordern in the protected
void Delete (T x); Corresponds to the Deletein in the pretected
Protected
void Countin (binode<t> *root);
void Creat (binode<t> * &root, T none);
Binode<t> *build (t ary[], int num, t none, int idx);
Binode<t> *parentin (binode<t> * root, T x); Query the double of a node.

Kiss
void Release (binode<t>* &root);
void Printin (Ostream &os, binode<t> *root, int depth); And the above four functions

has the corresponding function
void Preorderprintin (binode<t> *root); Finding the leaf section of a binary tree

Point output
int Depthin (binode<t> *root); Finding the depth of a binary tree
void Postordernin (binode<t> *root); Finding the inverse of a binary tree

Algorithm for sequential output traversal
void Deletein (binode<t> * root, T x); Seeking the deletion of binary tree

The algorithm

Private
binode<t> *p = NULL;
Binode<t> *rootptr; Apply for a pointer with
int count = 0; Global variables are used in the Count function
int HIGHL = 0,highr=0; Global variables in the depth function
};
Template<typename t>
void Bitree<t>::countin (binode<t> *root)
{
if (root = NULL)
{
Countin (Root->lchild);
count++;
Countin (Root->rchild);
/* between left and right count++ is the end of the recursive
At the end of the last left child is +*/.
}
}
Template<typename t>
void Bitree<t>::creat (binode<t> * &root, T None)//number of children created
{
T x;
cout << "Please enter (" << none << "means empty):"; CIN >> X;
if (x = = none)
root = = NULL; If the input is empty, then this binary tree

is an empty binary tree.
else//below prepare to implement element insertion
{
root = new binode<t>; First let the root pointer change to the root node.
Root->data = x; Assigning values to a node
Creat (Root->lchild, none); Creates the left child of the root node, at which point the number of left dial hand is empty
Creat (Root->rchild, none); Creates the right child of the root node, at which point the right number is empty
}//The current node assignment succeeds, and its child creation is also successful
}                                     // The secondary function is not a loop creation, but it is assigned at the root node.

The creation of the number of children, the following operation depends on other
Template<typename t>
binode<t> *bitree<t>::build (t ary[], int num, t none, int idx)
{
 /*ary represents the array, num represents the array length, none means null, and IDX represents the node ordinal
  The value assigned to 1*/
 binode for the first operation <T> *p;                     //Application pointer p
 int left, right;                  //define left and right
 if (Idx-1 < num&&ary[idx-1]! = none)// Why is idx-1 here????
 {
  /* Since the array starts at 0 and the IDX is able to grow in multiples, the IDX starts at the beginning
   then naturally subtracts one at the time of judgment, convenient for subsequent assignments */
  p = new binode<t>;           //The pointer p node is zoned
  p->data = ary[idx-1];      //assigns the value portion of the node to an array

element of


  left = 2 * IDX;
  right = 2 * idx + 1;         //ensures the left and right of the left and right of the number of children
   P->lchild = Build (ary, num, none, left);
  p->rchild = Build (ary, num, none, right);//Start Assignment
  return p;
 }
 else
  return null;         // else means that the array has been assigned and no value has been extended to the

So return null
}
Template<typename t>
void Bitree<t>::release (binode<t> * & Root)//have release meaning
{
 if (root! = NULL)                       //Start
 {
  release (Root->lchild) from the root node first;
  release (Root->rchild);
  delete root;                      //release of the subsequent traversal
 }
}
Template<typename t>
void Bitree<t>::count ()
{
 countin (rootptr);
 cout << "|" << SETW (5)   << "tree node Count:" << count <<setw (5) << "|" <<

Endl;
}
Template<typename t>
Bitree<t>::bitree (T None)
{
 creat (rootptr, none);
}
Template <typename t>
Bitree<t>::bitree (t ary[], int num, t none)
{
 rootptr = Build (ary, Num, none, 1);  //none used to pass the empty
}
template <typename t>
Bitree<t>::~bitree ()
{
 release (ROOTPTR);
}
Template <typename t>
void bitree<t>::p rintin (ostream &os, binode<t> *root, int Depth)
{
 if (root = NULL)
 {
  printin (OS, root->rchild, depth + 1);
  for (int i = 0; i < 4 * (depth-1); i++)
   os << "";
  os << "*--" << root->data << endl;  //This sentence is a function common to all
  printin (OS , Root->lchild, depth+1);

}
}
Template<typename t>
void Bitree<t>::P reorderprintin (binode<t> *root)
{
if (root = NULL)
{
if (root->lchild==null&&root->rchild==null)
cout <<SETW (3) << root->data; First, the root node is output.

Data
Preorderprintin (Root->lchild); The left node output is then started.
Preorderprintin (Root->rchild); And then the right node output.
}
}
Template<typename t>
void Bitree<t>::P Reorderprint ()
{
Preorderprintin (ROOTPTR);
}
Template<typename t>
int bitree<t>::D Epthin (binode<t> *root)
{
if (root = NULL) return 0; Flag Access End
else//flag continues down access
{
HIGHL = Depthin (root->lchild);//Why is it possible to assign values in this step?
HIGHR = Depthin (root->rchild);
if (Highl > Highr)
return HIGHL + 1;
Else
return HIGHR + 1;
}
}
Template<typename t>
void Bitree<t>::D epth ()
{
Cout<<depthin (ROOTPTR);
}
Template<typename t>
void Bitree<t>::P Ostordernin (binode<t> * root)
{
if (root = NULL)
{
cout << SETW (3) << root->data;
Postordernin (Root->rchild);
Postordernin (Root->lchild);
}
}
Template<typename t>
void Bitree<t>::P Ostordern ()
{
Postordernin (ROOTPTR);
}
Template<typename t>
Binode<t> *bitree<t>::P arentin (binode<t> *root, T x)
{
if (root = NULL)
{
if (Root->data = = x) cout<<p->data;
Else
{
p = root;
Parentin (Root->lchild, x);
Parentin (Root->rchild, x);
}
}
return 0;
}
Template<typename t>
void Bitree<t>::P arent (T x)
{
Parentin (rootptr, x);
}
Template<typename t>
void Bitree<t>::D Eletein (binode<t> * root, T x)
{
if (root = NULL)
{
if (Root->data = = x)
{
if (p = = NULL)
root = NULL;
Else
if (P->lchild = = root)
P->lchild = NULL;
Else
P->rchild = NULL;
}
Else
{
p = root;
Deletein (Root->lchild, x);
Deletein (Root->rchild, x);
}
}
}
Template<typename t>
void Bitree<t>::D elete (T x)
{
Deletein (rootptr, x);
}
Template<typename t>
void Bitree<t>::p rint (ostream & OS)
{
Printin (OS, rootptr, 1);
}
Template<typename t>
Ostream & operator<< (ostream & OS, bitree<t> &AMP;BT)
{
Bt.print (OS);
return OS;
}
#endif
************************************************************************
#include "BiTree.h"
#include <iostream>
using namespace Std;
void Main ()
{
Char ary[] = {' A ', ' B ', ' C ', ' D ', ' # ',
' E ', ' F ', ' # ', ' G ', ' # ',
' # ', ' H ', ' I ',

' J ', ' K ',
' # ', ' # ', ' L '};
Char zxh,gmy;
Bitree<char> mybtree (ary, 18, ' # ');
cout << mybtree << Endl;
cout << "--------------------------------------------------" << Endl;
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++" << Endl;
cout << "Find the number of binary tree nodes (first algorithm)" << Endl;
Mybtree.count ();
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++" << Endl;
cout << "--------------------------------------------------" << Endl;
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++" << Endl;
cout << "Finding Binary Tree Leaf node order (second algorithm)" <<endl;
cout << "The leaf nodes of the tree are arranged in the Order de mode of output as:";
Mybtree.preorderprint ();
cout << Endl;
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++" << Endl;
cout << "--------------------------------------------------" << Endl;
cout << "To find the depth of the binary tree (the third problem to expose):" << Endl;
cout << "The depth of this binary tree is:" <<endl;
Mybtree.depth ();
cout << "--------------------------------------------------" << Endl;
cout << "Finding the second-order inverse traversal of the binary tree (fourth solution):" << Endl;
cout << "The reverse order traversal of the two-fork tree is:" << Endl;
Mybtree.postordern ();
cout << "--------------------------------------------------" << Endl;
cout << "Finding the parent of a knot in a binary tree (fifth solution):" << Endl;
cout << "Please enter the node you want to query (! Note: case sensitive): ";
Cin >> Zxh;
cout << "The node you are querying is:" << zxh << "His parents are:" << Endl;
Mybtree.parent (ZXH);
cout << Endl;
cout << "--------------------------------------------------" << Endl;
cout << "Please enter the characters you want to delete:";
Cin >> Gmy;
Mybtree.delete (Gmy);
cout << "Two fork tree after deletion:" << Endl;
cout << Mybtree;
}
*****************************************************************************
Personal Summary:
For the first time, I was exposed to a two-prong tree, and I distinctly felt a difference from the list of orders that I learned earlier, the largest

Is that the function in the binary tree is paired because the original two-fork tree function cannot be called in the main function, so

He needs to define a corresponding function in the constructor of the class, so that it can be easily called in the main function. But plainly two

Fork tree, there is no rare, as long as we understand the principle, in a lot of practice is easy to solve.



Algorithm of binary tree and its FA

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.