2018, Fall Pusan National University Ki-Joune Li Threaded Binary Tree 2018, Fall Pusan National University Ki-Joune Li
Threaded Binary Tree – Basic Concept Limitations of Binary Tree Next node to visit (e.g. in-order traversal) Need an additional data structure: stack Some pointers to left and right children are NULL (not used) Use empty (null) left and/or right children a b c d e ? f ? ? ? ? ? ?
Threaded Binary Tree – single threaded binary tree c d e f g h
Threaded Binary Tree – single threaded binary tree Convert a binary tree to threaded binary tree a b c d e f g h Use in-order traversal: d b g e a c h f The right child pointer of rightmost node is null. If right child pointer is null, replace it with the pointer to successor of in-order traversal.
Threaded Binary Tree – single threaded binary tree c thread node f d e g null thread class Node { T data; Node *left, *right; bool rightThread; // true when right is a // thread }; class ThreadedBinaryTree { Node *root; public: void inOrder(); };
Threaded Binary Tree – single threaded binary tree: traversal c p d e Thread node f null Print: d g h null void ThreadedBinaryTree::inOrder() { Node *p=leftmost(root); while (p != NULL) { cout<< p->data <<“ “; if (p->rightThread) p=p->right; else p=leftmost(p->right); } Node* leftMost(Node *n) { if(n==NULL) return NULL; while (n->left != NULL) n=n->left; return n; }
Threaded Binary Tree – single threaded binary tree: traversal c p d e Thread node f null Print: d g h null void ThreadedBinaryTree::inOrder() { Node *p=leftmost(root); while (p != NULL) { cout<< p->data <<“ “; if (p->rightThread) p=p->right; else p=leftmost(p->right); } Node* leftMost(Node *n) { if(n==NULL) return NULL; while (n->left != NULL) n=n->left; return n; }
Threaded Binary Tree – single threaded binary tree: traversal p b c d e f null Print: d g h null void ThreadedBinaryTree::inOrder() { Node *p=leftmost(root); while (p != NULL) { cout<< p->data <<“ “; if (p->rightThread) p=p->right; else p=leftmost(p->right); } Node* leftMost(Node *n) { if(n==NULL) return NULL; while (n->left != NULL) n=n->left; return n; }
Threaded Binary Tree – single threaded binary tree: traversal p b c Not thread node d e f null Print: d b g h null void ThreadedBinaryTree::inOrder() { Node *p=leftmost(root); while (p != NULL) { cout<< p->data <<“ “; if (p->rightThread) p=p->right; else p=leftmost(p->right); } Node* leftMost(Node *n) { if(n==NULL) return NULL; while (n->left != NULL) n=n->left; return n; }
Threaded Binary Tree – single threaded binary tree: traversal c d e f null Print: d b g g h null p thread node void ThreadedBinaryTree::inOrder() { Node *p=leftmost(root); while (p != NULL) { cout<< p->data <<“ “; if (p->rightThread) p=p->right; else p=leftmost(p->right); } Node* leftMost(Node *n) { if(n==NULL) return NULL; while (n->left != NULL) n=n->left; return n; }
Threaded Binary Tree – single threaded binary tree: traversal c d e f null p Print: d b g e g h null void ThreadedBinaryTree::inOrder() { Node *p=leftmost(root); while (p != NULL) { cout<< p->data <<“ “; if (p->rightThread) p=p->right; else p=leftmost(p->right); } Node* leftMost(Node *n) { if(n==NULL) return NULL; while (n->left != NULL) n=n->left; return n; }
Threaded Binary Tree – single threaded binary tree: traversal Advantage of TBT: Not a recursive algorithm no stack is needed void ThreadedBinaryTree::inOrder() { Node *p=leftmost(root); while (p != NULL) { cout<< p->data <<“ “; if (p->rightThread) p=p->right; else p=leftmost(p->right); } Node* leftMost(Node *n) { if(n==NULL) return NULL; while (n->left != NULL) n=n->left; return n; }
Threaded Binary Tree – single threaded binary tree: traversal c d e f null g h null void ThreadedBinaryTree::inOrder() { Node *p=leftmost(root); while (p != NULL) { cout<< p->data <<“ “; if (p->rightThread) p=p->right; else p=leftmost(p->right); } How to modify it for preorder() ?
Threaded Binary Tree – double (full) threaded binary tree c d e f null h null g Use in-order traversal: (null) d b g e a c h f (null) The left child of leftmost node and right child pointer of rightmost node are null. If right child pointer is null, replace it with the pointer to successor of in-order traversal.