Data Structure BASICS (16) and data structure basics 16

Source: Internet
Author: User

Data Structure BASICS (16) and data structure basics 16
Basic Terms of the tree

1. node: {Data Element + several branches pointing to the subtree}

2. node degree: number of branches (number of sub-trees)

3. Tree degree: the maximum degree of all nodes in the tree

4. leaf node: zero degree Node

5. branch nodes: nodes with a degree greater than zero (including the root and intermediate nodes)

6. (from the root to the node) path: consists of the branches and nodes from the root to the node;

7. node hierarchy: If the root node hierarchy is 1, the root child is layer 2nd. If a node is at Layer L, the root of its child tree is at Layer L + 1.

8. Tree depth: the maximum layer of the leaf node in the tree;

 

Binary Tree

A binary tree is either an empty tree or a root node with two binary trees called left and right trees that do not interwork with each other. (The maximum tree degree is 2)



Important Properties of Binary Trees:

Property 1: at most 2 ^ (I-1) nodes (I ≥1) on the I layer of the binary tree );

Property 2: A binary tree with a depth of k contains at most (2 ^ k)-1 node (k ≥ 1 );

Property 3: For any binary tree, if it contains n0 leaf nodes (0-degree nodes) and n2 nodes with 2 degrees, there must be a relationship: n0 = n2 + 1.

 

Two types of special Binary Trees:

Full Binary Tree: Refers to a binary tree with a depth of k and containing (2 ^ k)-1 nodes.

Full Binary Tree: The n nodes contained in the tree correspond one to one with nodes numbered 1 to n in the full binary tree. (The numbering rule is from top to bottom, from left to right. As shown in)

Full Binary Tree features:

1. Leaf nodes appear on the last two layers

2. For any node, if the maximum hierarchy of the child of the right branch is L, the maximum hierarchy of the child of the left branch is L or L + 1;

 

Property 4: the depth of the Complete Binary Tree with n nodes is [logn] (rounded down) + 1.

Property 5: If a Complete Binary Tree Containing n nodes is numbered 1 to n from top to bottom and from left to right, any node numbered I in the Complete Binary Tree is:

(1) If I = 1, the node is the root of the binary tree and has no parent. Otherwise, the node numbered [I/2] (rounded down) is the parent node;
(2) If 2i> n, the node has no left child. Otherwise, the node number 2i is the left child node;
(3) If 2i + 1> n, the node has no right child node. Otherwise, the node numbered 2i + 1 is the right child node.

 

Implementation of Binary Tree chain Storage

Note:

Because this blog is only used to demonstrate the theory of Binary trees, the encapsulation and availability of Code are not ideal. However, in actual application, it is basically impossible to directly use binary trees like this, therefore, he was not optimized. I would like to say sorry here;

 

Binary Tree node Construction

Template <typename Type> class TreeNode {friend class BinaryTree <Type>; // It is defined as publicpublic for demonstration purposes only: treeNode (const Type & _ data = Type (), TreeNode * _ left = NULL, TreeNode * _ right = NULL): data (_ data), leftChild (_ left ), rightChild (_ right) {} Type data; TreeNode * leftChild; TreeNode * rightChild ;};

Binary Tree Structure:

Template <typename Type> class BinaryTree {public: // Binary Tree operation BinaryTree (): root (NULL) {} bool isEmpty () const {return root = NULL ;} // first traverse void preOrder () const {return preOrder (root);} // In the middle order traverse void inOrder () const {return inOrder (root );} // void postOrder () const {return postOrder (root);} // traverse void levelOrder () const; private: void preOrder (const TreeNode <Type> * rootNode) const; void inOrder (const TreeNode <Type> * rootNode) const; void postOrder (const TreeNode <Type> * rootNode) const; void visit (const TreeNode <Type> * node) const; // because this is only for demonstration, it is defined as publicpublic: TreeNode <Type> * root ;};

First (Root) order traversal algorithm:

1. If the binary tree is empty, it is directly returned;

2. Otherwise

(1) access the root node (visit );

(2) traverse the left subtree in sequence;

(3) traverse the right subtree in sequence;

// Implement template <typename Type> void BinaryTree <Type>: preOrder (const TreeNode <Type> * subTree) const {if (subTree! = NULL) {visit (subTree); preOrder (subTree-> leftChild); preOrder (subTree-> rightChild );}}

The traversal algorithm in the middle (Root) Order:

1. If the binary tree is an empty tree, the operation is null;

2. Otherwise

(1) traverse the left subtree in the middle order;

(2) access the root node;

(3) traverse the right subtree in ascending order.

// Implement template <typename Type> void BinaryTree <Type>: inOrder (const TreeNode <Type> * subTree) const {if (subTree! = NULL) {inOrder (subTree-> leftChild); visit (subTree); inOrder (subTree-> rightChild );}}

The following (Root) traversal algorithm:

1. If the binary tree is an empty tree, the operation is null;

2. Otherwise

(1) traverse the left subtree in descending order;

(2) traverse the right subtree in descending order;

(3) Access the root node.

// Implement template <typename Type> void BinaryTree <Type>: postOrder (const TreeNode <Type> * subTree) const {if (subTree! = NULL) {postOrder (subTree-> leftChild); postOrder (subTree-> rightChild); visit (subTree );}}

Hierarchical Traversal Algorithms and visit operations:

template <typename Type>void BinaryTree<Type>::levelOrder() const{    std::queue< TreeNode<Type>* > queue;    queue.push(root);    while (!queue.empty())    {        TreeNode<Type> *currentNode = queue.front();        queue.pop();        visit(currentNode);        if (currentNode->leftChild != NULL)            queue.push(currentNode->leftChild);        if (currentNode->rightChild != NULL)            queue.push(currentNode->rightChild);    }}
template <typename Type>void BinaryTree<Type>::visit(const TreeNode<Type> *currentNode) const{    cout << currentNode->data << ' ';}

Binary Tree Construction and Application Example

Construct a binary tree as follows:

// The Code is as follows: int main () {BinaryTree <char> tree; TreeNode <char> addition ('+'), subtraction ('-'), multiplies ('*'), divides ('/'); TreeNode <char> a ('A'), B ('B'), c ('C'), d ('D '), e ('E'); tree. root = & addition; addition. leftChild = & subtraction; addition. rightChild = & e; subtraction. leftChild = & multiplies; subtraction. rightChild = & d; multiplies. leftChild = ipvs; multiplies. rightChild = & c; divides. leftChild = & a; divides. rightChild = & B; cout <"preOrder:"; tree. preOrder (); cout <endl; cout <"inOrder:"; tree. inOrder (); cout <endl; cout <"postOrder:"; tree. postOrder (); cout <endl; cout <"level Order"; tree. levelOrder (); cout <endl; return 0 ;}

Application Example of Traversal Algorithm

1. count the number of leaf nodes in a binary tree (first traversal)

2. Calculate the depth of a binary tree (post-order traversal)

3. Copy a binary tree (post-order traversal)

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.