Data Structure: Tree and binary tree

Source: Internet
Author: User

Most of the notes are taken from "big talk Data Structure" and "Data Structure".

I. Definitions

1. A tree is a finite set of N nodes. If n = 0, it is called an empty tree. In any non-empty tree: (1) there is only one specific node called the root node; (2) When n> 1, other nodes can be divided into M finite sets that do not conflict with each other, T1, T2 ,...... , TM, where each set itself is a tree, and is called a root subtree.
2. The number of Subtrees owned by a node is called the degree of the node, and the degree of the tree is the maximum degree of the node. A node whose degree is 0 is called a leaf node or a terminal node, and a node whose degree is not 0 is called a branch node, an internal node, or a non-interrupt node.
3. The root of the Child tree of a node is the child of a node. The node is called the child's parent. The same parent is a brother, and the ancestor is the parent until the root node. The number of layers in a tree is called the depth or height of the tree.
4. The forest is a set of trees with different m classes. For each node in the tree, the set of its Subtrees is the forest.
5. Tree Storage Structure: parental notation, child notation, and child sibling notation. The child sibling notation sets two pointers pointing to the first child of the node and the right brother of the node respectively. The biggest benefit is that a complex tree is converted into a binary tree.
6. Generally, hierarchical classification schemes can be represented by hierarchies, that is, tree structures.

Ii. Definition of the Cross Tree:
1. A binary tree is a finite set of N nodes. The set can be empty or empty ), it can also be composed of a root node and a binary tree of two left or right subtree that are mutually exclusive and called the root node respectively.
2. Each node can have only two Subtrees at most, that is, the degree is not greater than 2, and the left and right Subtrees are ordered.
3. Special Binary Tree: Oblique tree with only the left or right subtree; full binary tree with all branch nodes full; full binary tree with a back cut from full binary tree.

III:
1. The sequential storage structure is complicated to implement the one-to-many relational structure of the common tree, but the binary tree is a special tree, so it can also be implemented using the sequential structure, however, the ordered structure is generally used only for full Binary Trees, otherwise it will cause a waste of space.
2. Each node of a binary tree has a maximum of two children. Therefore, it is natural to design a data domain and two pointer domains for it, such a linked list is called a binary linked list (only one pointer field is called a single-chain table ). If you add a pointer domain pointing to the parent, it is called a trigger linked list.
3. binary tree traversal methods include pre-order traversal, middle-order traversal, post-order traversal, and hierarchical traversal.
Forward traversal: first access the node, then traverse the left subtree in the forward order, and finally traverse the right subtree.
Central traversal: Intermediate Access Node
Post-order traversal: The last Access Node


Example 1: Create and traverse a binary tree

# Include "stdio. H "# include" malloc. H "// Binary Tree node typedef struct btnode {char data; struct btnode * lchild, * rchild;} btnode;/** function: Create a binary chain table in sequence * input: no * output: Root Node of the binary linked list */btnode * Create () {btnode * node; // node pointer char CH = ''; scanf (" % C ", & Ch); If (CH = '#') // use # To replace virtual node = NULL; else {node = (btnode *) malloc (sizeof (btnode); // allocate a new node-> DATA = CH; // store data node-> lchild = create (); // recursively create the left subtree node-> rchild = create (); // recursively create the right subtree node} return node; // return the root node}/** function: traverse Binary Tree in sequence * input: Root Node of binary linked list * output: none */void preordertraverse (btnode * node) {If (node = NULL) // If the node is a virtual node, return; printf ("% C", node-> data); // print the node data preordertraverse (node-> lchild ); // recursively access the left subtree node preordertraverse (node-> rchild); // recursively access the right subtree node}/** function: traverse a binary tree in the middle order * input: output of the root node * of the binary linked list: none */void inordertraverse (btnode * node) {If (node = NULL) return; inordertraverse (node-> lchild ); printf ("% C", node-> data); inordertraverse (node-> rchild);}/** function: traverse Binary Tree in descending order * input: output of the root node * of the binary linked list: none */void postordertraverse (btnode * node) {If (node = NULL) return; postordertraverse (node-> lchild ); postordertraverse (node-> rchild); printf ("% C", node-> data);} void main () {btnode * root = create (); // create a binary linked list and return the root node printf ("\ n"); printf ("first-order traversal:"); // first-order traversal of the Binary Tree preordertraverse (Root ); printf ("\ n"); printf ("sequential traversal :");//.. inordertraverse (Root); printf ("\ n"); printf ("post-order traversal :");//.. postordertraverse (Root); printf ("\ n ");}

Input: ABC # D # e # F ##

Its binary tree structure shows:




Print result:



The main knowledge about trees and Binary Trees is the properties and clues of Binary Trees. Binary Trees: point the pointer domain of a binary tree that originally points to a virtual node to the precursor node or the successor node, form a two-way linked list, tree and binary tree. The conversion between forests and Binary Trees also involves tree and forest traversal.



Data Structure: Tree and 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.