Data structure-storage structure of two fork tree

Source: Internet
Author: User

Sequential storage structure

The type definition of a binary tree storage structure:

#defineMAX_SIZE  typedeftelemtypesqbitree[MAX_SIZE];

The data elements of a complete binary tree are stored in a contiguous set of storage units, "top-down, from left to right".
The node element numbered I on a complete binary tree is stored in the component with the subscript value of i-1 for a one-dimensional array, as shown in 6-6 (c).
For a typical two-fork tree, each node is stored in a one-dimensional array, as opposed to a node on a fully binary tree.

Chained storage structure
设计不同的结点结构可构成不同的链式存储结构。

(1) Types of nodes and their definitions
① binary linked list node. There are three fields: one data field and two pointer fields pointing to the left and right sub-nodes respectively.

typedefstruct BTNode{  ElemType  data ;struct BTNode  

② three-prong linked list node. Add a pointer field to the parent node of the node, except for the three domains of the binary list

typedefstruct BTNode_3{  ElemType  data ;struct BTNode_3  
Traversing a binary tree (traversing binary trees)

Traversing binary trees (traversing binary tree): Refers to all nodes in the binary tree in the order specified (regular), so that each node is accessed once and accessed only once.
Access: Refers to the node to do some sort of processing. such as: output information, modify the value of the node and so on.
Order (Regular): Two The tree is a nonlinear structure, each node may have left and right two subtrees trees, so after accessing a node, the next visited node faces different choices. Therefore, it is necessary to look for a sequence (regular) so that the nodes on the two-fork tree can be arranged on a linear queue, thus facilitating traversal.
The basic composition of binary tree: root node, Zuozi, right subtree. If you can traverse these three parts in turn, you are traversing a two-fork tree.

若以L、D、R分别表示遍历左子树、遍历根结点和遍历右子树,则有六种遍历方案:DLR、LDR、LRD、DRL、RDL、RLD。   若规定先左后右,则只有前三种情况三种情况,分别是:

dlr--first (root) sequence traversal.
ldr--(root) sequence traversal.
lrd--after (root) sequence traversal.
Known binary tree, write sequence, sequence, order sequence
A sequence of first and middle sequence is known, and a two-fork tree is determined
A sequence of sequential and sequential sequences is known to determine a two-fork tree

Traversal algorithm

For the traversal of binary tree, recursive traversal algorithm and non-recursive traversal algorithm are discussed respectively.
Recursive traversal algorithms have a very clear structure, but beginners are often difficult to accept or suspect, dare not to use. In fact, recursive algorithms are controlled by the system by using stacks.
The control in the non-recursive algorithm is implemented by the designer to define and use the stack.

First-order traversal of binary tree

1 Recursive algorithm
The recursive definition of the algorithm is:
If the binary tree is empty, the traversal ends;
⑴ Access root node;
⑵ First Order traversal Zuozi (recursive Call of the algorithm);
⑶ traverses the right subtree (recursive invocation of the algorithm).

 First order traversal recursive algorithm void  Preordertraverse (Btnode *  T) {if  (T==  null ) return ;       Visit (T->  data ); /* Access root node */ Preordertraverse (T->  lchild); //and then iterates through the left subtree  Preordertraverse (T->  rchild); //re-order traverse right subtree }      Description: The 1 , visit () function is the data domain that accesses the node, and its requirements depend on the specific problem, and can be the simplest printout. 2 , the tree uses the binary linked list of the storage structure, with the pointer variable t to point to. 

2 Non-recursive algorithm
Set T is the pointer variable that points to the binary root node, and the non-recursive algorithm is:
If the binary tree is empty, then return; otherwise, make p=t;
⑴ accesses the node to which P is pointing;
⑵q=p->rchild, if q is not empty, then q into the stack;
⑶p=p->lchild, if p is not empty, turn (1), otherwise turn (4);
⑷ the stack to P, turn (1) until the stack is empty.
Algorithm implementation:

#define Max_stack_sizevoid Preordertraverse (Btnode*t) {Btnode*stack[Max_stack_size],*p=t,*q;inttop=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);}}

Data structure-storage structure of two fork tree

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.