Docsity

Binary Tree Traversal Using Recursive Algorithm, Slides of Data Structures and Algorithms

Partial preview of the text

Download Binary Tree Traversal Using Recursive Algorithm and more Slides Data Structures and Algorithms in PDF only on Docsity!

Tree

Unit 6

So far we discussed Linear data structures like stack

Tree

  • A tree is an abstract model of a hierarchical structure that consists of nodes with a parent-child relationship. - Tree is a sequence of nodes - There is a starting node known as a root node - Every node other than the root has a parent node. - Nodes may have any number of children

Some Key Terms:

  • Root − Node at the top of the tree is called root.
  • Parent − Any node except root node has one edge upward to a node called parent.
  • Child − Node below a given node connected by its edge downward is called its child node.
  • Sibling – Child of same node are called siblings
  • Leaf − Node which does not have any child node is called leaf node.
  • Sub tree − Sub tree represents descendants of a node.
  • Levels − Level of a node represents the generation of a node. If root node is at level 0, then its next child node is at level 1, its grandchild is at level 2 and so on.
  • keys − Key represents a value of a node based on which a search operation is to be carried out for a node.

Characteristics of trees

  • Non-linear data structure
  • Combines advantages of an ordered array
  • Searching as fast as in ordered array
  • Insertion and deletion as fast as in linked list
  • Simple and fast

Introduction To Binary Trees

  • A binary tree, is a tree in which no node can have more than two children.
  • Consider a binary tree T, here ‘A’ is the root node of the binary tree T.
  • ‘B’ is the left child of ‘A’ and ‘C’ is the right child of ‘A’ - i.e A is a father of B and C. - The node B and C are called siblings.
  • Nodes D,H,I,F,J are leaf node

Binary Trees

  • A binary tree, T , is either empty or such that I. T has a special node called the root node II. T has two sets of nodes L T and R T , called the left subtree and right subtree of T , respectively. III. L T and R T are binary trees.

The following figure shows a binary tree with 9 nodes where A is the root

Binary Tree

  • The root node of this binary tree is A.
  • The left sub tree of the root node, which we denoted by L A , is the set L A = {B,D,E,G} and the right sub tree of the root node, R A is the set R A

={C,F,H}

  • The root node of L A is node B, the root node of R A is C and so on

Types of Binary Tree

  • Complete binary tree
  • Strictly binary tree
  • Almost complete binary tree

Strictly binary tree

  • If every non-leaf node in a binary tree has nonempty left and right sub-trees, then such a tree is called a strictly binary tree.
  • Or, to put it another way, all of the nodes in a strictly binary tree are of degree zero or two, never degree one.
  • A strictly binary tree with N leaves always contains 2N – 1 nodes.