[Data structure] binary tree

Source: Internet
Author: User

(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
    1. typedef struct bitnode{
    2. Elemtype data;
    3. struct Bitnode *lchild,rchild;
    4. }bitnode,*bitree;


Build a binary tree so that it has only the head node.

[CPP]View Plaincopy
    1. int Initiate (Bitree *bt)
    2. {
    3. if ((bt= (Bitnode *) malloc (sizeof (Bitnode)) ==null)
    4. return 0;
    5. bt->lchild=null;
    6. bt->rchild=null;
    7. return 1;
    8. }


(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
    1. void preorder (Bitree BT)
    2. {/* First sequential traversal of binary tree bt*/
    3. if (bt==null) return; / * End condition for recursive call * /
    4. Visite (Bt->data); / * Access the data field of the node * /
    5. Preorder (Bt->lchild); / * Recursively traverse BT's left sub-tree First order recursion * /
    6. Preorder (Bt->rchild); / * Recursively traverse the right subtree of BT * /
    7. }

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
  1. void Nrpreorder (Bitree bt)
  2. {/* non-recursive sequential traversal of binary tree * /
  3. Bitree stack[maxnode],p;
  4. int top;
  5. if (bt==null) return;
  6. Top=0;
  7. P=BT;
  8. While (!) ( p==null&&top==0))
  9. {while (p!=null)
  10. {visite (p->data); / * Access the data field of the node * /
  11. if (top<maxnode-1)/ * Press the current pointer p * /
  12. {stack[top]=p;
  13. top++;
  14. }
  15. else {printf ("stack Overflow");
  16. return;
  17. }
  18. p=p->lchild;/ * Pointer pointing to P's left child * /
  19. }
  20. if (top<=0) return; / * End of Stack empty * /
  21. else{top--;
  22. P=stack[top]; / * Eject the top element of the stack from the stack * /
  23. p=p->rchild; / * Pointer points to P's right child node * /
  24. }
  25. }
  26. }


Data structure-two fork tree

[Data structure] binary 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.