[ACE] Binary Search Tree


  • Tree Terminology
Root: node without parent
Internal node: node with at least one child
External node: node without children
Depth: number of ancestors(not counting itself)/ number of lines from the node to the root
Height: maximum depth of any node/ length of longest path from root to a leaf

  • Binary Search Tree and its implementation
Binary Search Tree(BST): A binary search tree is a rooted binary tree, whose internal nodes each store a key (and optionally, an associated value) and each have two distinguished sub-trees, commonly denoted left and right. The tree additionally satisfies the binary search tree property, which states that the key in each node must be greater than all keys stored in the left sub-tree, and not greater than all keys in the right sub-tree. 
Simply speaking, it is a binary tree and the left child is always smaller than the parent, the right child is always greater than the parent

Implementation

Traversal: 
Preorder Traversal: 
  • In a preorder traversal, a node is visited before its descendants 

Inorder Traversal
  • In an inorder traversal a node is visited after its left subtree and before its right subtree 


Postorder Traversal
In a postorder traversal, a node is visited after its descendants


  • Properties of perfect binary trees
At level d, the number of nodes on level d is 2d
The number of all nodes are 1+ 2d+1
For the perfect binary trees, the height(h) and size(n) has the following relationship:
  • h=log2 (n+1)-1
which can be proved by induction 
I won't give the prove process of this because it is really simple





评论

此博客中的热门博文

[MLE] W2 Multivariate linear regression

[MLE] W1 Introduction

[AIM] MetaHeuristics