Simple chain binary tree (implementation of C ++ template Technology)

Source: Internet
Author: User

The following Code is only used by me to review the data structure. Its practicality is low ~~ Haha:>

/// C ++ template technology implements a simple binary tree. // # include <cstdlib> # include <cstring> # include <iostream> // binary tree class template pre-declaration template <typename T> class binarytree; //// Binary Tree node template. // template <typename T> class node {friend class binarytree <t>; private: T _ data; node <t> * _ pleftchild, * _ prightchild; public: node (const T & data): _ data (data), _ pleftchild (null), _ prightchild (null) {null ;}}; /// binary tree class template // template <typename T> class binarytree {PRIVATE: node <t> * _ proot; private: void _ create (node <t> ** pproot, t ** pparray, int * pisize, const T & delim ); void _ clear (node <t> * proot); void _ preorder (node <t> * proot) const; void _ inorder (node <t> * proot) const; void _ postorder (node <t> * proot) const; size_t _ height (node <t> * proot) const; size_t _ count (node <t> * proot) const; public: // traversal type. enum traversaltype {pre_order, in_order, post_order}; public: binar Ytree (T * array, int size, const T & delim );~ Binarytree (void); void traversal (const traversaltype type) const; size_t getheight (void) const; size_t getnumberofnode (void) const ;}; // create a binary tree. // The binary tree is constructed based on the forward traversal. The input is the first sequence of the binary tree. // However, a virtual node must be added to it to indicate the position of a null pointer. // For example: // (a) // \// (B) (c) // \// // (d) % (e) (f) /// \/\//// %%%%%%%//// The letter indicates the node, and % indicates the virtual node, so the first sequence is: abd % Ce % F %. /// the pproot parameter is a pointer to the root pointer. Therefore, if you modify * pproot, the real parameter (root pointer) itself is modified. // The pparray parameter is directed to the storage preordered sequence array location. Address pointer, we also need to modify it. // The pisize parameter is a pointer to an integer variable of the array size. We also need to modify it. // The delim parameter is a delimiter used to represent a virtual node. We need to skip the virtual node. // Template <typename T> void binarytree <t >:: _ create (node <t> ** pproot, t ** pparray, int * pisize, const T & delim) {If (0 <* pisize & delim! = ** Pparray) {* pproot = new node <t> (** pparray); _ create (& (* pproot)-> _ pleftchild ), & (++ * pparray), & (-- * pisize), delim); _ create (& (* pproot)-> _ prightchild ), & (++ * pparray), & (-- * pisize), delim) ;}/// delete all nodes. // template <typename T> void binarytree <t >:: _ clear (node <t> * proot) {If (null! = Proot) {_ clear (proot-> _ pleftchild); _ clear (proot-> _ prightchild); Delete proot ;}/// pre-order traversal. // template <typename T> void binarytree <t >:: _ preorder (node <t> * proot) const {If (null! = Proot) {STD: cout <proot-> _ DATA <"; _ preorder (proot-> _ pleftchild ); _ preorder (proot-> _ prightchild) ;}//// ordinal traversal. // template <typename T> void binarytree <t >:: _ inorder (node <t> * proot) const {If (null! = Proot) {_ inorder (proot-> _ pleftchild); STD: cout <proot-> _ DATA <""; _ inorder (proot-> _ prightchild) ;}/// post-order traversal. // template <typename T> void binarytree <t >:: _ postorder (node <t> * proot) const {If (null! = Proot) {_ postorder (proot-> _ pleftchild); _ postorder (proot-> _ prightchild); STD :: cout <proot-> _ DATA <";}}/// calculate the binary tree height by means of post-sequential traversal. // template <typename T> size_t binarytree <t >:: _ height (node <t> * proot) const {static size_t Height = 0, leftheight = 0, rightheight = 0; if (null! = Proot) {leftheight = _ height (proot-> _ pleftchild); rightheight = _ height (proot-> _ prightchild); Height = (leftheight> rightheight? Leftheight: rightheight) + 1 ;}else {Height = 0 ;}return height ;}/// calculate the number of Binary Tree nodes by means of pre-order traversal. // template <typename T> size_t binarytree <t >:: _ count (node <t> * proot) const {static size_t counter = 0; If (null! = Proot) {counter = _ count (proot-> _ pleftchild) + _ count (proot-> _ prightchild) + 1 ;}else {counter = 0 ;}return counter ;} template <typename T> inline binarytree <t>: binarytree (T * array, int size, const T & delim) {_ create (& _ proot, & array, & size, delim);} template <typename T> inline binarytree <t> ::~ Binarytree (void) {_ clear (_ proot) ;}/// select the traversal mode based on the control ID. // template <typename T> void binarytree <t>: traversal (const traversaltype) const {Switch (type) {Case pre_order: _ preorder (_ proot); break; case in_order: _ inorder (_ proot); break; Case post_order: _ postorder (_ proot); break ;}} template <typename T> size_t binarytree <t> :: getheight (void) const {return _ height (_ proot);} template <typename T> size_t binaryt REE <t>: getnumberofnode (void) const {return _ count (_ proot);} // test the binary tree. // int main (void) {const size_t max_size = 100; char szpreorder [max_size] = {'\ 0'}; STD :: cout <"Enter the sequence before the Binary Tree node data:" <STD: Endl; while (! STD: cin. getline (szpreorder, max_size) {STD: cout <"input error. "<STD: Endl; STD: cout <" re-input: "; STD: cin. clear (); STD: cin. sync ();} binarytree <char> bintree (szpreorder, strlen (szpreorder), ''); // traverse a binary tree. // STD: cout <"Forward traversal:"; bintree. traversal (binarytree <char >:: pre_order); STD: cout <STD: Endl <"sequential traversal:"; bintree. traversal (binarytree <char >:: in_order); STD: cout <STD: Endl <"sequential traversal:"; bintree. traversal (binarytree <char >:: post_order); STD: cout <STD: Endl; STD: cout <"tree height:" <bintree. getheight () <STD: Endl; STD: cout <"node:" <bintree. getnumberofnode () <STD: Endl; return exit_success ;}

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.