Binary Tree creation and Traversal Algorithms

Source: Internet
Author: User

 

// Binary Tree header file processing
// Includes the structure definition of a binary tree, the creation of a binary tree, And the traversal algorithm (recursive and 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 (display number)
# Define showchar 1
// Binary Tree Structure
Struct btnode
{
Int data;
Btnode * rchild;
Btnode * lchild;
};
// Non-recursive Binary Tree
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] =
{}, {6 }};
Int treenoden1 [nsize1] [2] =
{}, {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,-, 12, 14, 16 };
// Char * prestr = "abdecfg ";
// Char * midstr = "dbeacgf ";
/*
Binary Tree creation function dcreatebranchtree1 () <recursive algorithm>

Parameter description:
Int
Array []: an array of Binary Tree node data domains
Int
I: Serial number of the current node

Int
N: Number of Binary Tree nodes

Return Value:
Dcreatebranchtree1 =
Create a root node pointer for a binary tree
Note:
Root Node = array [(I + J)/2];
Left subnode =
[Array [I], array [(I + J) 2-1]
Right subnode =
[Array [(I + J)/2 + 1, array [J]
*/
Btnode * dcreatebranchtree1 (char array [], int I, int N)
{
Btnode * P;/* Binary 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 []: an array of Binary Tree node data domains
Int
I: Serial number of the current node

Int
N: Number of Binary Tree nodes

Return Value:
Dcreatebranchtree2 =
Create a root node pointer for a binary tree
Note:
Root Node = array [(I + J)/2];
Left subnode =
[Array [I], array [(I + J) 2-1]
Right subnode =
[Array [(I + J)/2 + 1, array [J]
*/
Btnode * dcreatebranchtree2 (char array [], int I, Int J)
{
Btnode * P;/* Binary Tree node */
If (I> 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>

Before a known binary tree, traverse the sequence string in the middle order to construct the corresponding binary tree

<Programming philosophy>:
First, the first element in the first traversal sequence is the root node of the binary tree, and then

To find this node in the ordinal traversal sequence, the previous node must be

Its left child node will be its right child node in the future;
Then, in the middle-order traversal sequence, the left and right subtree of the root node are

Determine the root node of the sub-tree by the first character of the pre-order traversal sequence of the sub-tree.

Determine the left and right subtree of the root nodes in the traversal sequence.

Node;
And so on, determine all the nodes of the Binary Tree, and construct the binary tree.

Parameter description:
Char
* Pre: preorder traversal Sequence
Char
* Mid: ordinal traversal Sequence
Int
N: number of nodes in the traversal Sequence

Return Value:
Dcreatebranchtree3 =
Create a root node pointer for a binary tree
*/
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;/* search for the root node in the middle order traversal sequence */

Left = T-
Mid;/* number of nodes in the left subtree */
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 []: an array of Binary Tree node data domains
Int
N: Number of Binary Tree nodes

Return Value:
Createbranchtree =
Create a root node pointer for a binary tree
*/
Btnode * createbranchtree (INT array [] [2], int N)
{
Btnode * head, * P;
Btnode
* Nodeaddr [maxnode]; // temporary buffer of the node address
Int I, norder, rorder;
Head = NULL;
Printf ("binary tree raw data <new sequence>:/t ");

For (I = 1; I <= N; I ++)
{
P = (btnode
*) Malloc (sizeof (btnode ));
If (P = NULL)
{
Printf ("/n memory overflow when creating a node! /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: mounted to your 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 forward traversal function dpre_order_access () <recursive algorithm>

Parameter description:
Btnode
* Head: the root node pointer of a binary tree

*/
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);/* recursively traverse the left subtree */

Dpre_order_access (Head-> rchild);/* recursively traverse the right subtree */

}
}
/*
Binary Tree sequential traversal function dmid_order_access () <recursive algorithm>

Parameter description:
Btnode
* Head: the root node pointer of a binary tree

*/
Void dmid_order_access (btnode * head)
{
If (Head! = NULL)
{
Dmid_order_access (Head-> lchild);/* recursively traverse the left subtree */

If (showchar)
Printf ("% C
", Head-> data );
Else
Printf ("% d
", Head-> data );
Dmid_order_access (Head-> rchild);/* recursively traverse the right subtree */

}
}
/*
Binary Tree post-order traversal function dlast_order_access () <recursive algorithm>

Parameter description:
Btnode
* Head: the root node pointer of a binary tree

*/
Void dlast_order_access (btnode * head)
{
If (Head! = NULL)
{
Dlast_order_access (Head-> lchild);/* recursively traverse the left subtree */

Dlast_order_access (Head-> rchild);/* recursively traverse the right subtree */

If (showchar)
Printf ("% C
", Head-> data );
Else
Printf ("% d
", Head-> data );
}
}
// -------------------------------- Recursive part ------------------------------

// -------------------------------- Non-recursive part ------------------------------

/*
Pre_order_access () <Non-recursive algorithm>

Parameter description:
Btnode
* Head: the root node pointer of a binary tree

*/
Void pre_order_access (btnode * head)
{
Btnode * PT;
Abtstack * ps, * top;
PT = head;
Top = NULL;
Printf ("/n pre-sequential traversal result of a binary tree <Non-recursive>:/t ");

While (PT! = NULL
| Top! = NULL)/* the binary tree has not been traversed, or the stack is not empty */

{
While (PT! = NULL)
{
If (showchar)

Printf ("% C
", Pt-> data);/* access the root node */

Else
Printf ("% d
", Pt-> data);/* access the root node */

PS =
(Abtstack
*) Malloc (sizeof (abtstack);/* Add the root node to the stack */

PS-> ptree
= Pt;
PS-> link =
Top;
Top =
PS;
PT =
Pt-> lchild;/* traverse the right subtree of the node and stack the nodes in sequence */

}
If (top! = NULL)
{
PT =
Top-> ptree;/* top stack node output stack */
PS =
Top;
Top =
Top-> link;
Free (PS);/* release the space of the top node of the stack */

PT =
Pt-> rchild;/* traverse the right subtree of the node */
}
}
}
/*
Binary Tree central order traversal function mid_order_access () <Non-recursive algorithm>

Parameter description:
Btnode
* Head: the root node pointer of a binary tree
*/
Void mid_order_access (btnode * head)
{
Btnode * PT;
Abtstack * ps, * top;
Int counter = 1;
PT = head;
Top = NULL;
Printf ("/n binary tree's ordinal traversal result <Non-recursive>:/t ");

While (PT! = NULL
| Top! = NULL)/* the binary tree has not been traversed, or the stack is not empty */

{
While (PT! = NULL)
{

PS =
(Abtstack
*) Malloc (sizeof (abtstack);/* Add the root node to the stack */
PS-> ptree
= Pt;
PS-> link =
Top;
Top =
PS;
PT =
Pt-> lchild;/* traverse the right subtree of the node and stack the nodes in sequence */

}
If (top! = NULL)
{
PT =
Top-> ptree;/* top stack node output stack */
PS =
Top;
Top =
Top-> link;
Free (PS);/* release the space of the top node of the stack */

If (showchar)

Printf ("% C
", Pt-> data);/* access the root node */
Else
Printf ("% d
", Pt-> data);/* access the root node */
PT =
Pt-> rchild;/* traverse the right subtree of the node */
}
}
}
/*
Binary Tree post-order traversal function last_order_access () <Non-recursive algorithm>

Parameter description:
Btnode
* Head: the root node pointer of a binary tree

*/
Void last_order_access (btnode * head)
{
Btnode * PT;
Abtstack * ps, * top;
Int counter = 1;
PT = head;
Top = NULL;
Printf ("/n post-sequential traversal result of a binary tree <Non-recursive>:/t ");

While (PT! = NULL
| Top! = NULL)/* the binary tree has not been traversed, or the stack is not empty */

{
While (PT! = NULL)
{

PS =
(Abtstack
*) Malloc (sizeof (abtstack);/* Add the root node to the stack */
PS-> ptree
= Pt;
PS-> link =
Top;
Top =
PS;
PT =
Pt-> lchild;/* traverse the right subtree of the node and stack the nodes in sequence */

}
If (top! = NULL)
{
PT =
Top-> ptree;/* top stack node output stack */
PS =
Top;
Top =
Top-> link;
Free (PS);/* release the space of the top node of the stack */

Printf ("% C
", Pt-> data);/* access the root node */
PT =
Pt-> rchild;/* traverse the right subtree of the node */
}
}
}
/*
Static query function static_search_stree () <Non-recursive algorithm>

Parameter description:
Btnode
* Head: the root node pointer of the Binary Search Tree.
Int
Key: Find the key code
Return Value:
Static_search_stree =
Node pointer with 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 domains = % 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 the right subtree */
}
Return (null);/* No search */
}
/*
Dynamic_search_stree () <Non-recursive algorithm>

Parameter description:
Btnode
* Head: the root node pointer of the Binary Search Tree.

Btnode
** Parent: pointer to the parent node pointer of the node whose key value is key
Btnode
** Head: pointer (found) or null (not found) of the node pointer whose key value is key)

Int
Key: Find the key code

Note:
* Parent = NULL and * P =
Null not found (Binary Tree is empty)
* Parent = NULL and * P! =
Locate null (locate root node)
* Parent! = NULL and * P =
Null not found (leaf node) <the node can be inserted after the parent>

* Parent! = NULL and * P! =
NULL (middle layer 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;/* use the current node as the parent node to continue searching */
If (* P)-> DATA>
Key)
* P =
(* P)-> lchild;/* continue searching along the left subtree */
Else
* P =
(* P)-> rchild;/* continue searching along the right subtree */
}
}
/*
Insert_node_stree (), the insert node function of the Binary Search Tree, <Non-recursive algorithm>

Parameter description:
Btnode
* Head: the root node pointer of the Binary Search Tree.
Int
Key: Find the key code
Return Value:
Insert_node_stree =
1 insert successful
Insert_node_stree =
0 insertion failed (the 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);/* the node already exists in the tree */

Nnode = (btnode
*) Malloc (sizeof (btnode);/* Create a node */
Nnode-> DATA = key;
Nnode-> lchild = nnode-> rchild = NULL;
If (P = NULL)
Head =
P;/* The original tree is empty, and the new node is the search tree */
Else
{
If (p-> DATA> key)
P-> lchild
= Nnode;/* act as the left child node */
Else
P-> rchild
= Nnode;/* as the right child node */
}
Return (1);/* Insert successful */
}
/*
Insert a batch of node functions into the Binary Search Tree: insert_batch_node_stree () <Non-recursive algorithm>

Parameter description:
Btnode
* Head: the root node pointer of the Binary Search Tree.
Int
Array []: an array of inserted data fields
Int
N: Number of inserted nodes

*/
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 ("/n insertion failed <the node with the 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.