A binary tree is defined as a tree where each node can has no more than and a children.
Building a Binary Search Tree:
First create a node class
Public classBtnode { Public intData {Get;Set; } PublicBtnode Left {Get;Set; } PublicBtnode Right {Get;Set; } Public voidDisplaynode () {Console.Write ("Data: {0},", This. Data); } }
Then create the BST Class to provide the Insert method:
Public classBinarysearchtree {PrivateBtnode root =NULL; Public voidInsert (intdata) {Btnode NewNode=NewBtnode (); if( This. root = =NULL) { This. root =NewNode; } Else { //Add to children nodes.Btnode current =NewBtnode (); Btnode Parent=NewBtnode (); while(true) {Parent=Current ; if(Data <Current . Data) { current=Current . Left; if(Current = =NULL) {parent. Left=NewNode; Break; } } Else{ Current=Current . right; if(Current = =NULL) {parent. Right=NewNode; Break; } } } } } }
Basic concepts and algorithms for (C # Binary Tree)