常见的二叉树遍历有递归和栈循环两种方式,其实还有另一种更为巧妙的遍历方式Morris Traversal。
Morris Traversal的空间复杂度为O(1),时间复杂度为O(n)
我们知道,在深度搜索遍历的过程中,之所以要用递归或者是用非递归的栈方式,都是因为其他的方式没法记录当前节点的parent,而如果在每个节点的结构里面加个parent 分量显然是不现实的,那么Morris是怎么解决这一问题的呢?好吧,他用得很巧妙,实际上是用叶子节点的空指针来记录当前节点的位置,然后一旦遍历到了叶子节点,发现叶子节点的右指针指向的是当前节点,那么就认为以当前节点的左子树已经遍历完成。
以inorder为例,初始化当前节点为root,它的遍历规则如下:
- 如果当前节点为空,程序退出。
- 如果当前节点非空,
- 如果当前节点的左儿子为空,那么输出当前节点,当前节点重置为当前节点的右儿子。
- 如果当前节点的左儿子非空,找到当前节点左子树的最右叶子节点(此时最右节点的右儿子有两种情况,一种是指向当前节点,一种是为空,你也许感到奇怪,右节点的右儿子怎么可能非空,注意,这里的最右叶子节点只带的是原树中的最右叶子节点。),若其最右叶子节点为空,令其指向当前节点,将当前节点重置为其左儿子,若其最右节点指向当前节点,输出当前节点,将当前节点重置为当前节点的右儿子,并恢复树结构,即将最右节点的右节点再次设置为NULL
morris 中序遍历
- void bst_morris_inorder(struct bst_node *root)
- {
- struct bst_node *p = root, *tmp;
-
- while (p) {
- if (p->left == NULL) {
- printf("%d ", p->key);
- p = p->right;
- }
- else {
- tmp = p->left;
- while (tmp->right != NULL && tmp->right != p)
- tmp = tmp->right;
- if (tmp->right == NULL) {
- tmp->right = p;
- p = p->left;
- }
- else {
- printf("%d ", p->key);
- tmp->right = NULL;
- p = p->right;
- }
- }
- }
- }
代码相当简单,局部变量也只需要两个。以下简单讲述其实现方式。
morris traversal 原理很简单,利用所有叶子结点的right 指针,指向其后继结点,组成一个环,在第二次遍历到这个结点时,由于其左子树已经遍历完了,则访问该结点
如下图为morris 的一个示例
morris 前序遍历
- void bst_morris_preorder(struct bst_node *root)
- {
- struct bst_node *p = root, *tmp;
-
- while (p) {
- if (p->left == NULL) {
- printf("%d ", p->key);
- p = p->right;
- }
- else {
- tmp = p->left;
- while (tmp->right != NULL && tmp->right != p)
- tmp = tmp->right;
- if (tmp->right == NULL) {
- printf("%d ", p->key);
- tmp->right = p;
- p = p->left;
- }
- else {
- tmp->right = NULL;
- p = p->right;
- }
- }
- }
- }
比较简单,只要注意访问结点的顺序即可
morris 后序遍历
- static void bst_morris_reverse(struct bst_node *node, struct bst_node *last)
- {
- struct bst_node *p = node, *x, *y;
- if (p == last) {
- printf("%d ", last->key);
- return;
- }
-
-
- x = p->right;
- for (;;) {
- if (x == last) {
- x->right = p;
- break;
- }
- y = x->right;
- x->right = p;
- p = x;
- x = y;
- }
-
-
- x = last;
- for (;;) {
- printf("%d ", x->key);
- if (x == node)
- break;
- x = x->right;
- }
-
-
- p = last;
- x = last->right;
- for (;;) {
- if (x == node) {
- x->right = p;
- break;
- }
- y = x->right;
- x->right = p;
- p = x;
- x = y;
- }
- }
-
-
- void bst_morris_postorder(struct bst_node *root)
- {
- struct bst_node dummy;
- struct bst_node *p, *tmp;
-
- dummy.left = root;
- dummy.right = NULL;
- p = &dummy;
-
- while (p) {
- if (p->left == NULL) {
- p = p->right;
- }
- else {
- tmp = p->left;
- while (tmp->right != NULL && tmp->right != p)
- tmp = tmp->right;
- if (tmp->right == NULL) {
- tmp->right = p;
- p = p->left;
- }
- else {
- bst_morris_reverse(p->left, tmp);
- tmp->right = NULL;
- p = p->right;
- }
- }
- }
- }
后序遍历比较复杂,它的思路是利用中序遍历,所以首先产生了一个假的根结点,其左子树为原来的二叉树,从假的根结点开始中序遍历
它和中序遍历有所不同,在发现当前结点左子树为空时,不访问此结点(后序遍历需要保证访问完右子树后才能访问根结点),直接访问右子树。
第二次遍历到某个结点时,将该结点左子树的最右路径反序输出即可,对应函数为bst_morris_reverse