Presentation is loading. Please wait.

Presentation is loading. Please wait.

2018, Fall Pusan National University Ki-Joune Li

Similar presentations


Presentation on theme: "2018, Fall Pusan National University Ki-Joune Li"— Presentation transcript:

1 2018, Fall Pusan National University Ki-Joune Li
Threaded Binary Tree 2018, Fall Pusan National University Ki-Joune Li

2 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 ? ? ? ? ? ?

3 Threaded Binary Tree – single threaded binary tree
c d e f g h

4 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.

5 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(); };

6 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; }

7 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; }

8 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; }

9 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; }

10 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; }

11 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; }

12 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; }

13 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() ?

14 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.


Download ppt "2018, Fall Pusan National University Ki-Joune Li"

Similar presentations


Ads by Google