Docsity

Binary Trees: Properties, Types, and Conversion to Binary Tree, Study notes of Digital & Analog Electronics

Partial preview of the text

Download Binary Trees: Properties, Types, and Conversion to Binary Tree and more Study notes Digital & Analog Electronics in PDF only on Docsity!

Binary TreesBinary Trees

CSE 204 โ€“ DATA STRUCTURES & ALGORITHMS CSE 204 โ€“ DATA STRUCTURES & ALGORITHMS

General Trees 2

Binary Trees Binary Trees

Binary tree

In computer science, a binary tree is a

tree data structure in which each node has at most two

children. Typically the first node is known as the

parent and the child nodes are called left and right.

Properties of Binary Tree

The number of nodes n in a perfect binary tree can be found using this formula: n = 2h + 1 โˆ’ 1 where h is the height of the tree. The number of nodes n in a complete binary tree is minimum: n = 2h and maximum: n = 2h + 1 โˆ’ 1 where h is the height of the tree. The number of nodes n in a perfect binary tree can also be found using this formula: n = 2L โˆ’ 1 where L is the number of leaf nodes in the tree. The number of leaf nodes n in a perfect binary tree can be found using this formula: n = 2h where h is the height of the tree. The number of NULL links in a Complete Binary Tree of n-node is (n+1). The number of leaf node in a Complete Binary Tree of n-node is UpperBound ( n / 2). For any non-empty binary tree with n 0 leaf nodes and n 2 nodes of degree 2, n 0 = n 2 + 1.[5] n = n0 + n1 + n2 + n4 + n3 + n5 + .... + nB-1 + nB B = n - 1, n = 1 + 1n1 + 2n2 + 3n3 + 4n4 + ... + BnB, NOT include n*

Types of Binary Tree

A rooted binary tree is a rooted tree in which every node has at

most two children.

A full binary tree (sometimes proper binary tree or 2-tree or

strictly binary tree ) is a tree in which every node other than the

leaves has two children.

A perfect binary tree is a full binary tree in which all leaves are at

the same depth or same level. (This is ambiguously also called a

complete binary tree .)

A complete binary tree is a binary tree in which every level, except

possibly the last , is completely filled, and all nodes are as far left as

possible.

Types of Binary Tree

A rooted complete binary tree can be identified with a free magma. A degenerate tree is a tree where for each parent node, there is only one associated child node. This means that in a performance measurement, the tree will behave like a linked list data structure.

Binary Search Tree

In computer science, a binary search tree ( BST ) or ordered binary tree is a node-based binary tree data structure which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees. From the above properties it naturally follows that: Each node (item in the tree) has a distinct key. Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are compared according to their keys rather than any part of their associated records.

General Tree General Tree

General trees are those in which the number of subtrees for any node is not

required to be 0, 1, or 2.

The tree may be highly structured and therefore

have 3 sub trees per node in which case it is called a ternary tree.

However, it is often the case that the number of subtrees for any node may

be variable. Some nodes may have 1 or no subtrees, others may have 3, some

4, or any other combination.

The ternary tree is just a special case of a general tree

General Tree General Tree

A

B C E

F

D

G H

I J

Converting to a Binary Tree Converting to a Binary Tree

To convert a general ordered tree to binary tree, we only need to represent the general tree in left child-sibling way. The result of this representation will be automatically binary tree, if viewed from a different respective. Each node N in the ordered tree corresponds to a node N' in the binary tree; the left child of N' is the node corresponding to the first child of N , and the right child of N' is the node corresponding to N 's next sibling --- that is, the next node in order among the children of the parent of N. This binary tree representation of a general order tree, is sometimes also referred to as a First-Child/Next-Sibling binary tree, or a Doubly-Chained Tree, or a Filial- Heir chain.

Converting to a Binary Tree Converting to a Binary Tree