Binary Tree C ++ implementation

Source: Internet
Author: User

I recently sorted out some of the original code, but it was a bit difficult to understand. Please write it down.

// Binary_tree.h. traversal includes recursive and non-recursive types.

# Ifndef _ binary_tree_h _ # DEFINE _ binary_tree_h_template <class T> struct binode {t data; binode <t> * lchild, * rchild ;}; template <class T> class bitree {public: bitree ();~ Bitree (); binode <t> * getroot (); void preorder (binode <t> * node); void inorder (binode <t> * node ); void postorder (binode <t> * node); // non-recursive void preordernonrec (binode <t> * node); void inordernonrec (binode <t> * node ); void postordernonrec (binode <t> * node); void levelorder (binode <t> * node); // layered traversal protected: Private: binode <t> * m_root; binode <t> * Create (); void release (binode <t> * root);}; # endif

// Bianry_tree.cpp

# Include <iostream> # include <stack> # include <queue> using namespace STD; Template <class T> binode <t> * bitree <t>: getroot () {return m_root;} template <class T> bitree <t >:: bitree () {m_root = new binode <t>; m_root = create ();} template <class T> bitree <t> ::~ Bitree () {release (m_root);} template <class T> binode <t> * bitree <t >:: create () {char ch; CIN> CH; binode <t> * pnode; If (CH = '#') pnode = NULL; else {pnode = new binode <t>; pnode-> DATA = CH; pnode-> lchild = create (); pnode-> rchild = create ();} return pnode;} template <class T> void bitree <t> :: release (binode <t> * root) {If (root! = NULL) {release (root-> lchild); release (root-> rchild) ;}} template <class T> void bitree <t> :: preorder (binode <t> * node) {// the first part of the CLR must be the root if (node = NULL) return; else {cout <node-> data <""; preorder (node-> lchild); preorder (node-> rchild );}} template <class T> void bitree <t>: inorder (binode <t> * node) {// the unique binary tree can be determined by the forward traversal and the middle traversal. // the latter and the middle traversal are also allowed. // However, the former and the latter are not allowed together. // the former and the latter are characteristic of the latter. dinggen, central order left and right IF (node = NULL) return; else {inorder (node-> lchild); cou T <node-> data <""; inorder (node-> rchild) ;}}template <class T> void bitree <t> :: postorder (binode <t> * node) {// The Last LRT must be the root if (node = NULL) return; else {postorder (node-> lchild ); postorder (node-> rchild); cout <node-> data <";}}template <class T> void bitree <t> :: preordernonrec (binode <t> * node) {stack <binode <t> *> S; binode <t> * P = node; while (P! = NULL |! S. Empty () {While (P! = NULL) {cout <p-> data <"; S. Push (p); P = p-> lchild;} If (! S. empty () {P = S. top (); S. pop (); P = p-> rchild; }}template <class T> void bitree <t>: inordernonrec (binode <t> * node) {// you need to use stack s to traverse non-recursion in sequence, and simulate recursive calls. // The overall loop boundary is that the current node is not empty or the stack is not empty, // @ 1 when the current node P is not empty, assign the left subtree of P into the stack S and P to ensure that the Left subtree can be empty in the stack // when P is empty, that is, when the left subtree is accessed at the leftmost, when the stack is not empty, // @ 2 gets the top of the stack to P, input P, and output the stack, at this time, the leftmost node at the bottom layer is accessed, and the right subtree of P is assigned to P, repeating @ 1 stack <binode <t> *> S; binode <t> * P = node; while (P! = NULL |! S. Empty () {While (P! = NULL) {S. Push (p); P = p-> lchild;} If (! S. empty () {P = S. top (); cout <p-> data <""; S. pop (); P = p-> rchild; }}template <class T> void bitree <t>: postordernonrec (binode <t> * node) {// ensure that the root node can be accessed only after the Left and Right nodes are accessed. // For any node P, the root node is first added to the stack. If P does not exist, you can directly access it. // or P has left or right children, but both left and right children have been accessed, you can also directly access this node. // If the preceding two conditions are not met, the right and left children of P are added to the stack in sequence. This ensures that each time the top element of the stack is taken, // The left child is accessed in front of the right child, and the left child and the Right child are accessed in front of the root node. If (node = NULL) return; stack <binode <t> *> S; S. push (node); // The node is rootbinode <t> * pre = NULL; binode <t> * cur; while (! S. Empty () {cur = S. Top (); If (cur-> lchild = NULL & cur-> rchild = NULL | (pre! = NULL) & (pre = cur-> lchild | pre = cur-> rchild )) // The last access is the left subtree of the current node {cout <cur-> data <""; S. pop (); Pre = cur;} else {If (cur-> rchild) s. push (cur-> rchild); If (cur-> lchild) s. push (cur-> lchild) ;}} template <class T> void bitree <t >:: levelorder (binode <t> * node) {// The queue is required for hierarchical traversal. Thought: // @ 1 initialize the queue // if the root is empty, return // @ 2 push (Root) // @ 3 while (the queue is not empty) // S <-- queue. front () // queue. pop () // input S. data // If (left subtree of S is not empty) // enter the left subtree of S // I F (the right subtree of S is not empty) // The right subtree of S queues queue <binode <t> *> q; binode <t> * s = node; if (S = NULL) return; q. push (s); While (! Q. empty () {S = Q. front (); q. pop (); cout <s-> data <"; if (S-> lchild) Q. push (S-> lchild); If (S-> rchild) Q. push (S-> rchild );}}

Test]

#include "list.h"#include "binary_tree.h"#include <iostream>using namespace std;int main(){BiTree<char> my_tree;my_tree.PreOrder(my_tree.GetRoot());cout<<endl;my_tree.PreOrderNonRec(my_tree.GetRoot());cout<<endl;my_tree.InOrder(my_tree.GetRoot());cout<<endl;my_tree.InOrderNonRec(my_tree.GetRoot());cout<<endl;my_tree.PostOrder(my_tree.GetRoot());cout<<endl;my_tree.LevelOrder(my_tree.GetRoot());cout<<endl;my_tree.PostOrderNonRec(my_tree.GetRoot());cout<<endl;}

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.