### Binary Search Trees

to describe relationships among tree nodes with the obvious interpretations. The tree was created by a recursive descent parser, which built the tree in a bottom up manner (i.e., from leaf nodes to An easy example of insertion is binary search trees. How many leaf nodes are there in a complete binary tree of height h?. These are obvious base cases for recursive algorithms. A complete binary tree is full binary tree in which all leaves have the same depth and all internal A binary search tree (BST) is a binary tree that satisfies the binary search tree To obtain the recurrence relation for n > 0, suppose the traversal is. The reason why I still decided to produce such a trivial page is that I will later on write a series of articles focusing on binary search tree in.

Classes do not meet June 29 or June The midterm exam is tenatively scheduled for Thursday, July 2, Trees A tree is a generalization of a linked list. A linked list node can be thought of as having a single successor or child, the node that follows it. The last node in the list has no children. We can extend this concept to allow a node to have many children. A program examining such a tree can decide which child it wants to follow based on the information in the node, or may choose to follow all the children.

Tree nodes that have no children are called leaf nodes. Tree nodes that are not leaves are called internal nodes. The number of children a node has it is degree. The "top" node in a tree, the one leading eventually to all of the other nodes, is called the root node. The depth of a node x is the number of nodes between the root and x. The root is said to be at depth 0; any children of the root are at depth 1, etc. The height of a tree is the maximum depth of any node. For convenience, the height of an empty tree is defined as All the nodes with the same depth d are in a set called level d.

Often, we will use familial relationships e. Similarly, tree analogies will be used e. In lecture 7we saw an example of such a data structure: The tree was created by a recursive descent parser, which built the tree in a bottom up manner i. Here's an example of an ordinary tree: How can we represent trees? If we know the maximum degree number of children of any internal node, we can do something similar to a linked list, with an array of pointers to the next node, instead of just one, e.

If we don't know the maximum degree, then we can use a linked list of siblings, each of which has a link to a linked list of children: It turns out that this kind of node is a very useful general list processing structure; see the computer language LISP for more information. For example, we can represent the tree: Well, it all depends on the nature of the values of keys and how they are related to the concept of successors to a node.

That is, until we know what a key means and why we want to stick it into a tree, it doesn't make sense to talk about how to insert it into a tree. An easy example of insertion is binary search trees. Binary Trees A binary tree is a tree where the maximum degree of any internal node is 2. Thus, a node may have 0, 1, or 2 children. Here is a binary tree: What are the leaves? A special case of a binary tree is a complete binary tree. Add both left and right children into the queue if the current nodehas children.

We will print the value of each node, level by level, with our queuehelper. The subtree 7—5—8—6 needs to be on the right side, and the subtree 2—1—3 needs to be on the left.

B is the only correct option. It satisfies the Binary Search Tree property. C has one problem: It needs to be on the left side of the root because it is smaller than 5.

What will we see here? We will insert new nodes, search for a value, delete nodes, and the balance of the tree. The first thing we need to know is if 50 is the root of our tree. We can now start inserting node by node. Node with value 50 has a left child Since 4 is smaller than 21, insert it on the left side of this node.

Since 32 is greater than 21, insert 32 on the right side of this node. Node with value 50 has a right child Since is greater than 76, insert on the right side of this node. Since 64 is smaller than 76, insert 64 on the left side of this node. Since 52 is smaller than 76, node with value 76 has a left child Do you notice a pattern here? Is the new node value greater or smaller than the current node? If the value of the new node is greater than the current node, go to the right subtree.

If the value of the new node is smaller than the current node, go to the left subtree. We did not handle special cases here. When the value of a new node is equal to the current value of the node, use rule number 3.

Consider inserting equal values to the left side of the subtree.

It seems very simple. The powerful part of this algorithm is the recursion part, which is on line 9 and line Lines 11 and 15 are the ones that do the insertion for each child. For a given value integer numberwe will say if our Binary Search Tree does or does not have that value.

An important item to note is how we defined the tree insertion algorithm. First we have our root node. All the left subtree nodes will have smaller values than the root node. And all the right subtree nodes will have values greater than the root node.

Imagine that we have this tree. Now we want to know if we have a node based on value We start with the root node as our current node. Is the given value smaller than the current node value?

If yes, then we will search for it on the left subtree. Is the given value greater than the current node value? If yes, then we will search for it on the right subtree.

## Introduction

If rules 1 and 2 are both false, we can compare the current node value and the given value if they are equal. Lines 8 and 9 fall under rule 1. Lines 10 and 11 fall under rule 2.

Line 13 falls under rule 3. How do we test it? And now we will insert many new nodes. Yeah, it works for these given values! Our search is done. For a given value, we need to remove the node with this value.

Imagine the following scenarios for this node: A node with no children leaf node. If the node we want to delete has no children, we simply delete it.

A node with just one child left or right child. In this case, our algorithm needs to make the parent of the node point to the child node. If the node is the left child, we make the parent of the left child point to the child. If the node is the right child of its parent, we make the parent of the right child point to the child.

This traversal visits nodes by levels from top to bottom and from left to right. As an example consider the following tree and its four traversals: PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3 InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11 PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8 LevelOrder - 8, 5, 4, 9, 7, 11, 1, 12, 3, 2 In the next picture we demonstarte the order of node visitation.

Number 1 denote the first node in a particular traversal and 7 denote the last node. These common traversals can be represented as a single algorithm by assuming that we visit each node three times. An Euler tour is a walk around the binary tree where each edge is treated as a wall, which you cannot cross. In this walk each node will be visited either on the left, or under the below, or on the right.

### Everything you need to know about tree data structures

The Euler tour in which we visit nodes on the left produces a preorder traversal. When we visit nodes from the below, we get an inorder traversal. And when we visit nodes on the right, we get a postorder traversal. The basic idea behind this data structure is to have such a storing repository that provides the efficient way of data sorting, searching and retriving.

A BST is a binary tree where nodes are ordered in the following way: Because both the left and right subtrees of a BST are again search trees; the above definition is recursively applied to all internal nodes: In order to support the binary search tree property, we require that data stored in each node is Comparable: We start at the root and recursively go down the tree searching for a location in a BST to insert a new node.

If the element to be inserted is already in the tree, we are done we do not insert duplicates. The new node will always replace a NULL reference. Given a sequence of numbers: Searching Searching in a BST always starts at the root. We compare a data stored at the root with the key we are searching for let us call it as toSearch. If the node does not contain the key we proceed either to the left or right child depending upon comparison. If the result of comparison is negative we go to the left child, otherwise - to the right child.