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

2019-05-14 21:53:34 +08:00
 pabupa
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变量,不论是在堆上、还是栈上,打印出来都是同一个地址,,,,,,,,

惊呆了。。。。

849 次点击
所在节点    问与答
1 条回复
pabupa
2019-05-14 22:15:14 +08:00
emmmmmmmm …

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/564174

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX