Binary tree creation and traversal algorithm

Source: Internet
Author: User
Tags continue empty printf

Binary Tree Processing header file
Including two-fork tree structure definition, binary tree creation, traversal algorithm (recursive and non-recursive),
/*
Author: Cheng
Time: October 7, 2001 (18:49:38-20:00:00)
Content: Complete two fork tree creation, binary tree before, middle, subsequent traversal (recursive)
Time: October 7, 2001 (21:09:38-22:09:00)
Contents: Before the completion of the two-fork tree, the sequence traversal (not recursive)
Time: October 8, 2001 (10:09:38-11:29:00)
Content: Find out the static of the binary tree, dynamic lookup (non-recursive)
*/
#include "Stdlib.h"

#define Maxnode 20
#define ISIZE 8
#define NSIZE0 7
#define NSIZE1 8
#define NSIZE2 15
Showchar = 1 (display character) Showchar = 0 (show number)
#define SHOWCHAR 1
Binary tree structure body
struct Btnode
{
int data;
Btnode *rchild;
Btnode *lchild;
};
Non-recursive binary tree over stack
struct Abtstack
{
Btnode *ptree;
Abtstack *link;
};
Char Treenodes[nsize0] = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G '};
Char Prenode[nsize0] = {' A ', ' B ', ' D ', ' E ', ' C ', ' F ', ' G '};
Char Midnode[nsize0] = {' D ', ' B ', ' E ', ' A ', ' C ', ' G ', ' F '};
int treenoden0[nsize1][2] = {{0,0},{1,1},{2,2},{3,3},{4,4},{5,5},{6,6},{7,7}};
int treenoden1[nsize1][2] = {{0,0},{4,1},{2,2},{6,3},{1,4},{3,5},{5,6},{7,7}};
int treenode0[nsize1][2] = {' 0 ', 0},{' D ', 1},{' B ', 2},{' F ', 3},{' A ', 4},{' C ', 5},{' E ', 6},{' G ', 7}};
int treenode1[nsize1][2] = {' 0 ', 0},{' A ', 1},{' B ', 2},{' C ', 3},{' D ', 4},{' E ', 5},{' F ', 6},{' G ', 7}};
int treenode2[nsize2][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}};
int Insertnode[isize] = { -10,-8,-5,-1,0,12,14,16};
Char *prestr = "abdecfg";
Char *midstr = "DBEACGF";
/*
Binary Tree creation function dCreateBranchTree1 () < recursive algorithm >
Parameter description:
int array[]: Two Fork tree node data field array
int I: Ordinal of the current node
int N: two fork tree node number
return value:
DCreateBranchTree1 = new two-fork tree root node pointer
Note:
root node = array[(I+J)/2];
Zoozi node = [array[i],array[(I+J) 2-1]]
Right child node = [array[(I+J)/2+1,array[j]]
*/
Btnode *dcreatebranchtree1 (char array[],int i,int N)
{
Btnode *p; /* Two fork tree node * *
if (i>=n)
return (NULL);
p = (Btnode *) malloc (sizeof (Btnode));
P->data = Array[i];
P->lchild = DCreateBranchTree1 (array,2*i+1,n);
P->rchild = DCreateBranchTree1 (array,2*i+2,n);
return (p);
}
/*
Binary Tree creation function dCreateBranchTree2 () < recursive algorithm >
Parameter description:
int array[]: Two Fork tree node data field array
int I: Ordinal of the current node
int N: two fork tree node number
return value:
DCreateBranchTree2 = new two-fork tree root node pointer
Note:
root node = array[(I+J)/2];
Zoozi node = [array[i],array[(I+J) 2-1]]
Right child node = [array[(I+J)/2+1,array[j]]
*/
Btnode *dcreatebranchtree2 (char array[],int i,int j)
{
Btnode *p; /* Two fork tree node * *
if (I&GT;J)
return (NULL);
p = (Btnode *) malloc (sizeof (Btnode));
P->data = array[(i+j)/2];
P->lchild = DCreateBranchTree2 (Array,i, (i+j)/2-1);
P->rchild = DCreateBranchTree2 (array, (I+J)/2+1,j);
return (p);
}
/*
Binary Tree creation function dCreateBranchTree3 () < non-recursive algorithm >
The first, middle sequence traversal sequence string of the binary tree is known, and the corresponding two-fork tree is constructed.
< programming Ideas:
First, the first element in the first sequence traversal sequence is the root node of the two-fork tree, and then
, this node is found in the middle sequence traversal sequence, then the node before this node must be
Its left child node, the future will be its right child node;
Then, in the sequential traversal sequence, the Saozi right subtree of the root node is divided
The first character of the sequential traversal sequence of the corresponding subtree determines the root node of the subtree, and then the middle order
The location of the root node in the traversal sequence determines the Saozi right subtree that constitutes them.
of nodes;
By analogy, we determine all nodes of the two-fork tree and construct two-fork trees.
Parameter description:
Char *pre: Pre-sequence traversal sequence
Char *mid: Central sequence traversal sequence
int N: Number of nodes in the traversal sequence
return value:
DCreateBranchTree3 = new two-fork tree root node pointer
*/
Btnode *dcreatebranchtree3 (char *pre,char *mid,int N)
{
Btnode *p;
Char *t;
int left;
if (n<=0)
return (NULL);
p = (Btnode *) malloc (sizeof (Btnode));
P->data = *pre;
for (t=mid;t<mid+n;t++)
if (*t==*pre) break; /* Find the root node in the sequence traversal series * *
left = T-mid; /* Zuozi Number of nodes/*
P->lchild = DCreateBranchTree3 (pre+1,t,left);
P->rchild = DCreateBranchTree3 (pre+1+left,t+1,n-1-left);
return (p);
}
/*
Binary Tree creation function Createbranchtree () < non-recursive algorithm >
Parameter description:
int array[]: Two Fork tree node data field array
int N: two fork tree node number
return value:
Createbranchtree = new two-fork tree root node pointer
*/
Btnode *createbranchtree (int array[][2],int N)
{
Btnode *head,*p;
Btnode *nodeaddr[maxnode]; Node address temporary buffer
int i,norder,rorder;
head = NULL;
printf ("Binary Tree raw data < New Order >:\t");
for (i=1;i<=n;i++)
{
p = (Btnode *) malloc (sizeof (Btnode));
if (p==null)
{
printf ("\ nthe memory overflow when new node is created!") \ n ");
return (NULL);
}
Else
{
P->data = array[i][0];
P->lchild = P->rchild = NULL;
Norder = array[i][1];
Nodeaddr[norder] = p;
if (norder>1)
{
Rorder = NORDER/2; /* Non-root node: Hook on own parent node * *
if (norder% 2 = 0)
Nodeaddr[rorder]->lchild = p;
Else
Nodeaddr[rorder]->rchild = p;
}
Else
head = p; /* Root Node/*
if (Showchar)
printf ("%c", p->data);
Else
printf ("%d", p->data);
}
}
return (head);
}
------------------------------Recursive part------------------------------
/*
Binary tree sequence traversal function dpre_order_access () < recursive algorithm >
Parameter description:
Btnode *head: Two fork tree root node pointer
*/
void Dpre_order_access (Btnode *head)
{
if (head!=null)
{
if (Showchar)
printf ("%c", head->data);
Else
printf ("%d", head->data);
Dpre_order_access (Head->lchild); * Recursive traversal of the left subtree * *
Dpre_order_access (Head->rchild); * Recursive traverse right subtree/*
}
}
/*
Sequence traversal function dmid_order_access () < recursive algorithm in binary tree >
Parameter description:
Btnode *head: Two fork tree root node pointer
*/
void Dmid_order_access (Btnode *head)
{
if (head!=null)
{
Dmid_order_access (Head->lchild); * Recursive traversal of the left subtree * *
if (Showchar)
printf ("%c", head->data);
Else
printf ("%d", head->data);
Dmid_order_access (Head->rchild); * Recursive traverse right subtree/*
}
}
/*
Binary tree sequence traversal function dlast_order_access () < recursive algorithm >
Parameter description:
Btnode *head: Two fork tree root node pointer
*/
void Dlast_order_access (Btnode *head)
{
if (head!=null)
{
Dlast_order_access (Head->lchild); * Recursive traversal of the left subtree * *
Dlast_order_access (Head->rchild); * Recursive traverse right subtree/*
if (Showchar)
printf ("%c", head->data);
Else
printf ("%d", head->data);
}
}
------------------------------Recursive part------------------------------
------------------------------non-recursive part------------------------------
/*
Binary tree sequence traversal function pre_order_access () < non-recursive algorithm >
Parameter description:
Btnode *head: Two fork tree root node pointer
*/
void Pre_order_access (Btnode *head)
{
Btnode *pt;
Abtstack *ps,*top;
PT = head;
top = NULL;
printf ("\ n two-tree sequence traversal result < non-recursive >:\t");
while (Pt!=null | | Top!=null)/* Two fork tree not traversed, or stack not empty * *
{
while (Pt!=null)
{
if (Showchar)
printf ("%c", pt->data); /* Access root node * *
Else
printf ("%d", pt->data); /* Access root node * *
PS = (Abtstack *) malloc (sizeof (abtstack)); /* root node into the stack * *
Ps->ptree = pt;
Ps->link = top;
top = PS;
PT = pt->lchild; /* Traverse node right subtree, after the node in turn into the stack * *
}
if (top!=null)
{
PT = top->ptree; /* Stack top node out stack * *
PS = top;
top = top->link;
Free (PS); /* Free stack top node space * *
PT = pt->rchild; /* Traverse node right subtree * *
}
}
}
/*
Sequence traversal function mid_order_access () < non-recursive algorithm in binary tree >
Parameter description:
Btnode *head: Two fork tree root node pointer
*/
void Mid_order_access (Btnode *head)
{
Btnode *pt;
Abtstack *ps,*top;
int counter = 1;
PT = head;
top = NULL;
printf ("\ n two-tree sequence traversal results < non-recursive >:\t");
while (Pt!=null | | Top!=null)/* Two fork tree not traversed, or stack not empty * *
{
while (Pt!=null)
{
PS = (Abtstack *) malloc (sizeof (abtstack)); /* root node into the stack * *
Ps->ptree = pt;
Ps->link = top;
top = PS;
PT = pt->lchild; /* Traverse node right subtree, after the node in turn into the stack * *
}
if (top!=null)
{
PT = top->ptree; /* Stack top node out stack * *
PS = top;
top = top->link;
Free (PS); /* Free stack top node space * *
if (Showchar)
printf ("%c", pt->data); /* Access root node * *
Else
printf ("%d", pt->data); /* Access root node * *
PT = pt->rchild; /* Traverse node right subtree * *
}
}
}
/*
Binary tree sequence traversal function last_order_access () < non-recursive algorithm >
Parameter description:
Btnode *head: Two fork tree root node pointer
*/
void Last_order_access (Btnode *head)
{
Btnode *pt;
Abtstack *ps,*top;
int counter = 1;
PT = head;
top = NULL;
printf ("\ n two-tree sequence traversal results < non-recursive >:\t");
while (Pt!=null | | Top!=null)/* Two fork tree not traversed, or stack not empty * *
{
while (Pt!=null)
{
PS = (Abtstack *) malloc (sizeof (abtstack)); /* root node into the stack * *
Ps->ptree = pt;
Ps->link = top;
top = PS;
PT = pt->lchild; /* Traverse node right subtree, after the node in turn into the stack * *
}
if (top!=null)
{
PT = top->ptree; /* Stack top node out stack * *
PS = top;
top = top->link;
Free (PS); /* Free stack top node space * *
printf ("%c", pt->data); /* Access root node * *
PT = pt->rchild; /* Traverse node right subtree * *
}
}
}
/*
Binary lookup tree static lookup function Static_search_stree () < non-recursive algorithm >
Parameter description:
Btnode *head: Two fork The root node pointer of the lookup tree
int key: Finding key codes
return value:
Static_search_stree = node pointer for key value (found)
Static_search_stree = NULL (not found)
*/
Btnode *static_search_stree (Btnode *head,int key)
{
while (Head!=null)
{
if (Head->data = = key)
{
printf ("\ n data domain =%d\t address =%d\t\n", head->data,head);
return (head); * * Find * *
}
if (Head->data > key)
Head = head->lchild; /* Continue searching along the left subtree * *
Else
Head = head->rchild; /* Continue searching along right subtree * *
}
return (NULL); /* No Lookup * *
}
/*
Binary lookup tree Dynamic lookup function Dynamic_search_stree () < non-recursive algorithm >
Parameter description:
Btnode *head: Two fork The root node pointer of the lookup tree
Btnode **parent: Pointer to the parent node of a node with key value key
Btnode **head: Pointer to node pointer with key value (found) or null (not found)
int key: Finding key codes
Attention:
*parent = null and *p = NULL not found (binary tree is empty)
*parent = = null and *p!= null found (root node found)
*parent!= null and *p = null not Found (leaf node) < can insert node after parent >
*parent!= null and *p!= null found (middle-tier node)
*/
void Dynamic_search_stree (Btnode *head,btnode **parent,btnode **p,int key)
{
*parent = NULL;
*p = head;
while (*p!=null)
{
if ((*p)->data = = key)
Return * * Find * *
*parent = *p; /* Take the current node as the parent, continue to find * *
if ((*p)->data > key)
*p = (*p)->lchild; /* Continue searching along the left subtree * *
Else
*p = (*p)->rchild; /* Continue searching along right subtree * *
}
}
/*
Binary lookup Tree Insert node function insert_node_stree () < non-recursive algorithm >
Parameter description:
Btnode *head: Two fork The root node pointer of the lookup tree
int key: Finding key codes
return value:
Insert_node_stree = 1 Insert Succeeded
Insert_node_stree = 0 Insert failed (node already exists)
*/
int Insert_node_stree (Btnode *head,int key)
{
Btnode *p,*q,*nnode;
Dynamic_search_stree (Head,&p,&q,key);
if (q!=null)
return (0); /* node already exists in the tree * *
Nnode = (Btnode *) malloc (sizeof (Btnode)); /* NEW node * *
Nnode->data = key;
Nnode->lchild = Nnode->rchild = NULL;
if (p==null)
head = p; /* The original tree is empty, the new node is the lookup tree * *
Else
{
if (P->data > key)
P->lchild = Nnode; /* As the Left child node * *
Else
P->rchild = Nnode; /* As the right child node * *
}
return (1); /* Insert Success * *
}
/*
Binary lookup tree Insert a batch of node functions Insert_batch_node_stree () < non-recursive algorithm >
Parameter description:
Btnode *head: Two fork The root node pointer of the lookup tree
int array[]: An array of inserted data fields
int N: Number of nodes inserted
*/
void Insert_batch_node_stree (Btnode *head,int array[],int N)
{
int i;
for (i=0;i<n;i++)
{
if (! Insert_node_stree (Head,array[i])
printf ("\ nthe node with insert failure < key value%d already exists >! \ n ", Array[i]);
}
}
------------------------------non-recursive part------------------------------

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.