 Javascript 10-Aug-2017

### BINARY TREE ALL IN ONE

A binary tree is either empty or consists of a node called the root together with two binary trees called the left subtree and the right subtree.

root
/       \
left      right
/    \      /  \

### SOME FACTS:

• If h = height of a binary tree,
• max # of leaves = 2h
• max # of nodes = 2h + 1 – 1
• Total number of binary trees having n nodes
• = number of stack-realizable permutations of length n
• = number of well-formed parentheses (with n left parentheses and n right parentheses)
• =(1/n+1)(2n C n) [Catalan Number]A binary tree with height h and 2h + 1 – 1 nodes (or 2h leaves) is called a full binary tree
• A binary tree with n nodes is said to be complete if it contains all the first n nodes of the following numbering scheme
1
/      \
2      3
/ \     /
4  5  6
• .Following is not complete binary tree
4
/   \
2     7
/ \     \
1   3     9
• In a (balanced) binary tree with `m` nodes, moving from one level to the next requires one comparison, and there are `log_2(m)` levels, for a total of `log_2(m)` comparisons.
• In contrast, an n-ary tree will require `log_2(n)` comparisons (using a binary search) to move to the next level. Since there are `log_n(m)` total levels, the search will require `log_2(n)*log_n(m)` = `log_2(m)` comparisons total. So, though n-ary trees are more complex, they provide no advantage in terms of total comparisons necessary.
• The reason that binary trees are used more often than n-ary trees for searching is that n-ary trees are more complex, but usually provide no real speed advantage.

### BINARY TREE TRAVERSAL:

1. Preorder

Visit root, visit left subtree in preorder, visit right subtree in preorder

2. Postorder

Visit left subtree in postorder, right subtree in postorder, then the root

3. Inorder

Visit left subtree in inorder, then the root, then the right subtree in inorder
<strong>Example 1:</strong>
1
/    \
2       3

Pre: 123 (root, left, right)
In : 213 (left, root, right)
Post : 231 (left, right, root)
<strong>Example 2 :</strong>
1
\
2
\
3

Pre: 123 (root, left, right)
In : 123 (left, root, right)
Post : 321 (left, right, root)

### PROGRAMMING PERSPECTIVE

A binary tree is a structure comprising nodes, where each node has the following 3 components:

1. Data element: Stores any kind of data in the node
2. Left pointer: Points to the tree on the left side of node
3. Right pointer: Points to the tree on the right side of the node

As the name suggests, the data element stores any kind of data in the node.
The left and right pointers point to binary trees on the left and right side of the node respectively.

If a tree is empty, it is represented by a null pointer.

Commonly-used terminologies

• Root: Top node in a tree
• Child: Nodes that are next to each other and connected downwards
• Parent: Converse notion of child
• Siblings: Nodes with the same parent
• Descendant: Node reachable by repeated proceeding from parent to child
• Ancestor: Node reachable by repeated proceeding from child to parent.
• Leaf: Node with no children
• Internal node: Node with at least one child
• External node: Node with no children

### APPLICATIONS OF BINARY TREES

• Binary Search Tree – Used in many search applications where data is constantly entering/leaving, such as the `map` and `set` objects in many languages’ libraries. These are a data structure in which searching, insertion, and removal are all very fast (about `log(n)` operations).
• Binary Space Partition – Used in almost every 3D video game to determine what objects need to be rendered.
• Binary Tries – Used in almost every high-bandwidth router for storing router-tables.
• Hash Trees – used in p2p programs and specialized image-signatures in which a hash needs to be verified, but the whole file is not available.
• Heaps – Used in implementing efficient priority-queues, which in turn are used for scheduling processes in many operating systems, Quality-of-Service in routers, and A* (path-finding algorithm used in AI applications, including robotics and video games). Also used in heap-sort.
• Huffman Coding Tree – used in compression algorithms, such as those used by the .jpeg and .mp3 file-formats.
• GGM Trees – Used in cryptographic applications to generate a tree of pseudo-random numbers.
• Syntax Tree – Constructed by compilers and (implicitly) calculators to parse expressions.
• Treap – Randomized data structure used in wireless networking and memory allocation.
• T-tree – Though most databases use some form of B-tree to store data on the drive, databases which keep all (most) their data in memory often use T-trees to do so.
• a Manipulate hierarchical data
• Make information easy to search (see tree traversal)
• Manipulate sorted lists of data
• Use as a workflow for compositing digital images for visual effects
• Use in router algorithms