Binary_tree.cpp: Defines the entry point for a console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace Std;
typedef int ELEMTYPE;
typedef char ELEMTYPE;
typedef struct TREENODE_S
{
Elemtype Data;
struct treenode_s *pleft;
struct treenode_s *pright;
}*ptree,btree;
The creation of two-fork tree
Btree * Createbitree (btree *root, elemtype data)
void Createbitree (Btree *root, elemtype data)
{
btree* Pnewnode = NULL;
Pnewnode = new Btree;
Pnewnode->data = Data;
Pnewnode->pleft = NULL;
Pnewnode->pright = NULL;
if (root = NULL)
{
root = Pnewnode;
return root;
}
Else
{
btree* Pbacknode = NULL;
btree* Pcurrentnode = root;
while (Pcurrentnode!= NULL)
{
Pbacknode = Pcurrentnode;
cout << pcurrentnode->data<<endl;
if (Pcurrentnode->data > Data)//Find Zuozi
{
Pcurrentnode = pcurrentnode->pleft;
}
else if (Pcurrentnode->data < Data)//Find right Subtree
{
Pcurrentnode = pcurrentnode->pright;
}
Else
{
Delete Pnewnode;
return pcurrentnode;
}
}
cout << pbacknode->data<<endl;
Compare insert data with current leaf node, insert right subtree if greater than insert left subtree
if (Pbacknode->data > Data)
{
Pbacknode->pleft = Pnewnode;
}
Else
{
Pbacknode->pright = Pnewnode;
}
return pnewnode;
}
}
Clear the two-pronged tree
void Clearbitree (btree* ptreenode)
{
if (Ptreenode)
{
Clearbitree (Ptreenode->pleft); Empty Zuozi
Clearbitree (Ptreenode->pright); Clear right Subtree
Delete Ptreenode; Releases the content occupied by the current node
}
}
Calculate the number of binary trees
int Bitreecount (Btree *root)
{
if (root = NULL)
{
return 0;
}
Else
{
Return Bitreecount (Root->pleft) + bitreecount (root->pright) + 1;
}
}
Print two-fork tree
void Printbitree (Btree *root)
{
Btree *pnode = root;
if (Pnode!= NULL)
{
cout<<pnode->data<< "";
Printbitree (Pnode->pleft);
Printbitree (Pnode->pright);
}
}
Lookup node, returning a pointer to the node where the data resides
btree* Findtreenode (btree *root, elemtype data)
{
Btree *pnode = root;
if (Pnode = NULL)
{
return NULL;
}
Else
{
Lookup node, returning a pointer to the node where the data resides
if (Pnode->data = = Data)
{
return pnode;
}
Else
{
if (Data < Pnode->data)//Zuozi Recursive lookup
{
Return Findtreenode (pnode->pleft, data);
}
else if (Data > Pnode->data)//right subtree recursive lookup
{
Return Findtreenode (Pnode->pright,data);
}
Else
{
return NULL;
}
}
}
}
Find leaf nodes
int Findleafnodenum (btree* root)
{
static int n = 0;
Btree *ptreenode = root;
if (Ptreenode = NULL)
{
return 0;
}
Else
{
if (Ptreenode->pleft = NULL
&& ptreenode->pright = NULL)
{
n + 1;
}
Else
{
Findleafnodenum (Ptreenode->pleft);
Findleafnodenum (Ptreenode->pright);
}
return n;
}
}
First-order traversal of the binary tree: first access to the root, and then traversing the left subtree, and then traversing the right subtree
void Preorderbitree (btree* root)
{
btree* Ptreenode = NULL;
Ptreenode = root;
if (Ptreenode = NULL)
{
return;
}
Else
{
cout << ptreenode->data << "";
Preorderbitree (Ptreenode->pleft);
Preorderbitree (Ptreenode->pright);
}
}
//Sequence Traversal binary tree: First traverse the left subtree, then the node data field, and then traverse the right subtree
void Inorderbitree (btree* root)
{
btree* Ptreenode = NULL;
ptreenode = root;
if (Ptreenode = NULL)
{
return;
}
Else
{
inorderbitree ( Ptreenode->pleft);
cout << ptreenode->data << "";
Inorderbitree (ptreenode->pright);
}
}
//Sequence Traversal binary tree: First traverse the left subtree, then traverse the right subtree, the last node data field
void Postorderbitree (btree* root)
{
btree* Ptreenode = NULL;
ptreenode = root;
if (Ptreenode = NULL)
{
return;
}
Else
{
postorderbitree (Ptreenode->pleft);
Postorderbitree (ptreenode->pright);
cout << ptreenode->data << "";
}
}
//Add node
void Addtreenode (Btree *root, elemtype data)
{
btree* pparent = NULL;
&N bsp; btree* pnode = new Btree;
pnode->data = Data;
pnode->pleft = NULL;
pnode->pright= NULL;
//The root node is empty, this creates the root node
if (root = NULL)
{
root = Pnode;
}
Else
{
pparent = Findtreenode (root, data);
if (!pparent)
{
}
}
//Compute the depth of the binary tree
int bitreedepth (btree *root)
{
btree *ptreenode = root;
& nbsp int depleft = 0;
int depright = 0;
if (Ptreenode = NULL)
{
return 0; //node is empty, depth is 0
}
Else
{
&nb sp; depleft = bitreedepth (ptreenode->pleft);
depright = bitreedepth (ptreenode->pright);
if (Depleft > Depright)
{
return ++depleft
}
Else
{
return ++depright;
}
}
int _tmain (int argc, _tchar* argv[])
{
Elemtype data;
Btree *proot = NULL;
btree* Ptreenode = NULL;
Constructing the root node
cin>>data;
Ptreenode = Createbitree (proot, data);
Proot = Ptreenode;
Add tree node
{7,4,2,3,15,36,6,45,55,20,1,14,56,57,58};
while (1)
{
cout<< "PLEASD input node data:" <<endl;
cin>>data;
if (data = = ' # ')//' # ' ASSCII = 35
{
Break
}
Ptreenode = Createbitree (proot, data);
}
First-order traversal
cout<< "Preorderbitree" <<endl;
Preorderbitree (Proot);
cout<<endl;
In-sequence traversal
cout<< "Inorder bitnary tree:" <<endl;
Inorderbitree (Proot);
cout<<endl;
Subsequent traversal
cout<< "Post order bitnary:" <<endl;
Postorderbitree (Proot);
cout<<endl;
Number of two-fork tree nodes
int num = Bitreecount (proot);
cout<< "Node num:" <<num<<endl;
Number of leaf nodes in two-pronged tree
int leafnum = Findleafnodenum (proot);
cout<< "Leafnode num:" <<leafnum<<endl;
Two Fork Tree Depth
int depth = bitreedepth (proot);
cout<< "Depth:" <<depth<<endl;
#if 1
Find nodes
cout << "Input find data:" <<endl;
CIN >>data;
Ptreenode = Findtreenode (proot, data);
if (Ptreenode!= NULL)
{
cout<<ptreenode->data<<endl;
}
Else
{
cout<< "Can not find" <<data<< "in the Binary tree";
}
#endif
Clearbitree (Proot);
return 0;
}