×

Close

- Data Structure using C - DS
- Note
- 1 Topics
**3649 Views**- 271 Offline Downloads
- Uploaded 1 year ago

Touch here to read

Page-2

Topic:

BINARY TREE DATA STRUCTURES GENERAL DEFINITION : A binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child. A binary tree may also be called a bifurcating arborescence. RECURSIVE DEFINITION : A full binary tree is either • • A single vertex. A graph formed by taking two (full) binary trees, adding a vertex, and adding an edge directed from the new vertex to the root of each binary tree. An extended binary tree is defined as • • the empty set is an extended binary tree if T1 and T2 are extended binary trees, then denote by T1 • T2 the extended binary tree obtained by adding a root r connected to the left to T1 and to the right to T2 by adding edges when these sub-trees are nonempty. Types of BINARY TREES • • A rooted binary tree has a root node and every node has at most two children. A full binary tree (sometimes referred to as a proper or plane binary tree) is a tree in which every node in the tree has either 0 or 2 children. A full binary tree • • A perfect binary tree is a binary tree in which all interior nodes have two children and all leaves have the same depth or same level. An example of a perfect binary tree is the ancestry chart of a person to a given depth, as each person has exactly two biological parents. In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes at the last level h. An alternative definition is a perfect tree whose rightmost leaves (perhaps all) have been removed. A complete binary tree

• • In the infinite complete binary tree, every node has two children. The set of all nodes is countably infinite, but the set of all infinite paths from the root is uncountable, having the cardinality of the continuum. These paths correspond by an order-preserving bijection to the points of the Cantor set, or to the set of positive irrational numbers. A balanced binary tree has the minimum possible maximum height (a.k.a. depth) for the leaf nodes, because for any given number of leaf nodes, the leaf nodes are placed at the greatest height possible.[clarification needed] h 0: Balanced ABCDE / \ 1: ABCD E / \ 2: AB CD / \ / \ 3: A B C D 4: • Unbalanced, h = (n + 1)/2 - 1 ABCDE / \ ABCD E / \ ABC D / \ AB C / \ A B A degenerate (or pathological) tree is where each parent node has only one associated child node.This means that performance-wise, the tree will behave like a linked list data structure. Properties of binary trees • • The number of nodes in a full binary tree, is at least and at most , where is the height of the tree. A tree consisting of only a root node has a height of 0. The number of leaf nodes in a perfect binary tree, is because the number of non-leaf (a.k.a. internal) • • nodes . This means that a perfect binary tree with leaves has nodes. In a balanced full binary tree, • • In a perfect full binary tree, thus . The maximum possible number of null links (i.e., absent children of the nodes) in a complete binary tree of n nodes is (n+1), where only 1 node exists in bottom-most level to the far left. • • The number of internal nodes in a complete binary tree of n nodes is . For any non-empty binary tree with n0 leaf nodes and n2 nodes of degree 2, n0 = n2 + 1. Common Operations There are a variety of different operations that can be performed on binary trees. Insertion Nodes can be inserted into binary trees in between two other nodes or added after a leaf node. In binary trees, a node that is inserted is specified as to which child it is. • Leaf nodes To add a new node after leaf node A, A assigns the new node as one of its children and the new node assigns node A as its parent. • Internal nodes

The process of inserting a node into a binary tree Insertion on internal nodes is slightly more complex than on leaf nodes. Say that the internal node is node A and that node B is the child of A. (If the insertion is to insert a right child, then B is the right child of A, and similarly with a left child insertion.) A assigns its child to the new node and the new node assigns its parent to A. Then the new node assigns its child to B and B assigns its parent as the new node. Deletion Deletion is the process whereby a node is removed from the tree. Only certain nodes in a binary tree can be removed unambiguously.[27] • Node with zero or one children The process of deleting an internal node in a binary tree Suppose that the node to delete is node A. If A has no children, deletion is accomplished by setting the child of A's parent to null. If A has one child, set the parent of A's child to A's parent and set the child of A's parent to A's child. • Node with two children In a binary tree, a node with two children cannot be deleted unambiguously.[27] However, in certain binary trees (including binary search trees) these nodes can be deleted, though with a rearrangement of the tree structure. Traversal Pre-order, in-order, and post-order traversal visit each node in a tree by recursively visiting each node in the left and right subtrees of the root. • Depth-first order In depth-first order, we always attempt to visit the node farthest from the root node that we can, but with the caveat that it must be a child of a node we have already visited. Unlike a depth-first search on graphs, there is no need to remember all the nodes we have visited, because a tree cannot contain cycles. Pre-order is a special case of this. See depth-first search for more information. • Breadth-first order Contrasting with depth-first order is breadth-first order, which always attempts to visit the node closest to the root that it has not already visited. Methods for storing binary trees Binary trees can be constructed from programming language primitives in several ways. Nodes and references In a language with records and references, binary trees are typically constructed by having a tree node structure which contains some data and references to its left child and its right child. Sometimes it also contains a reference to its unique parent. If a node has fewer than two children, some of the child pointers may be set to a special null value, or to a special sentinel node. This method of storing binary trees wastes a fair bit of memory, as the pointers will be null (or point to the sentinel) more than half the time; a more conservative representation alternative is threaded binary tree.[25] In languages with tagged unions such as ML, a tree node is often a tagged union of two types of nodes, one of which is a 3-tuple of data, left child, and right child, and the other of which is a "leaf" node, which contains

no data and functions much like the null value in a language with pointers. For example, the following line of code in OCaml (an ML dialect) defines a binary tree that stores a character in each node. type chr_tree = Empty | Node of char * chr_tree * chr_tree Arrays Binary trees can also be stored in breadth-first order as an implicit data structure in arrays, and if the tree is a complete binary tree, this method wastes no space. In this compact arrangement, if a node has an index i, its children are found at indices (for the left child) and (for the right), while its parent (if any) is found at index (assuming the root has index zero). This method benefits from more compact storage and better locality of reference, particularly during a preorder traversal. However, it is expensive to grow and wastes space proportional to 2h - n for a tree of depth h with n nodes. This method of storage is often used for binary heaps. No space is wasted because nodes are added in breadth-first order. Binary Expression tree A binary expression tree is a specific kind of a binary tree used to represent expressions. Two common types of expressions that a binary expression tree can represent are algebraic and boolean. These trees can represent expressions that contain both unary and binary operators. Each node of a binary tree, and hence of a binary expression tree, has zero, one, or two children. This restricted structure simplifies the processing of expression trees. • Algebraic expressions Binary algebraic expression tree equivalent to ((5 + z) / -8) * (4 ^ 2) Algebraic expression trees represent expressions that contain numbers, variables, and unary and binary operators. Some of the common operators are × (multiplication), ÷ (division), + (addition), − (subtraction), ^ (exponentiation), and - (negation). The operators are contained in the internal nodes of the tree, with the numbers and variables in the leaf nodes. The nodes of binary operators have two child nodes, and the unary operators have one child node. • Boolean expressions

## Leave your Comments