Data Structures and Algorithms(5)



Similar documents
1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Converting a Number from Decimal to Binary

Data Structure with C

DATA STRUCTURES USING C

Data Structures Using C++ 2E. Chapter 5 Linked Lists

10CS35: Data Structures Using C

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

TREE BASIC TERMINOLOGIES

Binary Search Trees (BST)

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Structural Design Patterns Used in Data Structures Implementation

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Binary Heap Algorithms

Heaps & Priority Queues in the C++ STL 2-3 Trees

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Data Structures. Level 6 C Module Descriptor

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Physical Data Organization

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Unordered Linked Lists

Algorithms and Data Structures

Full and Complete Binary Trees

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Analysis of Algorithms I: Binary Search Trees

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

How To Create A Tree From A Tree In Runtime (For A Tree)

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Binary Trees and Huffman Encoding Binary Search Trees

Analysis of Algorithms I: Optimal Binary Search Trees

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Recursive Implementation of Recursive Data Structures

Binary Search Trees CMPSC 122

Data Structures Fibonacci Heaps, Amortized Analysis

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Algorithms Chapter 12 Binary Search Trees

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

PES Institute of Technology-BSC QUESTION BANK

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

CS711008Z Algorithm Design and Analysis

Binary Heaps. CSE 373 Data Structures

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Linked List as an ADT (cont d.)

Algorithms and Data Structures Written Exam Proposed SOLUTION

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Binary Coded Web Access Pattern Tree in Education Domain

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Big Data and Scripting. Part 4: Memory Hierarchies

Binary Search Trees 3/20/14

Lecture 10: Regression Trees

Lecture Notes on Binary Search Trees

Introduction to Data Structures and Algorithms

Data Structure [Question Bank]

Ordered Lists and Binary Trees

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

The ADT Binary Search Tree

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Chapter 14 The Binary Search Tree

Lecture 1: Data Storage & Index

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Data Structures and Algorithms

From Last Time: Remove (Delete) Operation

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Fast nondeterministic recognition of context-free languages using two queues

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Sample Questions Csci 1112 A. Bellaachia

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Cpt S 223. School of EECS, WSU

Data Structures and Data Manipulation

Data Structures Using C++

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Parallelization: Binary Tree Traversal

Symbol Tables. Introduction

Vector storage and access; algorithms in GIS. This is lecture 6

Chapter 8: Binary Trees

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Binary Search Trees. Ric Glassey

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Algorithms and Data Structures

Glossary of Object Oriented Terms

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Unit Storage Structures 1. Storage Structures. Unit 4.3

Common Data Structures

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Persistent Binary Search Trees

CSE 326: Data Structures B-Trees and B+ Trees

Sorting Hierarchical Data in External Memory for Archiving

Application of Stacks: Postfix Expressions Calculator (cont d.)

1. Domain Name System

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Transcription:

Ming Zhang Data Structures and Algorithms Data Structures and Algorithms(5) Instructor: Ming Zhang Textbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 2008.6 (the "Eleventh Five-Year" national planning textbook) https://courses.edx.org/courses/pekingx/04830050x/2t2014/

Chapter 5 Concepts of Abstract Data Types Depth-First Search Breadth-First Search B D Storage Structures of A E G C H F I Binary Search Tree Heap and Priority Queue Huffman Tree and its Application 2

3 Chapter 5 5.3 Storage Structure of Linked Storage Structure of Each node of a Binary Tree is stored in memory randomly, and the logical relationships among the nodes are linked by pointers. Binary Linked List Left and right pointers, point to its left child and its right child respectively Trinomial Linked List Left and right pointers, point to its left child and its right child respectively Add a parent pointer left info right left info parent right

5.3 Storage Structure of Binary Linked List t D B A E C F G H I A B C D E F G H I 4 (a) (b)

5.3 Storage Structure of Trinomial Linked List Pointer parent points to its parent, the ability of "upward" t D A B E G C F H I A B C D E F G H I 5 (a) (b)

5.3 Storage Structure of Add Two Private Data Members into Class BinaryTreeNode private: BinaryTreeNode<T> *left; // The pointer pointing to the left subtree BinaryTreeNode<T> *right; // The pointer pointing to the right subtree template <class T> class BinaryTreeNode { friend class BinaryTree<T>; // Declare the class of as a friend class private: T info; // Data domain of nodes in a binary tree public: BinaryTreeNode(); // Default constructor function BinaryTreeNode(const T& ele); // Construction with given data BinaryTreeNode(const T& ele, BinaryTreeNode<T> *l, BinaryTreeNode<T> *r); // Construction of nodes in a subtree. } 6

