zoukankan      html  css  js  c++  java
  • 递归输出二叉树的每一个结点

    算法导论:10.4-2 给定一个二叉树,写出一个 O(n) 时间的递归过程,将该树每一个结点的keyword输出。


    #ifndef _BINARY_TREE_H_
    #define _BINARY_TREE_H_
    
    /*****************************************************
    算法导论:10.4-2 一个二叉树,使用递归输出每一个结点
    ******************************************************/
    
    #include <iostream>
    
    template <class T>
    class BinaryTree;
    
    template <class T>
    class BinaryTree
    {
    public:
    	class Node{
    	public:
    		friend class BinaryTree < T >;
    		T value;
    	private:
    		Node() :_parent(nullptr), _left(nullptr), _right(nullptr){}
    		Node(const T& v) :_parent(nullptr), _left(nullptr), _right(nullptr), value(v){}
    		Node* _parent;
    		Node* _left;
    		Node* _right;
    	};
    
    	BinaryTree() :_root(nullptr){  }
    	// 使用一个数组构造一个全然二叉树。给出数组的头指针和数组的长度
    	BinaryTree(T*, size_t);
    	~BinaryTree();
    
    	Node *getRoot()const{ return _root; }
    
    	void print() const;
    
    private:
    	// 二叉树的根结点
    	Node* _root;
    
    	void freeNodes(const Node* root);
    	void print(const Node*) const;
    	void createTree(Node *root, T* a, size_t pos, size_t size);
    };
    
    template <class T>
    BinaryTree<T>::BinaryTree(T* a, size_t size){
    	_root = new Node(a[0]);
    	createTree(_root, a, 0, size);
    }
    
    template<class T>
    void BinaryTree<T>::createTree(Node *root, T* a, size_t pos, size_t size){
    	// 将数组中的元素依照顺序增加到二叉树中
    	// 左子树坐标,左子数的坐标为 2 * pos + 1
    	size_t pos_left = 2 * pos + 1;
    	// 右子树坐标。右子树的坐标为 2 * pos + 2
    	size_t pos_right = pos_left + 1;
    	// 创建根结点
    	if(pos_left < size){
    		// 创建左子树
    		root->_left = new Node(a[pos_left]);
    		createTree(root->_left, a, pos_left, size);
    	}
    	if(pos_right < size){
    		// 创建右子树
    		root->_right = new Node(a[pos_right]);
    		createTree(root->_right, a, pos_right, size);
    	}
    }
    
    template <class T>
    BinaryTree<T>::~BinaryTree(){
    	// 释放全部结点所占空间
    	if (_root)
    		freeNodes(_root);
    }
    
    template <class T>
    void BinaryTree<T>::freeNodes(const Node* root){
    	// 释放左孩子结点
    	if (root->_left)
    		freeNodes(root->_left);
    	// 释放右孩子结点
    	if (root->_right)
    		freeNodes(root->_right);
    	// 释放本结点
    	delete root;
    }
    
    template <class T>
    void BinaryTree<T>::print() const{
    	if (_root) {
    		print(_root);
    	}
    }
    
    template <class T>
    void BinaryTree<T>::print(const Node* root) const{
    	if(root){
    		std::cout << root->value << " ";
    		print(root->_left);
    		print(root->_right);
    	}
    }
    #endif


  • 相关阅读:
    C# 字符串多行显示、文本换行
    安卓TextView显示图片与文字作为底部菜单
    在Eclipse中载入安卓SDK框架源代码(部分)
    Android分享到腾讯微博,信息,新浪微博等等,的实现方式
    PHP运行环境配置
    Android布局设计之ExpandableList绑定XML数据构成级联、item布局页面的控件查找及配置child事件,自定义适配显示内容
    Flash开发Android应用
    Android布局设计之ListView使用XML数据源来分页加载
    移动应用开发视频教学下载大全(VeryCD上)
    前端 关于请求地址时出现乱码, 出现%E2%80%8B的问题
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5152994.html
Copyright © 2011-2022 走看看