Data Structures Lecture 28 Sohail Aslam.

Slides:



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

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.
A Binary Tree root leaf. A Binary Tree root leaf descendent of root parent of leaf.
B+ Trees Similar to B trees, with a few slight differences
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
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.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
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.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Trees. Family Tree Recursive Definition Tree - Definition A tree is a finite set of one or more nodes such that: 1.There is a specially designated 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
Partially Ordered Data ,Heap,Binary Heap
Lecture No.14 Data Structures Dr. Sohail Aslam
Recursive Objects (Part 4)
Lecture No.13 Data Structures Dr. Sohail Aslam
UNIT III TREES.
Lecture No.15 Data Structures Dr. Sohail Aslam
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Binary Search Trees.
Trees Another Abstract Data Type (ADT)
Lecture 22 Binary Search Trees Chapter 10 of textbook
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Heapsort.
ITEC 2620M Introduction to Data Structures
Chapter 20: Binary Trees.
Data Structures and Database Applications Binary Trees in C#
Data Structures Lecture 27 Sohail Aslam.
Chapter 21: Binary Trees.
Trees.
Section 10 Questions Heaps.
Search Sorted Array: Binary Search Linked List: Linear Search
Trees Another Abstract Data Type (ADT)
Priority Queue and Binary Heap Neil Tang 02/12/2008
Chapter 16 Tree Implementations
Trees Definitions Implementation Traversals K-ary Trees
Trees Another Abstract Data Type (ADT)
Chapter 9 Binary Trees.
Data Structures Lecture 30 Sohail Aslam.
Operations on Binary Tree
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
Lecture No.20 Data Structures Dr. Sohail Aslam
Design and Analysis of Algorithms
2018, Fall Pusan National University Ki-Joune Li
Representing binary trees with lists
Data Structures Lecture 29 Sohail Aslam.
Heap code in C++ template <class eType>
Binary Tree Properties & Representation
Heapsort.
Chapter 20: Binary Trees.
Dynamic Equivalence Problem
Search Sorted Array: Binary Search Linked List: Linear Search
Lecture 9: Intro to Trees
Tree and its terminologies
Trees Trees.
General Tree Concepts Binary Trees
Data Structures Using C++ 2E
Heaps.
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.
Presentation transcript:

Data Structures Lecture 28 Sohail Aslam

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.

Fix with Dummy Node dummy 14 4 15 3 9 18 7 16 20 5

Inorder traversal void fastInorder(TreeNode* p) { while((p=nexInorder(p)) != dummy) cout << p->getInfo(); } Start the inorder traversal by calling fastInorder(dummy).

Trace or nextInorder dummy p 14 4 15 3 9 18 7 16 20 5

Trace or nextInorder dummy p 14 4 15 3 9 18 7 16 20 5

Trace or nextInorder dummy p 14 4 15 3 9 18 7 16 20 5

Trace or nextInorder dummy 14 p 4 15 3 9 18 7 16 20 5

Trace or nextInorder dummy 14 4 15 p 3 9 18 7 16 20 5

Trace or nextInorder dummy 14 4 15 p 3 9 18 7 16 20 5

Trace or nextInorder dummy 14 4 15 3 9 18 7 16 20 p 5

Trace or nextInorder dummy 14 4 15 3 9 18 7 p 16 20 5 And so on.

Complete Binary Tree

Complete Binary Tree A complete binary tree is a tree that is completely filled, with the possible exception of the bottom level. The bottom level is filled from left to right. Different definition of complete binary tree than an earlier definition we had. The earlier definition could be called a perfect binary tree.

Complete Binary Tree A B C D E F G H I J

Complete Binary Tree Recall that such a tree of height h has between 2h to 2h+1 –1 nodes. The height of such a tree is thus log2N where N is the number of nodes in the tree. Because the tree is so regular, it can be stored in an array; no pointers are necessary.

Complete Binary Tree A B C D E F G H I J A B C D E F G H I J 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Complete Binary Tree For any array element at position i, the left child is at 2i, the right child is at (2i +1) and the parent is at  i  2. A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Complete Binary Tree For any array element at position i, the left child is at 2i, the right child is at (2i +1) and the parent is at  i  2. A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Complete Binary Tree For any array element at position i, the left child is at 2i, the right child is at (2i +1) and the parent is at  i  2. A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Complete Binary Tree For any array element at position i, the left child is at 2i, the right child is at (2i +1) and the parent is at  i  2. A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Complete Binary Tree For any array element at position i, the left child is at 2i, the right child is at (2i +1) and the parent is at  i  2. A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Level-order numbers  array index Complete Binary Tree 1 A 2 3 B C 5 6 7 4 D E F G 8 9 10 H I J A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Level-order numbers  array index

Complete Binary Tree 1 A 2 3 B C 5 6 7 4 D E F G 8 9 10 H I J A B C D 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Complete Binary Tree 1 A 2 3 B C 5 6 7 4 D E F G 8 9 10 H I J A B C D End of lecture 28. Start of lecture 29. A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Question: why don’t we store all binary trees in arrays? Why use pointers?