V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
pabupa
V2EX  ›  问与答

遇到了一个问题,,,英文捉急,不知道怎么搜,,所以来这里问一下……

  •  
  •   pabupa · 2019-05-14 21:53:34 +08:00 · 842 次点击
    这是一个创建于 1780 天前的主题,其中的信息可能已经有所发展或是发生改变。
    template<typename T, typename size_type=size_t>
    class BinarySearchTree {
            template<typename Seq>
            void linear(Seq& seq) const;
    }
    
    template<typename T, typename size_type>
    template<typename Seq>
    void BinarySearchTree<T, size_type>::linear(Seq& seq) const {
    	if (!this->_size) return;
    
    	Node* cursor = this->root;
    	Node* cache = nullptr;
    	auto* path = new Stack<Node*>; // Stack<Node*> path;
    	console.log(path); // console.log(&path);
    
    	size_type count = this->_size;
    	while (count) {
    		path->push(cursor);
    
    		if (cursor->lnode != nullptr) {
    			cursor = cursor->lnode;
    			continue;
    		}
    
    		seq.push_back(path->pop()->data);
    		count--;
    
    		if (cursor->rnode != nullptr) {
    			cursor = cursor->rnode;
    			continue;
    		}
    
    		while (count) {
    			cache = path->pop();
    
    			seq.push_back(cache->data);
    			count--;
    
    			if (cache->rnode != nullptr) {
    				cursor = cache->rnode;
    				break;
    			}
    		}
    	}
    
    	delete path;
    }
    
    typedef BinarySearchTree<int> IBST;
    
    int main() {
    	IBST bst;
    	std::vector<int> nums = {100, 2, 7, 4, -1, 0, 14, 8, 3, 124, 380, 266, 255};
    
    	for (auto item : nums) {
    		bst.append(item);
    	}
    
    	for (auto item : nums) {
    		if (!bst.exist(item)) {
    			console.log("Error");
    			break;
    		}
    	}
    
    	std::vector<int> sorted;
    	bst.linear(sorted);
    	sorted.clear();
    	bst.linear(sorted);
    
    	for (auto item : sorted) {
    		console.log(item);
    	}
    }
    

    上面是我实现的中序遍历 bst。

    问题是那个path变量,不论是在堆上、还是栈上,打印出来都是同一个地址,,,,,,,,

    惊呆了。。。。

    pabupa
        1
    pabupa  
    OP
       2019-05-14 22:15:14 +08:00
    emmmmmmmm …
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   939 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:56 · PVG 04:56 · LAX 13:56 · JFK 16:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.