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.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
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.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
BST Data Structure A BST node contains: A BST contains
B+ Trees Similar to B trees, with a few slight differences
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
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.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
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.
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.
 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.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
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
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Binary Search Trees Chapter 7 Objectives
Chapter 12 – Data Structures
AA Trees.
Recursive Objects (Part 4)
CS 3013 Binary Search Trees.
BST Trees
Binary Search Tree (BST)
Binary Search Trees.
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Data Structures & Algorithm Design
ITEC 2620M Introduction to Data Structures
Chapter 20: Binary Trees.
Data Structures Lecture 27 Sohail Aslam.
TREES General trees Binary trees Binary search trees AVL trees
Chapter 21: Binary Trees.
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Search Sorted Array: Binary Search Linked List: Linear Search
Chapter 12: Binary Search Trees
2018, Fall Pusan National University Ki-Joune Li
Binary Search Trees Chapter 7 Objectives
Chapter 20: Binary Trees.
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
CSC 205 – Java Programming II
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.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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 We have the pointers reference the next node in an inorder traversal; called threads We need to know if a pointer is an actual link or a thread, so we keep a boolean for each pointer

Threaded Tree Code Example code: class Node { int val; Node * left, * right; boolean leftThread, rightThread; }

Threaded Tree Example 6 8 3 1 5 7 11 9 13

Threaded Tree Traversal We start at the leftmost node in the tree, print it, and follow its right thread If we follow a thread to the right, we output the node and continue to its right If we follow a link to the right, we go to the leftmost node, print it, and continue

Threaded Tree Traversal Output 1 6 8 3 1 5 7 11 9 13 Start at leftmost node, print it

Threaded Tree Traversal Output 1 3 6 8 3 1 5 7 11 9 13 Follow thread to right, print node

Threaded Tree Traversal Output 1 3 5 6 8 3 1 5 7 11 9 13 Follow link to right, go to leftmost node and print

Threaded Tree Traversal Output 1 3 5 6 6 8 3 1 5 7 11 9 13 Follow thread to right, print node

Threaded Tree Traversal Output 1 3 5 6 7 6 8 3 1 5 7 11 9 13 Follow link to right, go to leftmost node and print

Threaded Tree Traversal Output 1 3 5 6 7 8 6 8 3 1 5 7 11 9 13 Follow thread to right, print node

Threaded Tree Traversal Output 1 3 5 6 7 8 9 6 8 3 1 5 7 11 9 13 Follow link to right, go to leftmost node and print

Threaded Tree Traversal Output 1 3 5 6 7 8 9 11 6 8 3 1 5 7 11 9 13 Follow thread to right, print node

Threaded Tree Traversal Output 1 3 5 6 7 8 9 11 13 6 8 3 1 5 7 11 9 13 Follow link to right, go to leftmost node and print

Threaded Tree Traversal Code Node * leftMost(Node *n) { Node * ans = n; if (ans == null) { return nullptr; } while (ans.left != nullptr) { ans = ans->left; return ans; void inOrder(Node *n) { Node cur = leftmost(n); while (cur != nullptr) { print(cur->val); if (cur->rightThread) { cur = cur->right; } else { cur = leftmost(cur->right); }

Threaded Tree Modification We’re still wasting pointers, since half of our leafs’ pointers are still null We can add threads to the previous node in an inorder traversal as well, which we can use to traverse the tree backwards or even to do postorder traversals

Threaded Tree Modification 6 8 3 1 5 7 11 9 13

To use these we need to implement threaded versions for insert delete destroy copy traversal search as well as others ie balancing.

Insertion It’s a little more complicated to insert a new node into a threaded BST than into an unthreaded one. We must set the new node's left and right threads to point to its predecessor and successor, respectively Suppose that new node n is the right child of its parent p This means that p is n's predecessor, because n is the least node in p's right subtree. 

continued  n's successor is therefore the node that was p's successor before n was inserted, ie, it is the same as p's former right thread.