Binary Search Tree A binary tree consists of nodes and branches. A node is a place where data is stored, There is one special node the root which is the starting point of the tree. Branches connect the nodes to each other. A branch is either a left branch or a right branch. Accordingly a node is said to have a left child and a right child. A node without children is called a leaf. Non-leaf nodes are called internal nodes. There is a single path from any node to any other node in the tree. Another useful attribute of a node is its distance from the root, called its depth. Distance is counted in number of branches from the root. The depth of the root is zero. Nodes at the same depth are said to be at the same level, with the root being at level zero. The height of a tree is the maximum level (or depth) at which there is a node. Alternatively, the height of a tree is the distance of the farthest leaf from the root. Relative positions of nodes A binary tree is defined by the relative positions of the data in its nodes, and the tree as a whole carries a meaning that would change if the relative positions of the data in the tree were to change. Maximum number of nodes in a binary tree Let a binary tree contain Nmax, the maximum number of nodes possible for its height h. Then h= log(Nmax + 1) –1 Recursive definition of a binary tree A binary tree is either empty or consists of a special node called root that has a left subtree and a right subtree that are mutually disjoint binary trees. Recursive definition of number of nodes The number of nodes in an empty binary tree is zero. Otherwise, the nuber of nodes is one plus the number of nodes in the left and right subtrees of the root. Recursive definition of height The height of an empty binary tree is – 1. Otherwise, the height is one plus the maximum of the heights of the left and right subtrees of the root. Inorder recursive traversal of a binary tree T First recursively traverse the Left subtree of T, then Visit the root of T, then recursively traverse the Right subtree of T. Binary Search Tree A binary search tree is a binary tree whose entries can be arranged in order. For every node x in the tree, the value of the entry ay x is greater than the values of all the entries in the left subtree of x, and smaller than the values of all the entries in the right subtree Full, complete, and balanced binary tree In a full binary tree of height h, all nodes that are at a level less than h have two children each. Each node in a full binary tree has left and right subtrees of the same height. Among binary trees of height h, a full binary tree has as many leaves as possible, and they all are at level h. In short, a full binary tree has no missing nodes. A complete binary tree of height h is a binary tree that is full down to level h – 1, with level h filled in with from left to right. More formally, a binary tree of height h is complete if 1. All nodes at level h-2 and above have two children each 2. When a node at level h-1 has children, all nodes to its left at the same level have two children each 3. When a node at level h-1 has one child, it is a left child. Note that a full binary tree is complete. A binary tree is height balanced or simply balanced if the height of any node’s right subtree differs from the height of the node’s left tree by no more than 1. A complete binary tree is balanced. Balanced binary tree search A search of the balanced binary tree is equivalent to a binary search of an ordered list. In both cases, each check eliminates half of the remaining items. Hence searching is O(logN). Skewed tree search The worst possible binary tree structure is one that is completely skewed either to the left (no node has a right child) or to the right (no node has a left child). This happens when the values to be inserted arrive in descending or ascending order, respectively. Searching in such a tree degenerates to a sequential linear search of O(N). Binary search tree expectations Insertions and deletions should be faster than O(N), and searching should not be slower than O(logN). Balanced Binary tree The objective is to keep the structure of the binary tree always balanced so that the height never exceeds O(logN). After every insert or delete we must ensure that the tree does not become lopsided. Building a binary search tree (Recursive) If the root is null Replace the empty tree with a new tree with the item at the root Else if tem.data equals root.data Item already there === Error Else if item.data is less than root.data Recursively insert the item in the left subtree Else Recursively insert the item in the right subtree Note that all this says is start at the root (or create a root if empty tee) From the above place decide to go either left or right until a leaf is encountered Repeat the previous step Insert the item at the terminating leaf Searching a binary Tree (Recursive) If root is null The item is not there Compare targer.data with root.data If they are equal Target has been found and return root.data Else if target.data is less than root.data Return the result of searching the left subtree Else Return the result of searching the right subtree Note that all this says is start at the root From the above place decide to go either left or right until the item is found or a leaf is encountered Repeat the previous step Deletion of an item from a binary tree (Recursive) If the root is null The item is not in the tree Compare item.data with root.data If item.data is less than root.data Return the result of deleting from the left subtree Else if item.data is greater than root.data Return the result of deleting from the right subtree Else == item is in the root Store root.data If the root has no children Set the parent of the root to reference null Else if root has one child Set the parent of the root to reference that child Else == find the inorder succcessor If the right child has no left child it is the inorder successor Set the parent of the root to reference the right child Else Find the leftmost node in the left subtree of the right child Copy the data of this leftmost node into root.data and remove this leftmost node by setting its parent to reference its right child Note that all this says is first find the node to delete. If the indicated node has no children then simply change the appropriate link for that node’s parent to null. If the indicated node has only one child then simply bypass the indicated node and connect the node’s parent directly to the node’s child. If the indicated node has two children then we have to be careful but we can make use of a binary tree property. The binary tree is arranged in order by ascending keys. For each node the node with the next highest key is called the in order successor. So to delete a node with two children we simply replace this node with its in order successor. If the right child has no left child it is the in order successor of the node to be deleted. Otherwise we have to find the leftmost node in the left subtree of the right child.