1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018
General Overview of Data Structures 2
Introduction to trees 3 Tree: Important non-linear data structure Non-linear thinking Organizational relationship: hierarchical A tree is an abstract data type that stores elements hierarchically edge/link root parent With the exception of the top element (called root of the tree), each element in a tree has a parent element and zero or more children elements. A directed edge connects the parent to the child child child child Nodes with the same parent are called siblings A leaf has no children
Introduction to trees 4 Tree: Formal definition A tree T is a set of nodes storing elements such that the nodes have a parent-child relationship that satisfies the following properties: If T is not empty, it has a special node, called the root of T, that has no parent. Each node v of T different from the root has a unique parent node w. Every node with parent w is a child of w.
Introduction to trees 5 Terminology The depth of a node is the number of edges from the root to the node The height of a node is the number of edges from the node to the deepest leaf The height of a tree is a height of the root Other specific definitions Height: Sometimes, it is worth to consider the number of nodes rather than the number of edges (links) Depth: is generally related to the index level of the node in the tree
Introduction to trees 6 Node Height Depth A 3 0 B 1 1 C 0 1 D 1 1 E 2 1 F 0 2 G 0 2 H 0 2 I 0 2 J 1 2 K 0 3
Introduction to trees 7 Terminology The depth of a node is the number of edges from the root to the node The height of a node is the number of edges from the node to the deepest leaf The height of a tree is a height of the root A subtree is a section of the tree that is a complete tree in its own right, except that its root has a parent. A binary tree is a tree in which no node can have more than two children called left and right
Introduction to trees 8 Tree Binary tree
Introduction to trees 9 Terminology A binary tree is a tree in which no node can have more than two children called left and right A full binary tree is a binary tree in which each node has exactly zero or two children A complete binary tree is a binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right
Introduction to trees 10 Full binary tree Complete binary tree The height h of a complete binary tree with N nodes is at most O(log N).
Binary Tree Traversal Algorithms 11 depth-first traversal Algorithms PreOrder traversal visit the parent first and then left and right children InOrder traversal visit the left child, then the parent and the right child PostOrder traversal visit left child, then the right child and then the parent
Binary Tree Pre-order traversal 12 1. Visit parent 2. Visit left child 3. Visit right child Preorder traversal: A, B, D, H, I, E, C, F, G, J
Binary tree In-order traversal 13 1. Visit left child 2. Visit parent 3. Visit right child InOrder traversal: H, D, I, B, E, A, F, C, J, G
Binary Tree Post-order traversal 14 1. Visit left child 2. Visit right child 3. Visit parent PostOrder traversal: H, I, D, E, B, F, J, G, C, A
Binary Search Trees Basic idea 15 Binary Search Tree A binary tree that satisfies the search order property: For every node X in the tree, the values of all entries in the left subtree are smaller than the value of the entry in X, < < the values of all entries in the right subtree are larger than the value of the entry in X < < < < < <
Binary Search Tree Basic idea 16 A binary search tree A non binary search tree
Binary Search Trees Basic idea 17 Benefits of the search order property All the items in the tree can be ordered consistently For instance, an in-order traversal yields the items in sorted order 1. Visit left child 2. Visit parent 3. Visit right child In-order traversal: 1, 3, 4, 6, 7, 8, 10, 13, 14
Binary Search Trees - Operations 18 contains / find / lookup / search public boolean contains(t item) insert / add public void insert(t item) public void add(t item) isempty public boolean isempty() remove / delete public void remove(t item)
BST - Illustrative Example for contains operation 19 contains(76) :?
BST - Illustrative Example for contains operation 20 contains(76) :? True FOUND! Successful Search
BST - Illustrative Example for contains operation 21 contains(35) :? False null UNSUCCESSFUL search!
BST - Illustrative Example for insert operation 22 insert(89) 89
Conclusions 23 Main Advantages of Trees and Binary Search Trees (BST) Trees reflect structural and hierarchical relationships in the data BST provide an efficient insertion and searching Trees are very flexible data, allowing to move subtrees around with minimum effort