Problems with Linked List (as we’ve seen so far…)

Slides:



Advertisements
Similar presentations
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Advertisements

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Binary Trees Chapter 6.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Starting at Binary Trees
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
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.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Binary Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
B+-Trees.
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
B+-Trees.
Trees Lecture 12 CS2110 – Fall 2017.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CMSC 341 Introduction to Trees.
Trees.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
i206: Lecture 13: Recursion, continued Trees
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Data Structures and Database Applications Binary Trees in C#
8.1 Tree Terminology and Applications
TREES General trees Binary trees Binary search trees AVL trees
Trees.
Chapter 21: Binary Trees.
Binary Trees.
Trees and Binary Trees.
General Trees & Binary Trees
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Lesson Objectives Aims
Find in a linked list? first last 7  4  3  8 NULL
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Binary Trees.
Trees Lecture 9 CS2110 – Fall 2009.
CMSC 202 Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
General Trees & Binary Trees
Binary Trees.
Announcements Prelim 1 on Tuesday! A4 will be posted today
CSC 143 Java Trees.
Tree.
Chapter 20: Binary Trees.
B-Trees.
Trees.
Tree and its terminologies
Binary Search Trees CS 580U Fall 17.
8.1 Tree Terminology and Applications
8.2 Tree Traversals Chapter 8 - Trees.
Heaps.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Problems with Linked List (as we’ve seen so far…) We can insert a node only after a node we have a pointer to. We can remove a node only if we have a pointer to its predecessor node We can traverse the list in only the forward direction Solution?

Stack Linked list or array?? Subset of list Only 2 operations: push() and pop() Stick something on the end of the stack, or take something off the end of the stack Last-in, first-out (LIFO) Think of a pile of … almost anything at all … you can’t take anything off the BOTTOM of the pile, only off the top. Equally, you can’t add to the pile at the bottom, only at the top. Applications: Reversing a word The “undo” option Backgracking in a maze Memory for functions (the easiest way to visualize this is recursion) Linked list or array?? Hint: oh please…

Pop() How do we implement pop? How many steps? Now pop again… Where is the last pointer still pointing? Is there a better way? (More than one…) What about arrays – will that work better? last 7 4 3 8 first NULL

Doubly-linked list: pop() class DNode { friend class LL; int data; DNode *next; DNode *prev; int size; public: DNode(int x); ~DNode(); }; //DNode Now what do we need to do? DNode *temp = last; last = last->prev; delete temp; last->next = NULL; size--; Now how many steps? last first NULL 7 4 3 8 NULL NULL temp Made it to here

Inserting 6 into the ordered list: temp last first 2 4 7 8 NULL NULL NULL 6 NULL n DNode *temp = first; while (temp->data < 6) { // left out check for end of list temp = temp->next; } DNode *n = new DNode(6); n->prev = temp->prev; temp->prev->next = n; n->next = temp; temp->prev = n; size ++; Easier with a doubly-linked list?

bool LL::delete(int x) { DNode. tmp; for (tmp = first; tmp bool LL::delete(int x) { DNode *tmp; for (tmp = first; tmp != NULL; tmp = tmp->next) { if (tmp->data== x) { if (tmp->prev== NULL) { first = tmp->next; } else if(tmp->next== NULL) { tmp->prev->next = NULL; else { /* Remove from middle */ tmp->prev->next=tmp->next; /* Fix previous node's next to skip over the removed node. */ tmp->next->prev = tmp->prev; /* Fix next node's prev to skip over the removed node. */ delete tmp; return true; return false;

Doubly-linked list: Disadvantages: Advantages: A bit more memory (now we’ve got that prev pointer space for each node) Must manage more pointers when performing operations on the linked list (e.g., insert, remove, etc.) FOR EVERY CHANGE IN LIST: we must manage both the next AND the prev pointer Advantages: Makes pop() easier (O(1)) Makes inserting in ordered lists, and inserting at index 1 a bit easier Makes traversing the list in reverse order easier Reversing the list is easy now Can go backwards and forwards from a node in a list We may need surrounding nodes We may need data that occurred “close to” a node with certain data

How about Find in a linked list? first last 7  4  3  8 NULL Find 3 in the list Find 6 in the list O(n) Is there a better way? Binary search tree!

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 list have? Each node has exactly one predecessor (called the parent) Except the starting node, called the root Links from node to its successors are called branches Nodes with same parent are siblings Nodes with no children are called leaves

Tree We also use words like ancestor and descendent Pets is the parent of Dogs and Cats Poodle and Beagle are the children of Dogs Poodle, Beagle, Persian, and Siamese are descendents of Pets, Pets is the ancestor of Poodle, Beagle, Persian, and Siamese

Tree Terminology Subtree of a node: Depth of a node: 1 2 3 4 A tree whose root is a child of that node Depth of a node: A measure of its distance from the root: Depth of the root = 0 Depth of other nodes = 1 + depth of parent 1 2 3 4

Fullness and Completeness: Trees grow from the top down New values inserted in new leaf nodes In a full tree, every node has 0 or 2 non-null children A complete tree of height h is filled up to depth h-1, and, at depth h, any unfilled nodes are on the right.