Data Structures Lecture 27 Sohail Aslam.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
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.
Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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,
Binary Tree A data structure whose nodes contain two pointer fields. One or both pointers can have the value NULL. Each node in a binary tree can have.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Binary Trees Chapter 6.
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.
Foundation of Computing Systems Lecture 6 Trees: Part III.
Tree.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Threaded Binary Tree.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
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)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary 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
CSE 373 Data Structures Lecture 7
Non Linear Data Structure
Recursive Objects (Part 4)
Lecture No.13 Data Structures Dr. Sohail Aslam
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Lecture No.15 Data Structures Dr. Sohail Aslam
Multiway search trees and the (2,4)-tree
Lecture 22 Binary Search Trees Chapter 10 of textbook
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Chapter 16 Tree Implementations
ITEC 2620M Introduction to Data Structures
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
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.
TREES General trees Binary trees Binary search trees AVL trees
Data Structures Balanced Trees CSCI
Chapter 21: Binary Trees.
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Chapter 16 Tree Implementations
Lecture 7 Algorithm Analysis
Huffman Encoding Huffman code is method for the compression for standard text documents. It makes use of a binary tree to develop codes of varying lengths.
Chapter 12: Binary Search Trees
General Trees & Binary Trees
Lecture 7 Algorithm Analysis
2018, Fall Pusan National University Ki-Joune Li
Binary Trees, Binary Search Trees
Data Structures Lecture 28 Sohail Aslam.
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Data Structures Lecture 27 Sohail Aslam

Properties of Binary Tree Property: A binary tree with N internal nodes has 2N links: N-1 links to internal nodes and N+1 links to external nodes. Start lecture 27

Threaded Binary Tree Property: A binary tree with N internal nodes has 2N links: N-1 links to internal nodes and N+1 links to external nodes. A B C internal link D E F external link G E F Internal links: 8 External links: 10

Properties of Binary Tree Property: A binary tree with N internal nodes has 2N links: N-1 links to internal nodes and N+1 links to external nodes. In every rooted tree, each node, except the root, has a unique parent. Every link connects a node to its parent, so there are N-1 links connecting internal nodes. Similarly, each of the N+1 external nodes has one link to its parent. Thus N-1+N+1=2N links.

Threaded Binary Trees

Threaded Binary Tree In many applications binary tree traversals are carried out repeatedly. The overhead of stack operations during recursive calls can be costly. The same would true if we use a non-recursive but stack-driven traversal procedure It would be useful to modify the tree data structure which represents the binary tree so as to speed up, say, the inorder traversal process: make it "stack-free".

Threaded Binary Tree Oddly, most of the pointer fields in our representation of binary trees are NULL! Since every node (except the root) is pointed to, there are only N-1 non-NULL pointers out of a possible 2N (for an N node tree), so that N+1 pointers are NULL.

Threaded Binary Tree Internal nodes: 9 External nodes: 10 A B C F G E F external node

Threaded Binary Tree The threaded tree data structure will replace these NULL pointers with pointers to the inorder successor (predecessor) of a node as appropriate. We'll need to know whenever formerly NULL pointers have been replaced by non NULL pointers to successor/predecessor nodes, since otherwise there's no way to distinguish those pointers from the customary pointers to children.

Adding threads during insert 14 15 18 p 16 20 t t->L = p->L; // copy the thread t->LTH = thread; t->R = p; // *p is successor of *t t->RTH = thread; p->L = t; // attach the new leaf p->LTH = child;

Adding threads during insert 14 15 18 p 1 16 20 t 1. t->L = p->L; // copy the thread t->LTH = thread; t->R = p; // *p is successor of *t t->RTH = thread; p->L = t; // attach the new leaf p->LTH = child;

Adding threads during insert 14 15 18 p 1 16 20 t 2 1. t->L = p->L; // copy the thread 2. t->LTH = thread; t->R = p; // *p is successor of *t t->RTH = thread; p->L = t; // attach the new leaf p->LTH = child;

Adding threads during insert 14 15 18 p 1 16 20 t 2 1. t->L = p->L; // copy the thread 2. t->LTH = thread; 3. t->R = p; // *p is successor of *t t->RTH = thread; p->L = t; // attach the new leaf p->LTH = child; 3

Adding threads during insert 14 15 18 p 1 16 20 t 2 4 1. t->L = p->L; // copy the thread 2. t->LTH = thread; 3. t->R = p; // *p is successor of *t 4. t->RTH = thread; p->L = t; // attach the new leaf p->LTH = child; 3

Adding threads during insert 14 15 18 p 1 5 16 20 t 2 4 1. t->L = p->L; // copy the thread 2. t->LTH = thread; 3. t->R = p; // *p is successor of *t 4. t->RTH = thread; 5. p->L = t; // attach the new leaf p->LTH = child; 3

Adding threads during insert 14 15 18 p 1 5 6 16 20 t 2 4 1. t->L = p->L; // copy the thread 2. t->LTH = thread; 3. t->R = p; // *p is successor of *t 4. t->RTH = thread; 5. p->L = t; // attach the new leaf 6. p->LTH = child; 3

Threaded Binary Tree 14 4 15 3 9 18 7 16 20 5

Where is inorder successor? Inorder successor of 4. 14  4 15 3 9 18 7 16 20 5

Where is inorder successor? Inorder successor of 4. 14  4 15 3 9 18 7 16 20 5 Left-most node in right subtree of 4

Where is inorder successor? Inorder successor of 9. 14 4 15 3 9 18  7 16 20 5 Follow right thread to 14

Routine: nextInorder TreeNode* nextInorder(TreeNode* p) { if(p->RTH == thread) return(p->R); else { p = p->R; while(p->LTH == child) p = p->L; return p; }

Inorder traversal If we can get things started correctly, we can simply call nextInorder repeatedly (in a simple loop) and move rapidly around the tree inorder printing node labels (say) - without a stack. If we call nextInorder with the root of the binary tree, we're going to have some difficulty. The code won't work at all the way we want.

Calling nextInorder with root TreeNode* nextInorder(TreeNode* p){ if(p->RTH == thread) return(p->R); else { p = p->R; while(p->LTH == child) p = p->L; return p; } 14 15 4 9 7 18 3 5 16 20 p Start lecture 28

Calling nextInorder with root TreeNode* nextInorder(TreeNode* p){ if(p->RTH == thread) return(p->R); else { p = p->R; while(p->LTH == child) p = p->L; return p; } 14 15 4 9 7 18 3 5 16 20 p? End of lecture 27.