Data Structure-binary tree storage structure

Source: Internet
Author: User

Data Structure-binary tree storage structure
Sequential Storage Structure

Binary Tree Storage Structure Type Definition:

#define MAX_SIZE  100 typedef telemtype sqbitree[MAX_SIZE];

Store Data Elements of a Complete Binary Tree from top to bottom and from left to right with a set of sequential storage units.
For node elements that are compiled on a fully binary tree as I, they are stored in the components of the one-dimensional array whose lower value is the I-1, as shown in 6-6 (c.
For a common Binary Tree, compare each node with the node on the Complete Binary Tree and store it in a one-dimensional array,

Chained Storage Structure
Different Node structures can be designed to form different chain storage structures.

(1) node types and definitions
① Binary linked list node. There are three domains: one data domain, and two pointer domains pointing to the left and right sub-nodes respectively.

typedef struct BTNode{  ElemType  data ;struct BTNode  *Lchild , *Rchild ;}BTNode ; 

② The three linked list node. In addition to the three outer fields of the binary linked list, a pointer field is added to point to the parent node of the node.

typedef struct BTNode_3{ ElemType data ;struct BTNode_3 *Lchild , *Rchild , *parent ;}BTNode_3 ; 
Traverse Binary Tree)

Traverse Binary Tree: accesses all nodes in the Binary Tree in the specified Order (regular), so that each node is accessed once and only once.
Access: it refers to processing a node. For example, output information and modify node values.
Order (rule): a binary tree is a non-linear structure. Each node can have two left and right Subtrees. Therefore, after accessing a node, the next accessed node faces different choices. Therefore, we need to find an order (rule) so that the nodes on the binary tree can be arranged on a linear queue for convenient traversal.
Basic components of a binary tree: root node, left subtree, and right subtree. If the three parts can be traversed in sequence, the binary tree is traversed.

If L, D, and R are used to traverse the left subtree, the root node, and the right subtree, there are six traversal schemes: DLR, LDR, LRD, DRL, RDL, and RLD. If left and right are specified, only the first three cases are described as follows:

DLR -- first (Root) traversal.
LDR -- traverse in the (Root) Order.
LRD -- Post (Root) sequential traversal.
Known Binary Tree, write the first sequence, middle sequence, and back Sequence
Determine the binary tree by knowing the first and middle order sequences
Determine the binary tree based on the known post-order sequence and Middle-Order Sequence

Traversal Algorithm

For binary tree traversal, the recursive and non-recursive Traversal Algorithms are discussed respectively.
Recursive Traversal Algorithms have a very clear structure, but beginners are often unacceptable or skeptical and do not dare to use them. In fact, recursive algorithms are controlled by the system by using stacks.
The control in non-recursive algorithms is defined by the designer and implemented using stacks.

Traverse Binary Trees in sequence

1 Recursive Algorithm
The recursive definition of an algorithm is:
If the binary tree is empty, the traversal ends. Otherwise
(1) access the root node;
(2) traverse the left subtree in sequence (this algorithm is called recursively );
(3) traverse the right subtree in sequence (this algorithm is called recursively ).

Recursive Algorithm void PreorderTraverse (BTNode * T) {if (T = NULL) return; visit (T-> data ); /* access the root node */PreorderTraverse (T-> Lchild); // first traverse the left subtree PreorderTraverse (T-> Rchild ); // traverse the right subtree first} Description: 1. The visit () function is the data domain of the Access Node. Its requirements depend on the specific problem. It can be the simplest print output. 2. The tree adopts the storage structure of the binary linked list and points it with the pointer variable T.

2 Non-Recursive Algorithms
If T is a pointer variable pointing to the root node of a binary tree, the non-recursive algorithm is:
If the binary tree is empty, return; otherwise, p = T;
(1) access the node pointed to by p;
(2) q = p-> Rchild. If q is not empty, q is pushed to the stack;
(3) p = p-> Lchild. If p is not empty, convert (1). Otherwise, convert (4 );
(4) Roll Back to p and turn to (1) until the stack is empty.
Algorithm Implementation:

#define MAX_STACK_SIZE 50void PreorderTraverse( BTNode *T){ BTNode *Stack[MAX_STACK_SIZE ] ,*p=T, *q ;int top=0 ;if (T==NULL) printf(“ Binary Tree is Empty!\n”) ;else { do { visit( p-> data ) ; q=p->Rchild ; if ( q!=NULL ) stack[top++]=q ; p=p->Lchild ; if (p==NULL&& top!=0) {top-- ;p=stack[top] ; } } while (p!=NULL) ;}}

Related Article

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.