The creation of binary tree, the sequence traversal, the sequence traversal and the sequential traversal

Source: Internet
Author: User

BTree.cpp:Defines the entry point for the console application.
/*
Author: Cheng
Time: July 2, 2001 (9:00:00-14:00:00)
Content: The completion of the two-fork tree creation, forward traversal, in-sequence traversal, sequential traversal
Time: July 2, 2001 (14:00:00-16:00:00)
Content: Complete two fork tree leaf node access, swap left and right child
*/
#include "stdafx.h"
#include "Stdlib.h"

#define MAX_NODE 100
#define NODE_COUNT1 8
#define NODE_COUNT2 15

int treevalue0[node_count1][2] = {' 0 ', 0},{' D ', 1},{' B ', 2},{' F ', 3},{' A ', 4},{' C ', 5},{' E ', 6},{' G ', 7}};
int treevalue1[node_count1][2] = {' 0 ', 0},{' A ', 1},{' B ', 2},{' C ', 3},{' D ', 4},{' E ', 5},{' F ', 6},{' G ', 7}};
int treevalue2[node_count2][2] = {' 0 ', 0},{' A ', 1},{' B ', 2},{' C ', 3},{' D ', 4},{' E ', 5},{' F ', 6},{' G ', 7},{' H ', 8},{' I ', 9} , {' J ', 10},{' K ', 11},{' L ', 12},{' M ', 13},{' N ', 14}};
struct Btree
{
int data;
int order;
Btree *lchild;
Btree *rchild;
};

void Swap (int *p1,int *p2)
{
int t;
t = *P1;
*P1 = *P2;
*P2 = t;
}
/*
function Createbtree (): Creates a binary tree and returns a pointer to its root
*/
Btree *createbtree (int data[][2],int N)
{
Btree *addr[max_node];
Btree *p,
*head;
int nodeorder,//node serial number
Noderoot,//node's parents
I
if (N>max_node)
{
printf ("Parameter Error!) \ n ");
return (0);
}
for (i=1;i<=n;i++)
{
p = (Btree *) malloc (sizeof (btree));
if (p==null)
{
printf ("Memory overflow error!) \ n ");
return (0);
}
Else
{
P->data = data[i][0];
P->lchild = NULL;
P->rchild = NULL;
Nodeorder = data[i][1];
P->order = Nodeorder;
Addr[nodeorder] = p;
if (nodeorder>1)
{
Noderoot = NODEORDER/2;
if (nodeorder%2 = = 0)
Addr[noderoot]->lchild = p;
Else
Addr[noderoot]->rchild = p;
}
Else
head = p;
printf ("btree[%d] =%c\t", p->order,p->data);
}
Free (p);
}
return (head);
}

/*
function FirstOrderAccess0 () feature: implementation of two-fork tree in the first sequence traversal
The idea of traversing a binary tree in front of a sequence:
Starting with the root node, go along the Zuozi until the node has no left child,
Access the passed node in turn, and the address of the [node] into the stack;
When a node with no left child is found, the parent of the node is exited from the top of the stack
Right child, at this point, the left subtree of this node has been accessed;
The right subtree of the node is traversed in this way, so that it repeats until the stack is empty.
*/
void FirstOrderAccess0 (Btree * header)
{
Btree * Stack[max_node];
Btree *p;
int top;
top = 0;
p = header;
Todo
{
while (P!=null)
{
printf ("btree[%d] =%c\t", p->order,p->data);//Access Node P
top = top+1;
Stack[top] = p;
p = p->lchild;//continues searching Zuozi of node p
}
if (top!=0)
{
p = stack[top];
top = top-1;
p = p->rchild;//continues searching the right subtree of node p
}
}while ((top!=0) | | (P!=null));
}
/*
function FirstOrderAccess1 () feature: implementation of two-fork tree in the first sequence traversal
The idea of traversing a binary tree in front of a sequence:
Starting with the root node, go along the Zuozi until the node has no left child,
In turn, access the passed node, while the [node is not empty right child] into the stack;
When a node with no left child is found, the parent of the node is exited from the top of the stack
Right child, at this point, the left subtree of this node has been accessed;
The right subtree of the node is traversed in this way, so that it repeats until the stack is empty.
*/
void FirstOrderAccess1 (Btree * header)
{
Btree * Stack[max_node];
Btree *p;
int top;
top = 0;
p = header;
Todo
{
while (P!=null)
{
printf ("btree[%d] =%c\t", p->order,p->data);
if (p->rchild!=null)
Stack[++top] = p->rchild;
p = p->lchild;
}
if (top!=0)
p = stack[top--];
}while ((top>0) | | (P!=null));
}

