Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace tree
- {
- class BStnode
- {
- public int Data;
- public BStnode left;
- public BStnode right;
- public BStnode(int value)
- {
- Data = value;
- right = left = null;
- }
- public void Printnode()
- {
- Console.WriteLine(Data);
- }
- //insert
- public void insert(int value)
- {
- if (value < Data)
- {
- if (left == null)
- left = new BStnode(value);
- else
- left.insert(value);
- } //end if
- else
- {
- if (right == null)
- right = new BStnode(value);
- else
- right.insert(value);
- } //end else
- } //end insert
- }
- class Bsttree
- {
- public BStnode Root;
- public Bsttree()
- {
- Root = null;
- }
- public void insertnode(int value)
- {
- if (Root == null)
- Root = new BStnode(value);
- else
- Root.insert(value);
- }
- public void preorderhelp(BStnode n)
- {
- if (n == null)
- return;
- Console.WriteLine(n.Data);
- preorderhelp(n.left);
- preorderhelp(n.right);
- }//end preorder
- public void inorderhelp(BStnode n)
- {
- if (n == null)
- return;
- inorderhelp (n.left);
- Console.WriteLine(n.Data);
- inorderhelp(n.right);
- }//end inorder
- public void postrderhelp(BStnode n)
- {
- if (n == null)
- return;
- postrderhelp(n.left);
- postrderhelp(n.right);
- Console.WriteLine(n.Data);
- } //end postorder
- // to print from root in 3 methods
- public void preorder()
- {
- preorderhelp(Root);
- }
- public void inorder()
- {
- inorderhelp(Root);
- }
- public void postorder()
- {
- postrderhelp(Root);
- }
- public BStnode searchhelp(BStnode start, int key)
- {
- if (start == null)
- return null;
- else if (start.Data == key)
- return start;
- else if (start.Data > key)
- return searchhelp(start.left, key);
- else
- return searchhelp(start.right, key);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Bsttree mytree = new Bsttree();
- mytree.insertnode(10);
- mytree.insertnode(11);
- mytree.insertnode(12);
- mytree.insertnode(15);
- mytree.insertnode(19);
- mytree.insertnode(8);
- mytree.insertnode(7);
- mytree.insertnode(5);
- mytree.insertnode(2);
- Console.WriteLine("POST ORDR NLR");
- mytree.postorder();
- Console.WriteLine("_____________________");
- mytree.preorder();
- Console.WriteLine("_________________-");
- mytree.inorder();
- Console.WriteLine("_________________");
- mytree.inorderhelp(mytree.searchhelp(mytree.Root, 8));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement