Data Structure Learning 3 --- Binary Tree, Data Structure Learning 3 ---

Source: Internet
Author: User

Data Structure Learning 3 --- Binary Tree, Data Structure Learning 3 ---

Binary Tree node

#pragma once#include <stdlib.h>template<class T>class BinaryTreeNode{public:T data;BinaryTreeNode<T>* leftchild;BinaryTreeNode<T>* rightchild;BinaryTreeNode():leftchild(NULL),rightchild(NULL){}BinaryTreeNode(T d):data(d),leftchild(NULL),rightchild(NULL){}};
All operations on a binary tree: Build trees, destroy trees, and perform sequential and non-recursive sequence traversal in ascending order.
# Include "BinaryTreeNode. h "# include <queue> # include <stack> # include <iostream> using namespace std;/* void CreatePreOrder (BinaryTreeNode <T> ** root) void LevelOrderTraverse () // sequence traversal void PreOrderTraverse (BinaryTreeNode <T> * root) // first order traversal recursive void PreOrderTraverse () // first order traversal non-recursive algorithm void InOrderTraverse () // non-recursive method of sequential traversal void PostOrderTraverse () // non-recursive method of post-sequential traversal int depth () // tree depth BinaryTreeNode <T> * Parent (BinaryTreeNode <T> * root, Bin AryTreeNode <T> * node) // returns the BinaryTreeNode of the parent node. <T> * SiblingNode (BinaryTreeNode <T> * node) // return the sibling node */template <class T> class BinaryTree {public: BinaryTreeNode <T> * root; BinaryTree (): root (NULL) {} // empty tree/******************************** first create a chain table in sequence ************************************* * **/void CreatePreOrder (BinaryTreeNode <T> ** root) // or BinaryTreeNode <T> * & root Change {char d; cin> d; if (d = '#') (* root) = NULL; else {* root = New BinaryTreeNode <T> (d); CreatePreOrder (& (* root)-> leftchild); CreatePreOrder (& (* root)-> rightchild );}} /******************************** sequence traversal ***** * *********************************/void LevelOrderTraverse () {if (root = NULL) return; BinaryTreeNode <T> * p; queue <BinaryTreeNode <T> *> myQueue; myQueue. push (root); myQueue. push (NULL); while (! MyQueue. empty () {p = myQueue. front (); myQueue. pop (); if (p = NULL) {if (myQueue. empty () break; cout <endl; myQueue. push (NULL);} else {cout <p-> data; if (p-> leftchild) myQueue. push (p-> leftchild); if (p-> rightchild) myQueue. push (p-> rightchild );}}} /********************************* first-order traversal recursion *** * **********************************/void PreOrderTraverse (BinaryTreeNode <T> * root) {if (root) {cout <root-> data; PreOrderTraverse (Root-> leftchild); PreOrderTraverse (root-> rightchild );}} /******************************** center-order traversal recursion **** * **********************************/void InOrderTraverse (binaryTreeNode <T> * root) {if (root) {InOrderTraverse (root-> leftchild); cout <root-> data; InOrderTraverse (root-> rightchild );}} /******************************** post-order traversal recursion *** * **********************************/void PostOrderTraverse (BinaryTreeNode <T> * r Oot) {if (root) {PostOrderTraverse (root-> leftchild); PostOrderTraverse (root-> rightchild); cout <root-> data ;}} /********************************* traverse non-recursion in sequence ** * ***********************************/void preOrderTraverse () {if (root) {BinaryTreeNode <T> * p; stack <BinaryTreeNode <T> *> myStack; myStack. push (root); while (! MyStack. empty () {p = myStack. top (); myStack. pop (); cout <p-> data; if (p-> rightchild) myStack. push (p-> rightchild); if (p-> leftchild) myStack. push (p-> leftchild );}}} /******************************** non-recursive traversal in the central order *** * **********************************/void InOrderTraverse () {if (root = NULL) return; BinaryTreeNode <T> * p; stack <BinaryTreeNode <T> *> myStack; myStack. push (root); while (! MyStack. empty () {while (myStack. top () myStack. push (myStack. top ()-> leftchild); myStack. pop (); if (! MyStack. empty () {p = myStack. top (); myStack. pop (); cout <p-> data; myStack. push (p-> rightchild );}}} /******************************** post-order traversal non-recursive ** * ***********************************/void postOrderTraverse () {if (root = NULL) return; stack <BinaryTreeNode <T> *> myStack1; stack <BinaryTreeNode <T> *> myStack2; BinaryTreeNode <T> * p; myStack1.push (root); while (! MyStack1.empty () {p = myStack1.top (); myStack1.pop (); myStack2.push (p); if (p-> leftchild) myStack1.push (p-> leftchild ); if (p-> rightchild) myStack1.push (p-> rightchild);} while (! MyStack2.empty () {p = myStack2.top (); myStack2.pop (); cout <p-> data ;}} /********************************* tree depth **** * **********************************/int depth (binaryTreeNode <T> * root) {int dep; if (root = NULL) dep = 0; else {dep = 1 + (depth (root-> leftchild)> depth (root-> rightchild )? Depth (root-> leftchild): depth (root-> rightchild);} return dep ;} /******************************** return to the parent node **** * **********************************/BinaryTreeNode <T> * Parent (BinaryTreeNode <T> * root, binaryTreeNode <T> * node) {if (root = NULL | root = node) return NULL; if (root-> leftchild = node | root-> rightchild = node) {return root;} else if (Parent (root-> leftchild, node )) {return Parent (root-> leftchild, node);} elseret Urn Parent (root-> rightchild, node );} /******************************** return to the parent node (heavy load) **************************************** /BinaryTreeNode <T> * ParentPre (BinaryTreeNode <T> * root, binaryTreeNode <T> * node) // search by traversing the tree {if (root = node | root = NULL) return NULL; if (root) {if (root-> leftchild = node | root-> rightchild = node) return root; if (ParentPre (root-> leftchild, node )) returnParentPre (root-> leftchild, node); elsereturn ParentPre (root-> rightchild, node );}} /******************************** return to the sibling node **** * **********************************/BinaryTreeNode <T> * SiblingNode (BinaryTreeNode <T> * node) {BinaryTreeNode <T> * p = Parent (root, node); if (p) {if (p-> leftchild = node) return p-> rightchild; elsereturn p-> leftchild;} return NULL ;} ***** * ********************************/void DestroyTree (BinaryTre ENode <T> * root) {if (root! = NULL) {DestroyTree (root-> leftchild); DestroyTree (root-> rightchild); delete root;} root = NULL ;}};


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.