(1) The concept of binary tree
Binary tree is a set of finite elements that are either empty or consist of an element called root (root) and two disjoint, two-tree trees that are called Saozi right subtrees respectively. When the collection is empty, the two fork tree is called an empty binary tree.
Full two fork tree: In a binary tree, if all branch nodes have Saozi right subtree, and all leaf nodes are on the same layer, such a binary tree is called a full two-fork tree.
Complete binary tree: A two-prong tree with n nodes at a depth of k, the nodes in the tree are numbered from top to bottom, left to right, and if the nodes numbered I (1≤i≤n) are in the same position in the binary tree as the nodes numbered I in the two-fork tree, the binary tree is called a complete binary tree.
(2) Basic operation and storage of binary tree
A binary tree with a two-fork list storage representation can be described as
[CPP]View Plaincopy
- typedef struct bitnode{
- Elemtype data;
- struct Bitnode *lchild,rchild;
- }bitnode,*bitree;
Build a binary tree so that it has only the head node.
[CPP]View Plaincopy
- int Initiate (Bitree *bt)
- {
- if ((bt= (Bitnode *) malloc (sizeof (Bitnode)) ==null)
- return 0;
- bt->lchild=null;
- bt->rchild=null;
- return 1;
- }
(3) Traversal of binary tree
The traversal of a binary tree means that each node in a binary tree is accessed in some order so that each node is accessed once and accessed only once.
First-order Traversal:
(1) Access to the root node;
(2) The first sequence traverses the left subtree of the root node;
(3) The right sub-tree of the root node is traversed in the first order.
The recursive algorithm of the first order traversal binary tree is as follows:
[CPP]View Plaincopy
- void preorder (Bitree BT)
- {/* First sequential traversal of binary tree bt*/
- if (bt==null) return; / * End condition for recursive call * /
- Visite (Bt->data); / * Access the data field of the node * /
- Preorder (Bt->lchild); / * Recursively traverse BT's left sub-tree First order recursion * /
- Preorder (Bt->rchild); / * Recursively traverse the right subtree of BT * /
- }
Middle sequence traversal: First left subtree, then root, then right sub-tree
Post-the-loop traversal: First walk the tree left subtree, then right subtree, the last root
Non-recursive implementation of first order traversal
Can be achieved by using the last-in-first-out feature of the stack:
[CPP]View Plaincopy
- void Nrpreorder (Bitree bt)
- {/* non-recursive sequential traversal of binary tree * /
- Bitree stack[maxnode],p;
- int top;
- if (bt==null) return;
- Top=0;
- P=BT;
- While (!) ( p==null&&top==0))
- {while (p!=null)
- {visite (p->data); / * Access the data field of the node * /
- if (top<maxnode-1)/ * Press the current pointer p * /
- {stack[top]=p;
- top++;
- }
- else {printf ("stack Overflow");
- return;
- }
- p=p->lchild;/ * Pointer pointing to P's left child * /
- }
- if (top<=0) return; / * End of Stack empty * /
- else{top--;
- P=stack[top]; / * Eject the top element of the stack from the stack * /
- p=p->rchild; / * Pointer points to P's right child node * /
- }
- }
- }
Data structure-two fork tree
[Data structure] binary tree