7 Chapter 5 5.3 Storage Structure of A Recursive Structure to Find out the Father Node Be Careful of its Return template<class T> BinaryTreeNode<T>* BinaryTree<T>:: Parent(BinaryTreeNode<T> *rt, BinaryTreeNode<T> *current) { BinaryTreeNode<T> *tmp, if (rt == NULL) return(null); if (current == rt ->leftchild() current == rt->rightchild()) return rt; // If the child is current, then return parent if ((tmp =Parent(rt- >leftchild(), current)!= NULL) return tmp; if ((tmp =Parent(rt- > rightchild(), current)!= NULL) return tmp; return NULL; }

5.3 Storage Structure of Questions What structure does the algorithm use? Which order of traversal does the algorithm belong to? Could we use non-recursive methods? Could we use BFS? How to find out brother nodes starting from this algorithm? 8

} } } 9 Chapter 5 5.3 Storage Structure of A Non-Recursive Structure to Find out the Father Node BinaryTreeNode<T>* BinaryTree<T>::Parent(BinaryTreeNode<T> *current) { using std::stack; // Use the stack in STL stack<binarytreenode<t>* > astack; BinaryTreeNode<T> *pointer = root; astack.push(null); // The lookout at the bottom of the stack while (pointer) { // Or!aStack.empty() if (current == pointer->leftchild() current == pointer->rightchild()) return pointer; // if the child of pointer is current, then return parent if (pointer->rightchild()!= NULL) // Push the non-empty right subtree into the stack astack.push(pointer->rightchild()); if (pointer->leftchild()!= NULL) pointer = pointer->leftchild(); // Go down along the left side else { // After visiting the left subtree, it is turn to visit the right subtree pointer=astack.top(); astack.pop(); // Get the element on the top of the stack and pop it

5.3 Storage Structure of Analysis of the Space Cost Storage density ( 1) means the efficiency of the structure of data storage Storage size of data itself (storage density) = Storage size of the whole structure Overhead of the structure = 1-10

5.3 Storage Structure of According to the property of full binary tree: Half of the pointers are null Each node stores two pointers and one data domain Chapter 5 Analysis of the Space Cost Total space (2p + d)n Cost of the structure:2pn (a) (b) If p = d, then the cost of the structure is 2p/ (2p + d ) = 2/3 t A B C D E F G H I 11

5.3 Storage Structure of Analysis of the Space Cost C++ has two methods to implement different branch and leaf nodes: Use union type to define Use the subclass in C++ to implement branch nodes and leaf nodes respectively, and use virtual function isleaf to distinguish branch nodes and leaf nodes Save memory resource at the early stage Use a idle bit (one bit) of the pointer of a node to mark the type of the node Use the pointer pointing to a leaf or the pointer domain in a leaf to store the value of this leaf node 12

13 5.3 Storage Structure of Sequential Storage Structure of Complete Binary Tree Chapter 5 Sequential method to store a binary tree Store nodes into a piece of continuous space according to a particular order Make the position of nodes in the sequence able to reflect corresponding information of the structure The storage structure is linear 0 1 2 3 4 5 6 7 8 It is still a structure of a binary tree on its logical structure 0 1 2 3 4 5 6 7 8

5.3 Storage Structure of Index Formula of Complete Binary Tree We could know the indexes of a node s parent, children and brothers according to its index When 2i+1<n, the left child of node i is node 2i+1, otherwise node i doesn t have a left child When 2i+2<n, the right child of node i is node 2i+2, 0 1 2 3 4 5 6 7 8 14 otherwise node i doesn t have a right child

5.3 Storage Structure of Binary Trees Index Formula of Complete Binary Tree 0 1 2 3 4 5 6 When 0<i<n, the parent of node i is node (i-1)/2 7 8 When i is even and 0<i<n, the left brother of node i is node i-1, otherwise node i doesn t have a left brother When i is odd and i+1<n, the right brother of node i is node i+1, otherwise node i doesn t have a right brother 15

Binary Tree 5.3 Storage Structure of Questions What changes should we make to the algorithm of binary tree if we use the storage structure of trinomial linked list? We should pay more attention to maintain the father pointer when inserting and deleting nodes. What is the index formula of complete trinomial tree? 16

Ming Zhang Data Structures and Algorithms Data Structures and Algorithms Thanks! the National Elaborate Course (Only available for IPs in China) http://www.jpk.pku.edu.cn/pkujpk/course/sjjg/ Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 2008.6 (awarded as the "Eleventh Five-Year" national planning textbook)