Yam Code
Sign up
Login
New paste
Home
Trending
Archive
English
English
Tiếng Việt
भारत
Sign up
Login
New Paste
Browse
#include<stdio.h> #include<conio.h> #include<iostream> #include<stdlib.h> #define MAX 11 typedef int ItemType; struct Node { ItemType Info; Node* Left; Node* Right; }; typedef Node* NodeAddress; struct BinarySearchTree { NodeAddress Root; }; void InitTree(BinarySearchTree& x); NodeAddress createNode(ItemType x); void traverseTreeLNR(NodeAddress n); void traverseTreeNLR(NodeAddress n); void traverseTreeRNL(NodeAddress n); int deleteTree(NodeAddress& root); int insertNode(NodeAddress& t, NodeAddress p); int sumDuongAm(NodeAddress Root,int Duong); int countNodeDepth(NodeAddress Root, int Depth); int heightTree(NodeAddress Root); int countAtoB(NodeAddress Root, ItemType A,ItemType B); int sumSoChan(NodeAddress Root); void Menu() { printf("******************************************************\n"); printf("* BINARY SEARCH TREE *\n"); printf("******************************************************\n"); printf("* %-50s *\n","0.Thoat chuong trinh"); printf("* %-50s *\n","1.Tao cay nhi phan tu mot mang cho san"); printf("* %-50s *\n","2.Duyet Cay RLN"); printf("* %-50s *\n","3.Duyet Cay NLR"); printf("* %-50s *\n","4.Tong Am Tren Cay"); printf("* %-50s *\n","5.Tong Duong Tren Cay"); printf("* %-50s *\n","6.Nhap X vao trong cay"); printf("* %-50s *\n","7.Xuat cac gia tri theo chieu giam dan"); printf("* %-50s *\n","8.Dem so nut o muc k "); printf("* %-50s *\n","9.Dem chieu cao cua cay "); printf("* %-50s *\n","10.Tong cac nut co gia tri chan "); printf("* %-50s *\n","11.Dem so gia tri trong doan [a,b]"); printf("******************************************************\n"); } void main() { BinarySearchTree Tree; InitTree(Tree); do { Menu(); int choose; do { printf("Nhap Chuc Nang:"); scanf_s("%d",&choose); } while (!(choose >= 0 && choose <= MAX)); switch (choose) { case 1: { int number; do { printf("Nhap so nguyen vao cay (-999 de ket thuc):"); scanf_s("%d",&number); if(number == -999) break; insertNode(Tree.Root,createNode(number)); } while (true); printf("Nhap xong !!!"); break; } case 2: { printf("Duyet Cay RLN: "); traverseTreeLNR(Tree.Root); break; } case 3: { printf("Duyet Cay RLN: "); traverseTreeNLR(Tree.Root); break; } case 4: { printf("Tong Am Tren Cay: %d\n",sumDuongAm(Tree.Root,0)); break; } case 5: { printf("Tong Duong Tren Cay: %d\n",sumDuongAm(Tree.Root,1)); break; } case 6: { ItemType X; printf("Nhap gia tri X:"); scanf_s("%d",&X); insertNode(Tree.Root,createNode(X)); printf("Da nhap %d vao trong cay\n",X); break; } case 7: { printf("Gia tri giam dan trong cay: "); traverseTreeRNL(Tree.Root); break; } case 8: { ItemType K; printf("Nhap muc k:"); scanf_s("%d",&K); printf("So cac Node trong muc %d la: %d",K,countNodeDepth(Tree.Root,K)); break; } case 9: { printf("Chieu cao cua cay la: %d",heightTree(Tree.Root)); break; } case 10: { printf("Tong Duong Tren Cay: %d\n",sumSoChan(Tree.Root)); break; } case 11: { ItemType A,B; do { printf("Nhap muc A,B [A<B]:"); scanf_s("%d%d",&A,&B); } while (A > B); printf("So gia tri nam trong khoang [%d,%d] la: %d\n",A,B,countAtoB(Tree.Root,A,B)); break; } case 0: { exit(0); break; } } printf("\n\n"); }while(true); } void InitTree(BinarySearchTree& x) { x.Root = NULL; } NodeAddress createNode(ItemType x) { NodeAddress p = new Node; if (p == NULL) return NULL; p->Left = p->Right = NULL; p->Info = x; return p; } void traverseTreeLNR(NodeAddress n) { if (!n) return; traverseTreeLNR(n->Left); printf("%d\t", n->Info); traverseTreeLNR(n->Right); } void traverseTreeRNL(NodeAddress n) { if (!n) return; traverseTreeRNL(n->Right); printf("%d\t", n->Info); traverseTreeRNL(n->Left); } void traverseTreeNLR(NodeAddress n) { if (!n) return; printf("%d\t", n->Info); traverseTreeNLR(n->Left); traverseTreeNLR(n->Right); } int deleteTree(NodeAddress& root) { if (!root) return 0; deleteTree(root->Left); deleteTree(root->Right); delete root; return 1; } int insertNode(NodeAddress& t, NodeAddress p) { if (p == NULL) return 0; if (!t) { t = p; return 1; } if (t->Info == p->Info) return 0; if (p->Info < t->Info) insertNode(t->Left, p); else insertNode(t->Right, p); return 1; } int sumDuongAm(NodeAddress Root,int Duong) { if (!Root) return 0; int LeftSum = sumDuongAm(Root->Left,Duong); int RightSum = sumDuongAm(Root->Right,Duong); if(Duong == 1) if(Root->Info > 0) return LeftSum + RightSum + Root->Info; if(Duong == 0) if(Root->Info < 0) return LeftSum + RightSum + Root->Info; return LeftSum + RightSum; } int countNodeDepth(NodeAddress Root, int Depth) { if (!Root) return 0; Depth--; int LeftCount = countNodeDepth(Root->Left,Depth); int RightCount = countNodeDepth(Root->Right,Depth); if(Depth == 0) return LeftCount + RightCount + 1; return LeftCount + RightCount; } int countAtoB(NodeAddress Root, ItemType A,ItemType B) { if (!Root) return 0; int LeftCount = countAtoB(Root->Left,A,B); int RightCount = countAtoB(Root->Right,A,B); if(Root->Info >= A && Root->Info <= B) return LeftCount + RightCount + 1; return LeftCount + RightCount; } int heightTree(NodeAddress Root) { if (!Root) return 0; int LeftHeight = heightTree(Root->Left); int RightHeight = heightTree(Root->Right); if(LeftHeight > RightHeight) return LeftHeight + 1; return RightHeight + 1; } int sumSoChan(NodeAddress Root) { if (!Root) return 0; int LeftSum = sumSoChan(Root->Left); int RightSum = sumSoChan(Root->Right); if(Root->Info % 2 == 0) return LeftSum + RightSum + Root->Info; return LeftSum + RightSum; }
Paste Settings
Paste Title :
[Optional]
Paste Folder :
[Optional]
Select
Syntax Highlighting :
[Optional]
Select
Markup
CSS
JavaScript
Bash
C
C#
C++
Java
JSON
Lua
Plaintext
C-like
ABAP
ActionScript
Ada
Apache Configuration
APL
AppleScript
Arduino
ARFF
AsciiDoc
6502 Assembly
ASP.NET (C#)
AutoHotKey
AutoIt
Basic
Batch
Bison
Brainfuck
Bro
CoffeeScript
Clojure
Crystal
Content-Security-Policy
CSS Extras
D
Dart
Diff
Django/Jinja2
Docker
Eiffel
Elixir
Elm
ERB
Erlang
F#
Flow
Fortran
GEDCOM
Gherkin
Git
GLSL
GameMaker Language
Go
GraphQL
Groovy
Haml
Handlebars
Haskell
Haxe
HTTP
HTTP Public-Key-Pins
HTTP Strict-Transport-Security
IchigoJam
Icon
Inform 7
INI
IO
J
Jolie
Julia
Keyman
Kotlin
LaTeX
Less
Liquid
Lisp
LiveScript
LOLCODE
Makefile
Markdown
Markup templating
MATLAB
MEL
Mizar
Monkey
N4JS
NASM
nginx
Nim
Nix
NSIS
Objective-C
OCaml
OpenCL
Oz
PARI/GP
Parser
Pascal
Perl
PHP
PHP Extras
PL/SQL
PowerShell
Processing
Prolog
.properties
Protocol Buffers
Pug
Puppet
Pure
Python
Q (kdb+ database)
Qore
R
React JSX
React TSX
Ren'py
Reason
reST (reStructuredText)
Rip
Roboconf
Ruby
Rust
SAS
Sass (Sass)
Sass (Scss)
Scala
Scheme
Smalltalk
Smarty
SQL
Soy (Closure Template)
Stylus
Swift
TAP
Tcl
Textile
Template Toolkit 2
Twig
TypeScript
VB.Net
Velocity
Verilog
VHDL
vim
Visual Basic
WebAssembly
Wiki markup
Xeora
Xojo (REALbasic)
XQuery
YAML
HTML
Paste Expiration :
[Optional]
Never
Self Destroy
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Status :
[Optional]
Public
Unlisted
Private (members only)
Password :
[Optional]
Description:
[Optional]
Tags:
[Optional]
Encrypt Paste
(
?
)
Create New Paste
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Site Languages
×
English
Tiếng Việt
भारत