Representation of a Threaded Tree

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
© 2004 Goodrich, Tamassia Binary Search Trees   
CS 261 – Winter 2010 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting.
Binary Search Trees1 Part-F1 Binary Search Trees   
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
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.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
1 Graphs and Search Trees Instructor: Mainak Chaudhuri
24/3/00SEM107 - © Kamin & ReddyClass 16 - Searching - 1 Class 16 - Searching r Linear search r Binary search r Binary search trees.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
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.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
CIS* Quiz 2. For a large array, and in the worst case, selection sort is faster than insertion sort. False Both selection sort and insertion sort.
Data Structures and Algorithms TREE. Searching Sequential Searches Time is proportional to n We call this time complexity O(n) Pronounce this “big oh”
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.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Trees - Part II © Dave Bockus. 2 Building a Binary Search Tree h i b c e d f m k a Input: i h b m e c f a d k.
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.
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.
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.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
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.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
Lecture 9 Binary Trees Trees General Definition Terminology
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Recursive Objects (Part 4)
Representation of an AVL Node
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
BST Trees
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Tree Traversals – reminder
Chapter 20: Binary Trees.
Binary Search 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.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Tree Traversals – reminder
Chapter 21: Binary Trees.
AVL Trees CENG 213 Data Structures.
Binary Search Tree AVL Tree
Find in a linked list? first last 7  4  3  8 NULL
Search Sorted Array: Binary Search Linked List: Linear Search
Representation of an AVL Node
2018, Fall Pusan National University Ki-Joune Li
CSC 143 Binary Search Trees.
Chapter 20: Binary Trees.
Basic Data Structures - Trees
Tree traversals BST properties Search Insertion
Data Structures Using C++ 2E
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Representation of a Threaded Tree class ThreadedNode { <declarations for info stored in node, e.g. int info;> ThreadedNode left; ThreadedNode right; boolean lThread; // true if left is a thread boolean rThread; // true if right is a thread public void displayNode(){…} // display info stored in node } left lThread info right rThread COSC 2P03 Week 3 1

Threaded Trees – findSuccessor ThreadedNode findSuccessor(ThreadedNode T) // find successor S of node T { if (T.rThread) // thread points to successor S = T.right; else // successor is leftmost node in right subtree while (!S.lThread) S = S.left; } return S; COSC 2P03 Week 4

Threaded Trees – Inorder Traversal inorderThreadedTraverse(ThreadedNode T) { S = T; // leftmost node is start of traversal while (!S.lThread) S=S.left; while (S != null) displayNode(S); // visit S S = findSuccessor(S); } COSC 2P03 Week 4

Threaded Trees – thread existing tree ThreadedNode rightThreadTree(ThreadedNode T) { ThreadThis = null; if (T.left != null) { // thread left subtree ThreadThis = rightThreadTree(T.left); // ThreadThis is last node visited in T’s left subtree // so its successor is T: set thread ThreadThis.right = T; } if (T.right != null) // thread right subtree ThreadThis = rightThreadTree(T.right); else // T.right is null, so must be turned into a thread ThreadThis = T; T.rThread = true; return ThreadThis; // return node that needs a thread COSC 2P03 Week 4

Threaded Trees – threadedInsert void threadedInsert(ThreadedNode T, ThreadedNode newNode) // insert newNode into tree with root T // T is already threaded – ensure this is maintained { if(T == null) T = newNode; else if (newNode.info < T.info) // insert in left subtree if(T.lThread) // insert here newNode.left = T.left; newNode.right = T; T.left = newNode; T.lThread = false; } else T.left = threadedInsert(T.left, newNode); else // insert in right subtree if(T.rThread) // insert here newNode.left = T; newNode.right = T.right; T.right = newNode; T.rThread = false; T.right = threadedInsert(T.right, newNode); COSC 2P03 Week 4

Height-Balanced Trees Search complexity for binary search tree with n nodes: Best case: O(log n) Worst-case: O(n) Height-balanced trees attempt to keep subtrees at roughly the same height Some possible options for a balance condition: Left and right subtrees of the root have the same height: but height can still be n/2 For every node, left and right subtrees have the same height: only possible if n = 2k - 1 For every node, left and right subtrees differ in height by at most 1: this is the AVL property COSC 2P03 Week 4