2018, Fall Pusan National University Ki-Joune Li

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
B+ Trees Similar to B trees, with a few slight differences
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Data Structures Using C++1 Chapter 11 Binary Trees.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Tree Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Traversing Trees. Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree. Consider the three operations: V: Visit a node.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Binary Search Trees Chapter 7 Objectives
Recursive Objects (Part 4)
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Search Trees Chapter 7 Objectives
Binary Search Trees.
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Tree Traversals – reminder
Chapter 20: Binary Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Data Structures Lecture 27 Sohail Aslam.
Tree Traversals – reminder
Chapter 21: Binary Trees.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
2017, Fall Pusan National University Ki-Joune Li
Binary Trees.
Trees Definitions Implementation Traversals K-ary Trees
Binary Tree Traversals
2018, Fall Pusan National University Ki-Joune Li
m-Way Search Trees A m-way Search tree of degree ‘m’ can have
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Tree Traversal.
General Tree Concepts Binary Trees
Amir Kamil 8/8/02 1 B+ Trees Similar to B trees, with a few slight differences All data is stored at the leaf nodes (leaf pages); all other nodes (index.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

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.