Trees & Graphs

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

BFS vs. DFS

*Choose BFS --> when your target node is most likely to be in the UPPER levels of the tree *Choose DFS --> when your target node is likely to be in the LOWER levels of the tree

Tree Implementation (Object-Oriented)

*Define a class for the common parts of a node *Define one or more subclasses for the data held by a node public abstract class Node { private Node[] children; public Node( Node[] children ){ this.children = children; } public int getNumChildren(){ return children.length; } public Node getChild( int index ){ return children[ index ]; } } public class IntNode extends Node { private int value; public IntNode( Node[] children, int value ){ super( children ); this.value = value; } public int getValue(){ return value; } }

Binary Search Tree Implementation

*Each node in a tree is the root of a subtree beginning at that node (conducive to recursion) Node findNode ( Node root, int value) { if( root == null ) return null; int currval = root.getValue(); if( currval == value ) return root; if( currval < value) { return findNode (root.getRight(), value); } else { //currval > value return findNode(root.getLeft(), value); } }

Binary Search Trees (Run Time & Properties)

*Lookup = O( log(n) ) -- eliminate half the remaining nodes from your search on each iteration by choosing to follow the left or right subtree (only if you can guarantee that the number of nodes remaining to be searched will be halved on each iteration *Insertion/Deletion = O ( log(n) ) *Smallest Element - obtained by following all left children *Largest element - obtained by following all right children *NOTE: Many tree operations can be implemented recursively. The recursive implementation may not be the most efficient, but it's usually the best place to start

Depth-First Search (DFS)

*Start with the root *Follow one branch of the tree down as many levels as possible until the target node is found or the end is reached *When the search can't go down any farther, it is continued at the nearest ancestor with unexplored children *LOWER MEMORY requirements than BFS (not necessary to store all child pointers at each level)

Breadth-First Search (BFS)

*Start with the root *Move left to right across the second level, then move left to right across the third level, ... *Continue the search until you have examined all nodes or you find the node you are searching for *Uses ADDITIONAL MEMORY because it is necessary to track the child nodes for all nodes on a given level while searching that level

Descendant

All the nodes that can be reached by following a path of child nodes from a particular node are the DESCENDANTS of that node

Graph Implementation

Best representation is often determined by the algorithm being implemented *Adjacency List = dynamic data structure for each node includes a list of references to other nodes with which the node shares edges (analogous to child references) *Adjacency Matrix = square matrix with dimension equal to the number of nodes. The matrix element i, j represents the number of edges extending from node i to node j *Graph traversals are more complex due to the possibility of cycles

Graphs

More general and more complex than trees *Vertices (graph nodes) - can have multiple "parents", possibly creating a loop (a cycle) *Edges - may contain more information than just a pointer. Serve as links between nodes, as well as the nodes themselves, may have values or weights *Directed Graph = graph with ONE-WAY edges *Undirected Graph = graph with only TWO-WAY edges *Graphs model real-world problems that are difficult to model with other data structures

Inorder

Perform the operation first on... 1) left descendants 2) node itself 3) right descendants *Subtree is visited first, then the node itself, and then the node's right subtree

Postorder

Performs the operation first on... 1) left descendants 2) right descendants 3) node itself *Node is always visited AFTER all its children

Preorder

Performs the operation first on... 1) node itself 2) left descendants 3) right descendants *Node is always visited BEFORE any of its children

Root

Top level node *only node from which you have a path to every other node

Binary Search Trees (BST)

Tree used to store sorted or ordered data *Value held by a node's LEFT child is LESS THAN or equal to its own value *Value held by a node's RIGHT child is GREATER THAN or equal to its value *Data in BST is sorted by value *NOTE: when interviewers say "tree", they often mean a binary search tree

Ancestor

an ANCESTOR of a node is any other node for which the node is a descendant

Binary Tree

each node has no more than two children, referred to as left and right *NOTE: When interviewers say "tree", they often mean a binary tree

Tree

made up of nodes (data elements) with zero, one, or several references (or pointers) to other nodes. *Each node has only one other node referencing it

Child

node is the CHILD of any node that points to it

Parent

node that points to other nodes is the PARENT of those nodes *Every node except the root has one parent

Binary Tree Implementation

public class Node { private Node left; private Node right; private int value; public Node( Node left, Node right, int value ){ this.left = left; this.right = right; this.value = value; } public Node getLeft() { return left; } public Node getRight() { return right; } public int getValue() { return value; } }

Leaves

the LEAVES are nodes that do not have any children

Heap

trees (usually binary search trees) where (in a max-heap) each child of a node has a value less than or equal to the node's own value *Min-heap - each child is greater than or equal to its parent *Find max value = constant time (return root) *Insertion/Deletion = O( log(n) ) *Lookup = O( n ) *"Hospital Emergency Room Example" *NOTE: If extracting the max value needs to be fast, use a heap

Traversal

visit every node in the tree *often used to perform some operation on each node in the tree *Depth-First Traversals for Binary Trees: 1) Preorder 2) Inorder 3) Postorder *NOTE: If you're asked to implement a traversal, recursion is a good way to start thinking about the problem


Set pelajaran terkait

Unit 1 Nutrition (Food is more than something to Eat)

View Set

Mental Health Online Practice 2019 B with NGN

View Set

BATECH 165 Ch 3 - Analyzing Data with Pie Charts, Line Charts, and What-If Analysis Tools

View Set

Chapter 19: Postoperative Care Lewis: Medical-Surgical Nursing, 10th Edition

View Set

BMB 251 Lecture Points 7 Chromatin Structure and Function

View Set