/*
function middleorderaccess () feature: realization of the sequence traversal of two-fork tree
The idea of sequential traversal in binary tree:
Starting with the root node, go along the Zuozi until the node has no left child,
And the address of the [node] is put into the stack;
When a node with no left child is found, exit the node from the top of the stack and access it.
At this point, the left subtree of this node has been accessed;
The right subtree of the node is traversed in this way, so that it repeats until the stack is empty.
*/
void Middleorderaccess (Btree * header)
{
Btree * Stack[max_node];
Btree *p;
int top;
top = 0;
p = header;
Todo
{
while (P!=null)
{
Stack[++top] = p;//node P into stack
p = p->lchild; Continue searching for its left subtree
}
if (top!=0)
{
p = stack[top--];//node p out stack
printf ("btree[%d] =%c\t", p->order,p->data);//Access Node P
P = p->rchild;//continues to search for its left subtree
}
}while ((top!=0) | | (P!=null));
}

/*
function lastorderaccess (): Realizing the sequence traversal of two-fork tree
Binary tree after the idea of traversal:
Starting with the root node, go along the Zuozi until the node has no left child,
And the address of the [node] is first entered the stack;
When a node with no left child is found, the left subtree of this node has been accessed;
Exit the node from the top of the stack to determine if the node is the first stack, and then
The address of the [node] is added to the stack for a second time, and along the right subtree of the node
Without the right child's node, if no, then access the node, at which point the
The left and right subtrees are fully traversed and the pointer p = NULL;
So repeat until the stack is empty.
*/
void Lastorderaccess (Btree * header)
{
Btree * stack[max_node];//node's pointer stack
int count[max_node];//node Stack count array
Btree *p;
int top;
top = 0;
p = header;
Todo
{
while (P!=null)
{
Stack[++top] = p;//node P first in stack
Count[top] = 0;
p = p->lchild; Continue searching for node P Zuozi
}
p = stack[top--];//node p out stack
if (count[top+1]==0)
{
Stack[++top] = p;//node P first in stack
Count[top] = 1;
p = p->rchild; Continue searching for node P Zuozi
}
Else
{
printf ("btree[%d] =%c\t", p->order,p->data);//Access Node P
p = NULL;
}
}while ((top>0));
}
/*
function Isleafnode (): Determines whether the node of a given binary tree is a leaf node
*/
int Isleafnode (Btree *node)
{
if ((node->lchild==null) && (node->rchild==null)
return (1);
Else
return (0);
}
/*
function Printleafnode (): Output node of a given binary tree
*/
void Printleafnode (Btree *header)
{
Btree * stack[max_node];//node's pointer stack
Btree *p;
int top;
p = header;
top = 0;
Todo
{
while (P!=null)
{
Stack[++top] = p;
p = p->lchild;//continues searching Zuozi of node p
}
if (top!=0)
{
p = stack[top--];
if (Isleafnode (p))
printf ("lnode[%d] =%c\t", p->order,p->data);//Access leaf node
p = p->rchild;//continues searching the right subtree of node p
}
}while (top>0| | P!=null);
}
/*
function Hastwochildnode () feature: Determines whether the node of a given binary tree has two child nodes
*/
int Hastwochildnode (Btree *node)
{
if ((node->lchild!=null) && (node->rchild!=null)
return (1);
Else
return (0);
}
/*
function Swapchildnode (): Swap the left and right children for all nodes of a given binary tree
*/
void Swapchildnode (Btree *header)
{
Btree * stack[max_node];//node's pointer stack
Btree *p;
int top;
p = header;
top = 0;
Todo
{
while (P!=null)
{
Stack[++top] = p;
p = p->lchild;//continues searching Zuozi of node p
}
if (top!=0)
{
p = stack[top--];
if (Hastwochildnode (p))
Swap (&p->lchild->data,&p->rchild->data)//Switch node P's left and right child
p = p->rchild;//continues searching the right subtree of node p
}
}while (top>0| | P!=null);
}


int main (int argc, char* argv[])
{
Btree * Treeheader;
printf ("Binary Tree creation data result: \ n");
Treeheader = Createbtree (treevalue1,node_count1-1);
Treeheader = Createbtree (treevalue2,node_count2-1);
if (treeheader==0)
{
printf ("Binary Tree creation failed!") \ n ");
return (0);
}
Else
{
printf ("\ n two cross-tree sequence traversal result: \ n");
FirstOrderAccess1 (Treeheader);
printf ("\ n two-forked tree sequence traversal result: \ n");
Middleorderaccess (Treeheader);
printf ("\ n two fork tree sequence traversal result: \ n");
Lastorderaccess (Treeheader);
printf ("\ n" all leaf nodes \ n two-tree);
Printleafnode (Treeheader);
Swapchildnode (Treeheader);
printf ("\ n two fork-tree swap results for children: \ n");
Middleorderaccess (Treeheader);
printf ("\ n program run complete!\n");
return 0;
}
}

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.