Trees Dr. Andrew Wallace PhD BEng(hons) EurIng

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and black antennas.
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.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
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,
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Trees and Red-Black Trees Gordon College Prof. Brinton.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Dr. Andrew Wallace PhD BEng(hons) EurIng
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
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.
CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of.
CSIT 402 Data Structures II
Lecture 2 Red-Black Trees. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 2 Red-Black Trees Definition: A red-black tree is a binary search tree in which: 
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Trees (BST)
Red-Black Trees Definitions and Bottom-Up Insertion.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Foundation of Computing Systems Lecture 4 Trees: Part I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Red-Black Trees Bottom-Up Deletion. Recall “ordinary” BST Delete 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has just.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
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
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
CSE 373 Data Structures Lecture 7
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CC 215 Data Structures Trees
CSCE 210 Data Structures and Algorithms
Fundamentals of Programming II Introduction to Trees
Tree.
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Trees CSE 373 Data Structures.
Data Structures and Algorithms
Binary Trees, Binary Search Trees
Non-Linear Structures
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

Trees Dr. Andrew Wallace PhD BEng(hons) EurIng

Overview Why trees? Terminology Operations Implementation

Why trees? Structural relationships Hierarchies Efficient insertion and search Flexibility (moving tree around)

Why trees? Examples Navigating (robot) Files systems Family trees (animals, relationships) Assembles Hierarchies Decision making

Terminology Root (1) Node Leaves Branches Node

Terminology Level 2 Level 1 Position Label Parent (1) child sibling

Terminology Height depth h(0) h(1)d(0) d(1) level (x) = depth(x) + 1

Terminology Finite number of nodes Homogeneous data type Unique path Sub tree is a tree recursive

Terminology Ordered Sibling linearly ordered Siblings in a list Unordered Disorganised No significance between siblings

Terminology Directed Trees Paths in only one direction Down directed trees lack parents Up directed trees lack children Binary Trees Has (max) two children per node Sorted Relationship / values

Terminology Binary tree Left child Right child Number of nodes in a binary tree At least n = 2h – 1 (h = height) Number of leaves in a full binary tree l = (n + 1)/2

Terminology Full binary tree Each node has zero or two children Complete binary tree Full at each level with possible exception of last level Balanced binary tree Left and right sub trees have same number of nodes

Terminology Height of a binary tree k2k What is k? 2 k = n, therefore k = log 2 n

Operations Navigate Balancing Enumerate Search Insert Delete Pruning Grafting

Operations Navigate Travers Breadth first Depth first

Operations Breadth first Create a queue Save all out going nodes to the queue Work through the queue and check each node End when queue is empty

Operations

Depth first Walking the tree Walk Pre-order Parent traversed before child 1, 2, 4, 5, 3 Post-order Child traversed before parent 4, 5, 2, 3, 1 In-order Left tree, then node, then right tree 4, 2, 5, 1,

Operations DepthFirst(n)(pre-ordered) if node == NULL return visit(n) DepthFirst(n.left) DepthFirst(n.right)

Operations DepthFirst(n)(post-order) if n not = NULL return DepthFirst(n.left) DepthFirst(n.right) visit(n) return

Operations

Balancing a tree Adding sorted data to a tree Tree becomes like a list Rotate left Red-black trees

Operations tmp 4 3 n = 3

Operations Red-black tree Colours for nodes All leaves coloured black Root is black If a node is red the both children are black Root from any given node to a leaf contains the same number of black nodes NULL

Operations Insert(n, data) if n = null then n = new(data) else if n.data < data insert(n.left, data) else if n-data > data insert(n.root, data)

Operations Delete 1.Deleting a leaf 2.Deleting a node with one child 3.Deleting a node with two children Case 2 Remove node and replace with child

Operations Select either in order predecessor or successor, r, of n Copy r to n Recursively call delete on r until case 1 or 2 n = 2 and r =

Operations Insert in a red-back tree Add a red node in place of a black leaf Then what happens? The insert can violate the rules!

Operations 1.n is the root node 2.n parent is black 3.n parent and uncle is red 4.n is added left/right or right/left so parent is red and uncle black 5.n is added left/left or right/right so parent is red and uncle black NULL

Operations To fix Colour changes Rotations 1.Colour change red to black 2.OK 3.Colour parent and uncle red, repeat as needed 4.Left rotation 5.Right rotation

Operations B A3 1 2 A 1B 2 3 Right Left

Operations Time complexity Worse case: Search ever branch and every node O(|V| + |E|) Space complexity O(|V|)

Implementation Linked list Label Pointer to parent Array of pointers to children struct { void* pParent; void*pChildren[]; char*strLabel[]; }

Implementation pParent pChildren pLabel pParent pChildren pLabel pParent pChildren pLabel pParent pChildren pLabel pParent pChildren pLabel NULL

Implementation As an array Label Parent N 1 N 2N 3 N 4N 5 LabelParent N 2 N 1 N 5 N 2 N 3 N 1 N 0 N 4N 2

Implementation Binary tree as an array Parent

Implementation Binary tree as an array index

Questions?