lecture 17 non-linear data structures richard gesick

13
Lecture 17 Non-Linear data structures Richard Gesick

Upload: phoebe-willis

Post on 29-Dec-2015

227 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Lecture 17 Non-Linear data structures Richard Gesick

Lecture 17

Non-Linear data structures

Richard Gesick

Page 2: Lecture 17 Non-Linear data structures Richard Gesick

Non-Linear data structures2 primary types:•Trees•Graphs•All trees are graphs, but not all graphs are trees•Recursion is useful and is the easiest way to process them. It helps keep track of what's been processed and what remains

14-2

Page 3: Lecture 17 Non-Linear data structures Richard Gesick

Graphs•Graphs can have multiple references in and multiple references out (whereas tree node only has one reference in)•Graphs can be directed or undirected and cyclic or acyclic

14-3

Page 4: Lecture 17 Non-Linear data structures Richard Gesick

Trees• Single parent• 0 or more children• A node with no children is called a "leaf" • The topmost node is called the "root"• N-ary trees• Binary trees

14-4

Page 5: Lecture 17 Non-Linear data structures Richard Gesick

N-ary TreesThe best example of an n-ary tree is your computer’s directory system. It has a single starting point and then 0 or more branches.

14-5

Page 6: Lecture 17 Non-Linear data structures Richard Gesick

Binary Trees and Binary Search Trees•Binary search trees allow for fast insertion and removal of elements •They are specially designed for fast searching •A binary tree consists of two nodes, each of which has two child nodes• All nodes in a binary search tree fulfill the property that:

•Descendants to the left have smaller data values than the node data value •Descendants to the right have larger data values than the node data value

14-6

Page 7: Lecture 17 Non-Linear data structures Richard Gesick

BST Tree Nodes

•All nodes in a binary search tree fulfill the property that: •Descendants to the left have smaller data values than the node data value •Descendants to the right have larger data values than the node data value

14-7

Page 8: Lecture 17 Non-Linear data structures Richard Gesick

A BST

14-8

Page 9: Lecture 17 Non-Linear data structures Richard Gesick

BST

•Balanced tree: each node has approximately as many descendants on the left as on the right •If a binary search tree is balanced, then adding an element takes O(log(n)) time •If the tree is unbalanced, insertion can be slow

•Perhaps as slow as insertion into a linked list

14-9

Page 10: Lecture 17 Non-Linear data structures Richard Gesick

Tree Traversal

•Tree traversal schemes include •Preorder traversal •Inorder traversal •Postorder traversal

14-10

Page 11: Lecture 17 Non-Linear data structures Richard Gesick

Recursive Processing Example

void PrintTree(Node current) { if (current != null) { Console.WriteLine(current.data); PrintTree(current.left); PrintTree(current.right); } }

14-11

Page 12: Lecture 17 Non-Linear data structures Richard Gesick

A tree struc- ture?

14-12

Page 13: Lecture 17 Non-Linear data structures Richard Gesick

A graph?

14-13