In Order Traversal Algorithm In Order traversal algorithm

  • Slides: 9
Download presentation
In. Order Traversal Algorithm // In. Order traversal algorithm in. Order(Tree. Node<T> n) {

In. Order Traversal Algorithm // In. Order traversal algorithm in. Order(Tree. Node<T> n) { if (n != null) { in. Order(n. get. Left()); visit(n) in. Order(n. get. Right()); } }

Examples l Iterative version of in-order traversal l Option 1: using Stack Option 2:

Examples l Iterative version of in-order traversal l Option 1: using Stack Option 2: with references to parents in Tree. Nodes Iterative version of height() method

Binary Tree Implementation l The binary tree ADT can be implemented using a number

Binary Tree Implementation l The binary tree ADT can be implemented using a number of data structures l l l Reference structures (similar to linked lists), as we have seen Arrays – either simulating references or complete binary trees allow for a special very memory efficient array representation (called heaps) We will look at 3 applications of binary trees l l l Binary search trees (references) Red-black trees (references) Heaps (arrays)

Problem: Design a data structure for storing data with keys l Consider maintaining data

Problem: Design a data structure for storing data with keys l Consider maintaining data in some manner (data structure) l l The data is to be frequently searched on the search key e. g. a dictionary, records in database Possible solutions might be: l A sorted array (by the keys) l l l Access in O(log n) using binary search Insertion and deletion in linear time An sorted linked list l Access, insertion and deletion in linear time

Dictionary Operations l The data structure should be able to perform all these operations

Dictionary Operations l The data structure should be able to perform all these operations efficiently l l l Create an empty dictionary Insert Delete Look up (by the key) The insert, delete and look up operations should be performed in O(log n) time Is it possible?

Data with keys l l For simplicity we will assume that keys are of

Data with keys l l For simplicity we will assume that keys are of type long, i. e. , they can be compared with operators <, >, <=, ==, etc. All items stored in a container will be derived from Keyed. Item. public class Keyed. Item { private long key; public Keyed. Item(long k) { key=k; } public get. Key() { return key; } }

Binary Search Trees (BSTs) l A binary search tree is a binary tree with

Binary Search Trees (BSTs) l A binary search tree is a binary tree with a special property l For all nodes v in the tree: l l All the nodes in the left subtree of v contain items less than the item in v and All the nodes in the right subtree of v contain items greater than or equal to the item in v

BST Example 17 13 9 27 16 11 20 39

BST Example 17 13 9 27 16 11 20 39

BST In. Order Traversal in. Order(n. left. Child) 5 visit(n) 17 in. Order(n. right.

BST In. Order Traversal in. Order(n. left. Child) 5 visit(n) 17 in. Order(n. right. Child) 3 1 9 13 in. Order(l) visit in. Order(r) 4 16 in. Order(l) visit in. Order(r) 2 11 in. Order(l) visit in. Order(r) 7 6 20 in. Order(l) visit in. Order(r) 27 in. Order(l) visit in. Order(r) 8 39 in. Order(l) visit in. Order(r) Conclusion: in-Order traversal of BST visit elements in order.