Blog Author
Tekslate
Published Date
21st September, 2018
Ratings
Views
1077
A binary trees in data structures T is defined as a finite set of elements, called nodes, such that :
If T does contain a root R, Then the two trees T1 and T2 are called, respectively the left and right sub trees of R.
Algebraic expressions Following figure shows some binary trees that represent arithmetic expressions. Each operator (+,-,*,/) may have one or two operands The left operand (if any) is the left sub trees of the operator. The right operand is its right sub tree. The right operand is its right sub tree . The leaf element in an expression tree are either constants or variables. Note that an expression tree contains parenthesis One application of expression trees is in the generation of optimal computer code to evaluate an expression Example : (a-b)/(Cc*d)+e)
Example: (a*b) + (c/d)
Example : ((a+b)+c)+d)
A binary tree of height h that contains exactly 2^{n-1} elements is called a full binary tree. The tree T is said to be complete if all its levels, except possible the last, have the maximum number of possible nodes, and if all the nodes at the last level appear as far left as possible.
The following are the two ways of representing binary trees
Suppose T is a binary tree that is complete or nearly complete or nearly complete. Then there is an efficient representation of T. This representation uses only a single linear array TREE as follows.
Again , Null is used to indicate an empty sub tree in particular, TREE[1]= NULL indicates that the tree is empty. This representation schema is quite waste of space when many elements are mission. The array representation is useful only when the number of missing elements is small.
The most popular way to represent a binary tree is by using links or pointers. Each element is represented by a node. That has exactly two link fields. Let us call these link fields left child and right child. In addition to these two link fields each node has a filed named data. Example:
There are three common ways to traversal a binary tree :
Algorithm Preorder traversal void preorder(Binary Tree Node*t) { // preorder traversal if(t) { printf(“%d”,t- >data); // visit tree root preorder(t-> leftchild); // do left sub tree preorder(t-> right child); // do right sub tree } } Algorithm In order traversal void inorder (Binary TreeNode *t) { // Inorder traversal if(t) { inorder(c-> leftchild); // do left subtree printf(“%d”,t-> data); // visit tree root inorder(t-> right child); // do right sub tree } } Algorithm Post order traversal void post order(Binary Tree Node *t) { if(t) { past order(t-> left child); // do left sub tree past order(t-> right child); // do right sub tree printf(“%d”,t-> data); // visit tree root } }
TekSlate
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .
Write For Us