+ All Categories
Home > Documents > binarytree

binarytree

Date post: 13-Sep-2015
Category:
Upload: rajendran
View: 2 times
Download: 0 times
Share this document with a friend
Description:
binarytree
16
Data structures Binary Tree
Transcript
  • Data structures

    Binary Tree

  • Binary TreesA structure containing nodes with more than one self-referenced field. A binary tree is made of nodes, where each node contains a "left" reference, a "right" reference, and a data element. The topmost node in the tree is called the root.

    Any node can have at most two branches, i.e.,there is no node with degree greater than two. For binary trees we distinguish between the subtree on the left and on the right, whereas for trees the order of the subtree was irrelevant. Also a binary tree may have zero nodes.

    Definition: A binary tree is a finite set of nodes which is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree.

  • Binary Trees

    Every node (excluding a root) in a tree is connected by a directed edge from exactly one other node. This node is called a parent. On the other hand, each node can be connected to arbitrary number of nodes, called children.

    leaves or external nodes Nodes with no children are called leaves, or external nodes. Nodes which are not leaves are called internal nodes.

    Siblings Nodes with the same parent are called siblings.

  • 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 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.

  • Binary TreesBinary trees are characterized by the fact that any node can have at most two branchesDefinition (recursive):A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtreeThus the left subtree and the right subtree are distinguished

    Any tree can be transformed into binary treeby left child-right sibling representation

  • Binary TreesThe abstract data type of binary tree

  • Binary TreesTwo special kinds of binary trees: (a) skewed tree, (b) complete binary treeThe all leaf nodes of these trees are on two adjacent levels

  • Binary Trees (4/9)Properties of binary treesLemma 5.1 [Maximum number of nodes]:The maximum number of nodes on level i of a binary tree is 2i-1, i 1.The maximum number of nodes in a binary tree of depth k is 2k-1, k1.Lemma 5.2 [Relation between number of leaf nodes and degree-2 nodes]:For any nonempty binary tree, T, if n0 is the number of leaf nodes and n2 is the number of nodes of degree 2, then n0 = n2 + 1.These lemmas allow us to define full and complete binary trees

  • Binary Trees (5/9)Definition:A full binary tree of depth k is a binary tree of death k having 2k-1 nodes, k 0.A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k.From Lemma 5.1, the height of a complete binary tree with n nodes is log2(n+1)

  • Binary TreesBinary tree representations (using array)Lemma 5.3: If a complete binary tree with n nodes is represented sequentially, then for any node with index i, 1 i n, we have parent(i) is at i /2 if i 1. If i = 1, i is at the root and has no parent. LeftChild(i) is at 2i if 2i n. If 2i n, then i has no left child. RightChild(i) is at 2i+1 if 2i+1 n. If 2i +1 n, then i has no left child

  • Binary TreesBinary tree representations (using array)Waste spaces: in the worst case, a skewed tree of depth k requires 2k-1 spaces. Of these, only k spaces will be occupiedInsertion or deletion of nodes from the middle of a tree requires the movement of potentially many nodes to reflect the change in the level of these nodes

  • Binary TreesBinary tree representations (using link)

  • Binary TreesBinary tree representations (using link)

  • . A complete binary tree is very special tree, it provides the best possible ratio between the number of nodes and the height.

    The height h of a complete binary tree with N nodes is at most O(log N). We can easily prove this by counting nodes on each level, starting with the root, assuming that each level has the maximum number of nodes.

    n = 1 + 2 + 4 + ... + 2h-1 + 2h = 2h+1 - 1 Solving this with respect to h, we obtain h = O(log n)

  • Algorithm structure BTREE declare CREATE( ) --> btree ISMTBT(btree,item,btree) --> boolean MAKEBT(btree,item,btree) --> btree LCHILD(btree) --> btree DATA(btree) --> item RCHILD(btree) --> btree for all p,r in btree, d in item let ISMTBT(CREATE)::=true ISMTBT(MAKEBT(p,d,r))::=false LCHILD(MAKEBT(p,d,r))::=p; LCHILD(CREATE)::=error DATA(MAKEBT(p,d,r))::d; DATA(CREATE)::=error RCHILD(MAKEBT(p,d,r))::=r; RCHILD(CREATE)::=error end end BTREE

  • Advantages of trees

    Trees are so useful and frequently used, because they have some very serious advantages:

    Trees reflect structural relationships in the data. Trees are used to represent hierarchies. Trees provide an efficient insertion and searching. Trees are very flexible data, allowing to move subtrees around with minimum effort .


Recommended