lecture 17 non-linear data structures richard gesick
TRANSCRIPT
![Page 1: Lecture 17 Non-Linear data structures Richard Gesick](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/1.jpg)
Lecture 17
Non-Linear data structures
Richard Gesick
![Page 2: Lecture 17 Non-Linear data structures Richard Gesick](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/8.jpg)
A BST
14-8
![Page 9: Lecture 17 Non-Linear data structures Richard Gesick](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/10.jpg)
Tree Traversal
•Tree traversal schemes include •Preorder traversal •Inorder traversal •Postorder traversal
14-10
![Page 11: Lecture 17 Non-Linear data structures Richard Gesick](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/12.jpg)
A tree struc- ture?
14-12
![Page 13: Lecture 17 Non-Linear data structures Richard Gesick](https://reader036.vdocument.in/reader036/viewer/2022062321/56649e705503460f94b6e77f/html5/thumbnails/13.jpg)
A graph?
14-13